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, 所以这和普通的装饰器并无差别.

  • 又如flask源码中的:
   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源码中只是对装饰器参数进行了处理.

(Author)
No description.Please update your profile.

View all post by admin

One thought on “带有参数的装饰器

  1. Admin

    10 months ago

    import inspect
    from functools import wraps
    
    def request_mapping(value: str):
        def get_func(o: type):
            class_request_mapping = dict()
            if inspect.isclass(o):
                for method_name in dir(o):
                    if hasattr(getattr(o, method_name), 'request_mapping'):
                        class_request_mapping.setdefault(method_name, value + getattr(o, method_name).request_mapping)
                setattr(o, 'request_mapping', class_request_mapping)
            else:
                setattr(o, 'request_mapping', value)
    
    <pre><code>    @wraps(o)
        def inner(*args, **kwargs):
            return o(*args, **kwargs)
    
        return inner
    
    return get_func
    </code></pre>
    
    <h1>request_mapping = makeRegisteringDecorator(rem)</h1>
    
    @request_mapping(value='/hello')
    class TestView():
    
    <pre><code>@request_mapping(value="/get_list")
    def get(self, request):
        print('get')
    
    @request_mapping(value="/set_name")
    def set_name(self):
        print('set')
    </code></pre>
    
    if <strong>name</strong> == '<strong>main</strong>':
        print(TestView.request_mapping)
    

Leave Comments

我们立足于美利坚合众国,对全球华人服务,受北美法律保护