一个函数用两个装饰器(极大的拓展函数额外功能)

def set_fun1(func1):
    print("set_func1")


    def call_fun1():
        print("call_func1")
        func1()


    return call_fun1


def set_fun2(func2):
    print("set_func2")


    def call_func2():
        print("call_func2")
        func2()


    return call_func2


@set_fun1 # set_fun1 ==> test = set_fun1(test)
@set_fun2
def test():
    print("test")


test()

# 注意:函数与装饰器之间调用到引用先由内至外,再由外到内调用。先 def dest>@set_fun2>@set_fun1,然后逆方向回调引用,执行@set fun1>@set_fun2>def test

    所以打印结果为:set_func2
                               set_func1
                               call_func1
                               call_func2

                               test

感兴趣的朋友可以自行验证一下!

猜你喜欢

转载自blog.csdn.net/weixin_42139375/article/details/80555258
今日推荐