深入理解Python 装饰器(decorator)
来源:互联网 发布:机械吞噬者数据 编辑:程序博客网 时间:2024/06/05 06:39
转载请注明来源:http://blog.csdn.net/TangHuanan/article/details/45094497
返璞归真, 看山还是山
刚看到Python装饰器时, 觉得很神奇。简单实验下,发现也就那么回事。但是慢慢的看到越来越多的装饰器。很多时候又不了解到底是怎么回事了。
最后还是决定好好研究下。
先看看一些实例, 然后再来分析下原理
假设我们有如下的基本函数
def do_something(): for i in range(1000000): pass print "play game"do_something()
结果如下:
play game
需求1: 统计函数的执行时间
1. 不是装饰器的装饰器
import timedef decorator(fun): start = time.time() fun() runtime = time.time()-start print runtimedef do_something(): for i in range(1000000): pass print "play game"decorator(do_something)
结果如下:
play game0.0299999713898
这种实现看上去还可以,但是每次调用的是decorator,还要把函数作为一个参数传入。这样需要修改调用的地方,使用起来就不方便了。
2. 最简单的装饰器
import timedef decorator(fun): def wrapper(): start = time.time() fun() runtime = time.time()-start print runtime return wrapper@decoratordef do_something(): for i in range(1000000): pass print "play game"do_something()
结果如下:
play game0.0329999923706
装饰器是在函数定义时前面加@,然后跟装饰器的实现函数。可以看出,现在只要直接调用do_something就可以了。调用的地方不要作任何修改。
3. 目标函数带固定参数的装饰器
import timedef decorator(fun): def wrapper(name): start = time.time() fun(name) runtime = time.time()-start print runtime return wrapper@decoratordef do_something(name): for i in range(1000000): pass print "play game " + namedo_something("san guo sha")
结果如下:
play game san guo sha0.039999961853
实现很简单, 就是给wrapper函数参加相同的参数
4. 目标函数带不固定参数的装饰器
import timedef decorator(fun): def wrapper(*args, **kwargs): start = time.time() fun(*args, **kwargs) runtime = time.time()-start print runtime return wrapper@decoratordef do_something(name): for i in range(1000000): pass print "play game " + name@decoratordef do_something2(user, name): for i in range(1000000): pass print user+" play game " + namedo_something("san guo sha")do_something2("wang xiao er","san guo sha")
结果如下:
play game san guo sha0.029000043869wang xiao er play game san guo sha0.0310001373291
需求2: 目标函数每次调用重复执行指定的次数
5. 让装饰器带参数
import timedef decorator(max): def _decorator(fun): def wrapper(*args, **kwargs): start = time.time() for i in xrange(max): fun(*args, **kwargs) runtime = time.time()-start print runtime return wrapper return _decorator@decorator(2)def do_something(name): for i in range(1000000): pass print "play game " + namedo_something("san guo sha")
结果如下:
play game san guo shaplay game san guo sha0.0600001811981
6. 原理
看了这么多实例, 装饰器的基本类型也基本上都有了。是不是清楚了呢?
如果还是不清楚,那就继续看下面的内容。
1 不带参数的装饰器
@a_decoratordef f(...): ...#经过a_decorator后, 函数f就相当于以f为参数调用a_decorator返回结果。f = a_decorator(f)
来分析这个式子, 可以看出至少要满足以下几个条件
1. 装饰器函数运行在函数定义的时候
2. 装饰器需要返回一个可执行的对象
3. 装饰器返回的可执行对象要兼容函数f的参数
2 验证分析
1 装饰器运行时间
import timedef decorator(fun): print "decorator" def wrapper(): print "wrapper" start = time.time() fun() runtime = time.time()-start print runtime return wrapper@decoratordef do_something(): for i in range(1000000): pass print "play game"
结果如下:
decorator
可以看出, 这里的do_something并没有调用, 但是却打印了decorator, 可wrapper没有打印出来。也就是说decorator是在do_something调用的时候执行的。
2 返回可执行的对象
import timedef decorator(fun): print "decorator" def wrapper(): print "wrapper" start = time.time() fun() runtime = time.time()-start print runtime return None@decoratordef do_something(): for i in range(1000000): pass print "play game"do_something()
结果如下:
decoratorTraceback (most recent call last): File "deco.py", line 17, in <module> do_something()TypeError: 'NoneType' object is not callable
3 兼容函数f的参数
import timedef decorator(fun): print "decorator" def wrapper(): print "wrapper" start = time.time() fun() runtime = time.time()-start print runtime return wrapper@decoratordef do_something(name): for i in range(1000000): pass print "play game"do_something("san guo sha")
结果如下:
decoratorTraceback (most recent call last): File "deco.py", line 17, in <module> do_something("san guo sha")TypeError: wrapper() takes no arguments (1 given)
看到这里, 至少对不带参数的装饰器应该全弄清楚了, 也就是说能到看山还是山了。
3 带参数的装饰器
这里就给一个式子, 剩下的问题可以自己去想
@decomaker(argA, argB, ...)def func(arg1, arg2, ...): pass#这个式子相当于func = decomaker(argA, argB, ...)(func)
4 被装饰过的函数的函数名
import timedef decorator(fun): def wrapper(): start = time.time() fun() runtime = time.time()-start print runtime return wrapper@decoratordef do_something(): print "play game"print do_something.__name__
结果如下:
wrapper
可以看出, do_something的函数名变成了wrapper,这不是我们想要的。原因估计各位也都清楚了。那要怎么去解决呢?
import timedef decorator(fun): def wrapper(): start = time.time() fun() runtime = time.time()-start print runtime wrapper.__name__ = fun.__name__ return wrapper@decoratordef do_something(): print "play game"print do_something.__name__
结果如下:
do_something
但是这个看起来是不是很不专业, python的unctools.wraps提供了解决方法
import timeimport functools def decorator(fun): @functools.wraps(fun) def wrapper(): start = time.time() fun() runtime = time.time()-start print runtime return wrapper@decoratordef do_something(): print "play game"print do_something.__name__
结果如下:
do_something
到此为止, 你是不是觉得已经完全明白了呢?
但事实是, 这其实还不够
7. 装饰器类
需求3: 让函数只能运行指定的次数
前面我们讲的都是函数式的装饰器, 那么类能不能成为装饰器呢?
import timeimport functools class decorator(object): def __init__(self, max): self.max = max self.count = 0 def __call__(self, fun): self.fun = fun return self.call_fun def call_fun(self, *args, **kwargs): self.count += 1 if ( self.count == self.max): print "%s run more than %d times"%(self.fun.__name__, self.max) elif (self.count<self.max): self.fun(*args, **kwargs) else: pass@decorator(10)def do_something(): print "play game"@decorator(15)def do_something1(): print "play game 1"for i in xrange(20): do_something() do_something1()
结果如下:
play gameplay game 1play gameplay game 1play gameplay game 1play gameplay game 1play gameplay game 1play gameplay game 1play gameplay game 1play gameplay game 1play gameplay game 1do_something run more than 10 timesplay game 1play game 1play game 1play game 1play game 1do_something1 run more than 15 times
是不是感觉有点怪, 但它确实是可行的。
在Python中, 其实函数也是对象。 反过来, 对象其实也可以像函数一样调用, 只要在类的方法中实现__call__方法。回想一下创建对象的过程
class A: def __init__(self): passa = A()
这其实和函数调用没什么区别, 那么把这个式子代入到之前两个装饰器的式子中,结果如下:
带参数的装饰器
fun = A.__init__(args)(fun)
不带参数的装饰器
fun = A.__init__(fun)()
现在装饰器的内容基本差不多了。 还有一些问题, 可以自己去尝试研究。
自己的才是自己的
还有几个问题如下:
1. 类装饰器(装饰器装饰的对象是类)
2. 类函数装饰器(装饰器装饰的对象是类的函数)
3. 多个装饰器一起使用(函数嵌套)
8 参考资料
https://www.python.org/dev/peps/pep-0318/
https://docs.python.org/2/library/functools.html
- 深入理解Python 装饰器(decorator)
- Python 装饰器( Decorator)
- python装饰器(decorator)
- Python Decorator(装饰器)
- python decorator(装饰器)
- Python 装饰器/Decorator
- Python decorator(装饰器)
- 装饰器 decorator (python)
- Python装饰器 Decorator
- python 装饰器 decorator
- 【Python】装饰器 - Decorator
- python decorator装饰器
- python装饰器(decorator)
- Python 装饰器decorator
- python-装饰器Decorator
- Python Decorator装饰器
- python--装饰器(decorator)
- Python decorator装饰器
- 设计模式:2)策略设计模式
- 从“hello”开始去分析linux下的socket编程
- 深圳医院3D打印钛合金假体置换患者肩胛骨
- sqlite如何避免重复建表(获取已经存在的表)
- js中变量和jsp中java代码中变量互相访问解决方案
- 深入理解Python 装饰器(decorator)
- 排序
- 从小到大排序2
- zookeeper原理
- Mac OS X上使用Wireshark抓包
- UVA 11292 Dragon of Loowater(贪心+排序)
- hadoop 2.5.2 + hive1.1.0 单机环境安装步骤
- apache-tomcat 集群配置
- iOS应用架构谈 开篇