decorator是python的一个特性。
decorator可以是函数也可以是一个类。
decorator@的用处:
1)在函数开始前做一些初始工作,例如参数校验,调用统计等。
2)在函数结束后做一些收尾工作。
3)类似C++模板的功能。
decorator是函数时,
def decorator(fun):
def call(*args, **kwargs):
print "do something before fun!"
if len(args) < 1:
print "args is less"
else:
fun(args[0])
print "do something after fun!"
return call
@decorator
def hello(a):
print "hello"
# hello(*args, **kwargs)
# is equivalent to
# decorator(hello)(*args, **kwargs)
# see the example for more detail
print "hello() is called:"
hello()
print "-----------------------------"
print "hello(1) is called:"
hello(1)
# decorator chain
def chain0(fun):
def call(*args0, **kwargs0):
print "chain0"
fun(*args0, **kwargs0)
return call
def chain1(fun):
def call(*args1, **kwargs1):
print "chain1"
fun(*args1, **kwargs1)
return call
@chain0
@chain1
def chain2(*args2, **kwargs2):
print "chain2"
# attention!!
# chain2(*args0, **kwargs0)
# is equivalent to chain0(chain1(chain2))(*args0, **kwargs0)
# it is not chain2(*args2, **kwargs2)
# it is not chain0(chain1(chain2(*args0, **kwargs0)))
# it is not chain0(chain1(chain2(*args2, **kwargs2)))
chain2()
运行结果:
hello() is called:
do something before fun!
args is less
do something after fun!
-----------------------------
hello(1) is called:
do something before fun!
hello
do something after fun!
chain0
chain1
chain2
具体原因看注释就明白了。
decorator是类时,
class decorator(object):
def __init__(self, func):
print func
self.func = func
print '__init__'
def __call__(self, *args, **kwargs):
print 'obj = %s, cls = %s' % (self.obj, self.cls)
return self.func.__call__(*args, **kwargs)
def __get__(self, obj, cls):
self.obj = obj
self.cls = cls
print '__get__'
return self.__call__
class foo(object):
@decorator
def hello(self):
print 'hello'
@decorator
def world(self):
print 'world'
tmp = foo()
print '-------------------------'
tmp.hello(tmp)
tmp.world(tmp)
运行结果:
<function hello at 0x2b978ba2d578>
__init__
<function world at 0x2b978ba2d5f0>
__init__
-------------------------
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls = <class '__main__.foo'>
hello
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls = <class '__main__.foo'>
world
可以看出在创建foo实例的时候,
foo中的函数如果有对应的类decorator,
那么类decorator也会分别做相应的初始化。
__get__之后返回一个函数,然后就跟前面的函数decorator一样了。