装饰器实际运行轨迹
def f7(x1):
print('加的又一个新功能d')
def f8():
print('加的又一个新功能7')
x1()
print('加的又一个新功能8')
return f8
def f5(x2):
print('加的又一个新功能c')
def f6():
print('加的又一个新功能5')
x2()
print('加的又一个新功能6')
return f6
def f3(x3):
print('加的又一个新功能b')
def f4():
print('加的又一个新功能3')
x3()
print('加的又一个新功能4')
return f4
def f1(x4):
print('加的又一个新功能a')
def f2():
print('加的又一个新功能1')
x4()
print('加的又一个新功能2')
return f2
@f7
@f5
@f3
@f1
def fx():
print('调用fx')
fx()
结果为:
加的又一个新功能a
加的又一个新功能b
加的又一个新功能c
加的又一个新功能d
加的又一个新功能7
加的又一个新功能5
加的又一个新功能3
加的又一个新功能1
调用fx
加的又一个新功能2
加的又一个新功能4
加的又一个新功能6
加的又一个新功能8
解释
fx() - ->f1(fx()) - ->f3(f1(fx())) - ->f3(f1(fx())) - ->f5(f3(f1(fx()))) - ->f7(f5(f3(f1(fx()))))
f7(f5(f3(f1(fx())))) - ->f8
x1=f5(f3(f1(fx())))
f7(x1) - ->f8
f8
x1() - ->f5(f3(f1(fx())))()
f5(f3(f1(fx()))) - ->f6
x2=f3(f1(fx()))
f5(x1) - ->f6
f6
x2() - ->f3(f1(fx()))()
f3(f1(fx())) - ->f4
x3=f1(fx())
f3(x3) - ->f4
f4
x3() - ->f1(fx())()
f1(fx()) - ->f2
x4=fx()
f1(x4) - ->fx()
fx()
倒回去执行丢掉的那一句语句
加的又一个新功能2 - -> 加的又一个新功能4 - ->加的又一个新功能6 - ->加的又一个新功能8
python装饰器改变命运
原创
©著作权归作者所有:来自51CTO博客作者东方佑的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
常用的10个Python装饰器
python中常用的10个装饰器
python 装饰器 py装饰器 -
技术改变命运
技术改变命运!梦想造就未来!
技术改变命运 -
定投改变命运?python 帮你解答
其他
数据 html python -
改变命运的这八年!
是的,我是既得利益者。
公众号 微信 商业 迭代 微信公众平台