返回 首页

带有参数的装饰器


1. 函数带多个参数

# 普通的装饰器, 打印函数的运行时间
def decrator(func):
    def wrap(*args, **kwargs):
        start_time = time.time()
        res = func(*args, **kwargs)
        end_time = time.time()
        print('运行时间为', end_time-start_time)
        return res
    return wrap

2. 装饰器带有多个参数

当装饰器带有多个参数的时候, 装饰器函数就需要多加一层嵌套:

比如:

def decrator(*dargs, **dkargs):
    def wrapper(func):
        def _wrapper(*args, **kargs):
            print ("装饰器参数:", dargs, dkargs)
            print ("函数参数:", args, kargs)
            return func(*args, **kargs)
        return _wrapper
    return wrapper

为什么被装饰函数体可以传入内层呢?

装饰器函数有多个参数, 需要以@decrator(1, a=2)的方式使用, 这时候decrator是已经执行的(因为加了括号), 可以粗略的理解为加载被装饰函数的上的是wrapper, 所以这和普通的装饰器并无差别.

   def route(self, rule, **options):
        """Like :meth:`Flask.route` but for a blueprint.  The endpoint for the
        :func:`url_for` function is prefixed with the name of the blueprint.
        """
        def decorator(f):
            endpoint = options.pop("endpoint", f.__name__)
            self.add_url_rule(rule, endpoint, f, **options)
            return f
        return decorator

flask的蓝图route源码中的装饰器, 最内层直接返回return f 并没有多加一层处理的函数, 在无需对被装饰函数进行过多处理的时候这是较为方便的做法. route源码中只是对装饰器参数进行了处理.


登录