怎么给装饰器函数加装饰器????

def decorator_with_args(decorator_to_enhance):
    """
    这个函数将被用来作为装饰器.
    它必须去装饰要成为装饰器的函数.
    它将允许所有的装饰器可以接收任意数量的参数,所以以后你不必为每次都要做这个头疼了.
    saving you the headache to remember how to do that every time.
    """

    # 我们用传递参数的同样技巧.
    def decorator_maker(*args, **kwargs):
        # 我们动态的建立一个只接收一个函数的装饰器,
        # 但是他能接收来自maker的参数
        def decorator_wrapper(func):
            # 最后我们返回原始的装饰器,毕竟它只是'平常'的函数
            # 唯一的陷阱:装饰器必须有这个特殊的,否则将不会奏效.
            print("log")
            return decorator_to_enhance(func, *args, **kwargs)

        return decorator_wrapper

    return decorator_maker


@decorator_with_args
def decorated_decorator(func, *args, **kwargs):  #decorated_decorator= decorator_maker
    def wrapper(function_arg1, function_arg2):
        print("Decorated with")
        return func(function_arg1, function_arg2)

    return wrapper


@decorated_decorator()
def decorated_function(function_arg1, function_arg2):
    print("Hello", function_arg1, function_arg2)


decorated_function("Universe and", "everything")
View Code
原文地址:https://www.cnblogs.com/ltk-python/p/9378404.html