python 装饰器和 kwargs 的正确句柄
Proper handle of python decorators and kwargs
我有一个带有装饰器的烧瓶应用程序,我制作它来处理一些值。为了这个问题的目的,我将保持简单:
def decorator(f):
@wraps(f)
def decorated function(*args, **kwargs):
kwargs['test_value'] = 15
return f(*args, **kwargs)
return decorated_function
这个装饰器就在请求之后,比如:
@app.route('/a/<path>/with/<these>')
@decorator
def function_flask(path, these):
print(path, these)
return 1
但是,我得到一个错误 function_flask 得到了一个意外的关键字参数 'test_value'。现在显然,kwargs 'test_value' 以某种方式传递给了 function_flask。但是为什么?
我是否对 kwargs 处理不当?
添加**kwargs
作为function_flask
的第三个参数。
我有一个带有装饰器的烧瓶应用程序,我制作它来处理一些值。为了这个问题的目的,我将保持简单:
def decorator(f):
@wraps(f)
def decorated function(*args, **kwargs):
kwargs['test_value'] = 15
return f(*args, **kwargs)
return decorated_function
这个装饰器就在请求之后,比如:
@app.route('/a/<path>/with/<these>')
@decorator
def function_flask(path, these):
print(path, these)
return 1
但是,我得到一个错误 function_flask 得到了一个意外的关键字参数 'test_value'。现在显然,kwargs 'test_value' 以某种方式传递给了 function_flask。但是为什么?
我是否对 kwargs 处理不当?
添加**kwargs
作为function_flask
的第三个参数。