【发布时间】:2019-10-09 11:57:36
【问题描述】:
我正在研究装饰器,并且通过这个例子我无法弄清楚如何访问作为 wrapper_function 中的参数(函数 display_info)发送的函数的参数而不接收然后作为参数放在 decorator_function 上.
(我想我理解 *args 和 **kwargs 的概念,但在下面的示例中,装饰器函数只能使用一个参数,但在访问 *args 中的包装器表示与一起发送的参数display_info)。
def decorator_function(originla_function):
def wrapper_function(*args, **kwargs):
#how wrapper accessed the arguments that weren't received on decorator_function
print('wrapper executed before {}'.format(originla_function.__name__))
return originla_function(*args, **kwargs)
return wrapper_function
@decorator_function
def display_info(name, age):
print('display_info has the following arguments ({}, {})'.format(name, age))
display_info('Bob', 29)
【问题讨论】:
标签: python