Python正课41 —— 叠加多个装饰器分析

本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12564486.html

一:叠加多个装饰器的加载、运行分析(了解***)

def deco1(func1):   #func1 = wrapper2的内存地址
    def wrapper(*args,**kwargs):
        print('正在运行======>deco1.wrapper1')
        res1 = func1(*args,**kwargs)    # func1是wrapper2
        return res1
    return wrapper

def deco2(func2):   #func2 = wrapper3的内存地址
    def wrapper2(*args,**kwargs):
        print('正在运行======>deco2.wrapper2')
        res2 = func2(*args,**kwargs)    # func2是wrapper3
        return res2
    return wrapper2

def deco3(x):
    def outter3(func3): #func3 = 被装饰对象index的内存地址
        def wrapper3(*args,**kwargs):
            print('正在运行======>deco2.wrapper3')
            res3 = func3(*args,**kwargs)    # func3是index
            return res3
        return wrapper3
    return outter3


# 加载顺序:自下而上(了解)
@deco1          # deco1(wrapper2的内存地址)                  ===> index = wrapper1的内存地址
@deco2          # deco2(wrapper3的内存地址)                  ===> index = wrapper2的内存地址
@deco3(111)     # ===> @outter3 ===> index = outter3(index) ===> index = wrapper3的内存地址
def index(x,y):
    print('from index %s:%s'%(x,y))



index(1,2)

# 执行顺序:自上而下 即:wrapper1 ==> wrapper2 ==> wrapper3
# index(1,2)    # wrapper1(1,2)
posted @ 2020-03-25 10:42  轻描丨淡写  阅读(209)  评论(0编辑  收藏  举报