理解:
规律:
一个函数可以被多个装饰器装饰. @wrapper1 @wrapper2 def target(): print('我是目标') 规则和规律 wrapper1 wrapper2 TARGET wrapper2 wrapper1
def wrapper1(fn): # fn: wrapper2.inner
def inner(*args, **kwargs):
print("这里是wrapper1 进入") # 1
ret = fn(*args, **kwargs) # wrapper2.inner
print("这里是wrapper1 出去") # 5
return ret
return inner
def wrapper2(fn): # fn: target
def inner(*args, **kwargs):
print("这里是wrapper2 进入") # 2
ret = fn(*args, **kwargs) # target
print("这里是wrapper2 出去") # 4
return ret
return inner
@wrapper1 # target = wrapper1(wrapper2.inner) => target: wrapp1.inner
@wrapper2 # target = wrapper2(target) => target: wrapper2.inner
def target():
print('我是目标') # 3
target()
结果: