中多个装饰器的执行顺序

中多个装饰器的执行顺序

python 中多个装饰器的执行顺序:

def wrapper1(f1):
print('in wrapper1')
def inner1(*args,**kwargs):
print('in inner1')
ret = f1(*args,**kwargs)
print('111')
return ret
return inner1 def wrapper2(f2):
print('in wrapper2')
def inner2(*args,**kwargs):
print('in inner2')
ret = f2(*args,**kwargs)
print('222')
return ret
return inner2 def wrapper3(f3):
print('in wrapper3')
def inner3(*args,**kwargs):
print('in inner3')
ret = f3(*args,**kwargs)
print('333')
return ret
return inner3 @wrapper1 #3 func = wrapper1(func)即 func = wrapper2(inner2) -->f1 = inner2 -->打印in wrapper1 --> func = inner1
@wrapper2 #2 func = wrapper2(func)即 func = wrapper2(inner3) -->f2 = inner3 --> 打印in wrapper2 --> func = inner2
@wrapper3 #1 func = wrapper3(func) --> f3 = func -->打印in wrapper3 --> func = inner3
def func(): # 先执行离被装饰函数最近的那个装饰器
print('in func')
func() #4 func = inner1
#5 执行func() 即 执行inner1()-->打印in inner1 --> 执行f1()即执行inner2()-->打印in inner2-->执行f2()即执行inner3()-->打印in inner3-->
#6 -->执行f3()即执行func()-->打印in func-->打印333-->inner3执行完毕,返回到inner2-->打印222-->inner2执行完毕,返回到inner1-->
#7 -->打印111-->inner1执行完毕-->程序执行完毕 # 结果:
# in wrapper3
# in wrapper2
# in wrapper1
# in inner1
# in inner2
# in inner3
# in func
# 333
# 222
# 111 # 装饰器函数在被装饰函数定义好后立即执行。多个装饰器的调用顺序是自下往上的(装饰器装饰函数时的上下顺序)。
# 被装饰函数执行时,装饰器的执行顺序是从上往下的(装饰器装饰函数时的上下顺序,和装饰器函数定义顺序无关)。
04-20 19:27