今天看到python中的一个修饰符'@',不了解它的使用,查看了下官方文档,有了一点了解。
原文 PEP-318 网址:http://www.python.org/dev/peps/pep-0318/
不得不佩服老外,治学很严谨,在python网站相关网页上把为什么使用decorator(主要为了简便一些代码),以及使用什么字符,甚至语法怎么设计写了个详详细细,好长的一篇啊。
这是查看的其中一篇,我翻译关键部分的一些内容,又摘取一些有用的,有空再翻译。
@dec2 @dec1 def func(arg1, arg2, ...): pass
def func(arg1, arg2, ...): pass func = dec2(dec1(func))
Much of the discussion on comp.lang.python and the python-dev mailing list focuses on the use of decorators as a cleaner way to use the staticmethod() and classmethod() builtins. This capability is much more powerful than that. This section presents some examples of use.
在comp.lang.python 和 python-dev的大部分讨论集中在更简捷地使用内置修饰符staticmethod() 和 classmethod() 上。但修饰符的功能远比这强大。下面会对它的使用进行一些讲解:
1.Define a function to be executed at exit. Note that the function isn't actually "wrapped" in the usual sense.
def onexit(f): import atexit atexit.register(f) return f @onexit def func(): ...
2. Define a class with a singleton instance. Note that once the class disappears enterprising programmers would have to be more creative to create more instances. (From Shane Hathaway onpython-dev.)
def singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance @singleton class MyClass: ...
def attrs(**kwds): def decorate(f): for k in kwds: setattr(f, k, kwds[k]) return f return decorate @attrs(versionadded="2.2", author="Guido van Rossum") def mymethod(f): ...
def accepts(*types): def check_accepts(f): assert len(types) == f.func_code.co_argcount def new_f(*args, **kwds): for (a, t) in zip(args, types): assert isinstance(a, t), \ "arg %r does not match %s" % (a,t) return f(*args, **kwds) new_f.func_name = f.func_name return new_f return check_accepts def returns(rtype): def check_returns(f): def new_f(*args, **kwds): result = f(*args, **kwds) assert isinstance(result, rtype), \ "return value %r does not match %s" % (result,rtype) return result new_f.func_name = f.func_name return new_f return check_returns @accepts(int, (int,float)) @returns((int,float)) def func(arg1, arg2): return arg1 * arg2
def provides(*interfaces): """ An actual, working, implementation of provides for the current implementation of PyProtocols. Not particularly important for the PEP text. """ def provides(typ): declareImplementation(typ, instancesProvide=interfaces) return typ return provides class IBar(Interface): """Declare something about IBar here""" @provides(IBar) class Foo(object): """Implement something here..."""Of course, all these examples are possible today, though without syntactic support.