1. 程式人生 > >多個裝飾器裝飾同一個函式

多個裝飾器裝飾同一個函式

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