多个装饰器装饰同一个函数

def wrapper1(fn):
    def inner(*args, **kwargs):
        print("1111111")
        ret = fn(*args, **kwargs)
        print("2222222")
        return ret
    return inner

def wrapper2(fn):
    def inner(*args, **kwargs):
        print("3333333")
        ret = fn(*args, **kwargs)
        print("44444444")
        return ret
    return inner

def wrapper3(fn):
    def inner(*args, **kwargs):
        print("555555")
        ret = fn(*args, **kwargs)
        print("666666")
        return ret
    return inner


# 就近原则
@wrapper1
@wrapper2
@wrapper3
def func():
    print("我是可怜的func")

func()
# 1 2 3  func 3 2 1

  

posted @ 2018-12-14 16:37  =-=-  阅读(324)  评论(0编辑  收藏  举报