</pre><h1>1.<span style="white-space:pre"> </span>装饰器说明</h1><div></div><div><pre name="code" class="python">import time
def timeit(func):
def wrapper():
b = time.time()
func()
e = time.time()
print "tv:", e - b
return wrapper
@timeit
def foo():
print 'im foo'
time.sleep(1)
foo()
print dir(foo)
print foo.__name__
print foo.__closure__
print foo.__closure__[0]
print dir(foo.__closure__[0])
print foo.__closure__[0].cell_contents
foo.__closure__[0].cell_contents()
执行结果:
im foo
tv: 1.00193500519
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
wrapper
(<cell at 0x2926b40: function object at 0x2922668>,)
<cell at 0x2926b40: function object at 0x2922668>
['__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'cell_contents']
<function foo at 0x2922668>
im foo
说明: 这个例子很好的说明了装饰器的实质, 装饰器用起来用些抽象,但可见实质并不复杂。
装饰器其实就是新定义一个函数,在这个函数内部调用要被装饰的函数,除此之外还可以在这个被装饰的函数执行前后做一些切面编程的事情。
而@timeit 其实是做了一件事情,即 foo=timeit(foot)。这样就明白了吧。之后对对foo的调用其实是对wrapper的调用。这个可从
<span style="white-space:pre"> </span>print foo.__name__
看出。那如果有一个地方想要这个函数不要被装饰回到本来面目,怎么办呢?可以通过
<span style="white-space:pre"> </span>foo.__closure__[0].cell_contents
来得到。当然这里的[0],是要看函数被包装的层次。
以上很好的装饰了foo,但有一点,正如上面所说,foo已不是原来的foo,如果用到反射的话,就变得奇怪了, foo.__name__竟然是wrapper。
这怎么办呢,查阅一下functools模块的wraps这个装饰器就可以明白了。如下例子:
import time
import functools
def timeit(func):
def wrapper():
@functools.wraps(func)
def __w():
print 'im w b'
b = time.time()
func()
e = time.time()
print "tv:", e - b
print 'im w a'
return __w
return wrapper()
@timeit
def foo():
print 'im foo'
time.sleep(3)
foo()
print dir(foo)
print foo.__name__
print foo.__closure__
print foo.__closure__[0]
print dir(foo.__closure__[0])
print foo.__closure__[0].cell_contents
foo.__closure__[0].cell_contents()
执行结果:
im w b
im foo
tv: 3.00361323357
im w a
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
foo
(<cell at 0x1cb4c90: function object at 0x1cb0848>,)
<cell at 0x1cb4c90: function object at 0x1cb0848>
['__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'cell_contents']
<function foo at 0x1cb0848>
im foo
可见:fool.__name__已经是foo了。这其实是通过改变foo的属性做到的,即:foo.__name__= 'foo'.
以上说明了装饰器及其实质。