python装饰器解释: 如果通过@语法使用多个装饰器,就需要按照自底向上的顺序来应用它们,首先创建函数,然后立即使用装饰器将其封装起来: (借用一篇代码来做分析 http://lib.csdn.net/article/python/62942 (如果打不开就复制url在浏览器打开))
def dec1(func):
print("1111")
def one():
print("2222")
func()
print("3333")
return one
def dec2(func):
print("aaaa")
def two():
print("bbbb")
func()
print("cccc")
return two
@dec1
@dec2
def test():
print("test test")
输出:
aaaa
1111
2222
bbbb
test test
cccc
3333
首先发生的是由解释器创建test()函数,然后向上应用dec2装饰器。该函数返回了一个可调用函数,该函数被发送给dec1装饰器,同理,dec1也做了同样的事情,接下来结果被赋给test函数