装饰器 无惨固定模式 和 有参装饰器的固定模式

"""
无参装饰器
from functools import wraps  
"""
def outter(func):
    @wraps(func)    #wraps 装饰器修补技术
    def inner(*args,**kwargs):
        print("装饰前的操作")
        res = func(*args,**kwargs)
        print("装饰后的操作")
        return res
    return inner
"""
装饰器修补技术 
1 返回原来的函数 的函数名
2 返回原来的 函数注释
"""

"""
2. 有参装饰器(最复杂就是三层)
"""
def wrappers(data):
    def outter(func):
        def inner(*args,**kwargs):
            if data=="file":
                #执行被装饰函数之前的操作
                res = func(*args,**kwargs)
                #执行被装饰函数之后的操作
                return res
            return inner
        return outter


3.装饰器的嵌套
1.装饰的时候 从下往上执行(******)

@outter1 # index = outter1(func2)
@outter2 # func2 = outter2(func1)
@outter3 # func1 = outter3(index)
def index():
pass

原文地址:https://www.cnblogs.com/yangxinpython/p/11189517.html