装饰器

</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'.

以上说明了装饰器及其实质。






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值