zoukankan      html  css  js  c++  java
  • day14补充:装饰器思考题 ( 多个装饰器叠加)

    # -*- coding: utf-8 -*-
    # @Time:
    # @Auther: kongweixin
    # @File:

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

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

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

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


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

    # print(index)
    """
    输出结果:
    <function deco1.<locals>.wrapper1 at 0x000001636005F318> 所以我们推断的加载过程是正确的!!!!!!!!


    """

    # 执行顺序自上而下的,即wraper1-》wrapper2-》wrapper3

    index(1,2) # wrapper1(1,2)

    """

    输出结果:
    正在运行===>deco1.wrapper1
    正在运行===>deco2.wrapper2
    正在运行===>deco3.outter3.wrapper3
    from index 1:2

    """

    执行顺序自上而下的,即wraper1-》wrapper2-》wrapper3

    
    
  • 相关阅读:
    代理模式和装饰模式的理解
    Mysql常用命令
    java动态代理(JDK和cglib)
    MyEclipse中SVN使用步骤
    ActionContext和ServletActionContext小结
    java和unicode
    Win7下telnet使用
    MyEclipse8.5安装SVN插件
    linux常用命令(基础)
    选择TreeView控件的树状数据节点的JS方法
  • 原文地址:https://www.cnblogs.com/kwkk978113/p/13303622.html
Copyright © 2011-2022 走看看