python中的 @ 修饰符
2015-09-12 22:30
555 查看
今天学习廖老师的python教程,碰到了修饰符'@',不太了解,查看了下官方文档。
简单的整理下:
等价于
使用示例:
在comp.lang.python 和 python-dev的大部分讨论集中在更简捷地使用内置修饰符staticmethod() 和 classmethod() 上。但修饰符的功能远比这强大。下面会对它的使用进行一些讲解:
1.定义一个执行即退出的函数。注意,这个函数并不像通常情况那样,被真正包裹。
(Note that this example is probably not suitable for real usage, but is for example purposes only.)
注意,这个示例可能并不能准确表达在实际中的使用,它只是做一个示例。
2.定义一个只能产生一个实例的类(有实例后,这个类不能再产生新的实例)。注意,一旦这个类失效了(估计意思是保存在下文的singleton中字典中的相应键失效),就会促使程序员让这个类产生更多的实例。(来自于python-dev的Shane Hathaway)
3.Add attributes to a function. (Based on an example posted by Anders Munch on python-dev.)
4.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:
5.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].
简单的整理下:
@dec2 @dec1 def func(arg1, arg2, ...): pass
等价于
def func(arg1, arg2, ...): pass func = dec2(dec1(func))
使用示例:
在comp.lang.python 和 python-dev的大部分讨论集中在更简捷地使用内置修饰符staticmethod() 和 classmethod() 上。但修饰符的功能远比这强大。下面会对它的使用进行一些讲解:
1.定义一个执行即退出的函数。注意,这个函数并不像通常情况那样,被真正包裹。
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.)
注意,这个示例可能并不能准确表达在实际中的使用,它只是做一个示例。
2.定义一个只能产生一个实例的类(有实例后,这个类不能再产生新的实例)。注意,一旦这个类失效了(估计意思是保存在下文的singleton中字典中的相应键失效),就会促使程序员让这个类产生更多的实例。(来自于python-dev的Shane Hathaway)
def singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance @singleton class MyClass: ...
3.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): ...
4.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
5.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..."""
相关文章推荐
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"
- 《head first python》——定制数据对象
- [有码有真相]python unittest2 源码解读及各类运行测试方法示例
- python简单爬虫例子(一)
- python对字符串实现四则运算
- Python 爬虫 urllib2异常处理
- Python学习----错误、调试和测试
- numpy函数fromfunction分析
- [python] 爬取乌云知识库的标题
- Python+OpenCV学习(8)---Harris_corner
- python简单小记
- python模拟新浪微博登陆之获取cookies
- Python如何调用R:rpy2使用介绍
- python-property
- python-property
- python-traceback
- python学习——collections模块
- python画柱状图并且输出到html文件
- 《机器学习实战》笔记之七——利用AdaBoost元算法提高分类性能
- Python中的AttributeError错误的常见原因