# !/usr/bin/env python
# -*- coding:utf-8 -*-
def wrapper1(func1): # func1 == func原函数
def inner1(*args, **kwargs):
print('wrapper1, before func')
ret1 = func1(*args, **kwargs)
print('wrapper1, after func')
return ret1
return inner1
def wrapper2(func2): # func2 = inner1
def inner2(*args, **kwargs):
print('wrapper2, before func')
ret2 = func2(*args, **kwargs)
print('wrapper2, after func')
return ret2
return inner2
@wrapper2 # 第二步: func = wrapper2(func), 执行装饰器之前根据第一步func==inner1, 执行装饰器wrapper2之后,func2==inner1,左边的func==inner2
@wrapper1 # 第一步: func = wrapper1(func), 执行装饰器wrapper1,则func1 == func, 左边的 func==inner1
def func():
print("Hello World!")
func() # 第三步: func() == inner2() ,然后根据调用依次执行函数 , (根据第二步:func==inner2)
执行顺序如图: