Python decorators example
2016-03-20 16:33
197 查看
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.
Define a function to be executed at exit. Note that the function isn't actually "wrapped" in the usual sense.
Note that this example is probably not suitable for real usage, but is for example purposes only.
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 on python-dev .)
Add attributes to a function. (Based on an example posted by Anders Munch on python-dev .)
Enforce function argument and return types. Note that this copies the func_name attribute from the old to the new function. func_name was made writable in Python 2.4a3:
Declare that a class implements a particular (set of) interface(s). This is from a posting by Bob Ippolito on python-dev based
on experience with PyProtocols [27] .
Of course, all these examples are possible today, though without syntactic support.
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.
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(): ...
Note that this example is probably not suitable for real usage, but is for example purposes only.
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 on python-dev .)
def singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance @singleton class MyClass: ...
Add attributes to a function. (Based on an example posted by Anders Munch on python-dev .)
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): ...
Enforce function argument and return types. Note that this copies the func_name attribute from the old to the new function. func_name was made writable in Python 2.4a3:
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
Declare that a class implements a particular (set of) interface(s). This is from a posting by Bob Ippolito on python-dev based
on experience with PyProtocols [27] .
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.
相关文章推荐
- python 关键字 之 as
- python,针对所有邮件的自由收发。(例子,QQ发给任何邮箱)
- 添加自定义的Python模块方法
- Python初体验之基础篇(1)- 算术和变量
- python assert的作用
- Python爬虫之MySql5.7.11数据库安装教程及其路径选择
- 【边喝caffee边Caffe 】(二)python版本的报错:Mean shape incompatible with input shape
- 以文件为单位的分句、分词python封装脚本
- 解决OS X下Pycharm无法识别Python第三方库的问题
- Python Challenge 10
- python——sys
- Python 遍历子文件和所有子文件夹
- ssh的工作原理之python获取远程主机公钥
- python 关键字 之 and 与 or
- leetcode之jump game II
- 关于python爬虫的一点心得
- Python 09 模块学习
- Python 08 re 正则表达式
- Python常用模块
- Python 07 Pickle 与 Json 序列化