[英]How pass a function with parameters as a parameter to another function in Python?
I'm studying decorators and have came through this example that i can't figure it out how it's possible to access the function's parameters sent as parameter (function display_info) within the wrapper_function without receveing then as argument on first place on decorator_function.我正在研究装饰器,并且通过这个例子我无法弄清楚如何访问在 wrapper_function 中作为参数(函数 display_info)发送的函数参数,而不会将其作为参数接收到 decorator_function 的首位。
(I think I understand the concept of *args and **kwargs, but in the example below it the decorator function only gets one argument to work with, but wrapper that's within access *args that represent the parameters sent along side with display_info). (我想我理解 *args 和 **kwargs 的概念,但在下面的示例中,装饰器 function 仅获得一个可使用的参数,但在访问 *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)
You have to understand that:你必须明白:
@decorator_function
def display_info(name, age):
# ...
is basically just:基本上只是:
def display_info(name, age):
# ...
display_info = decorator_function(display_function)
decorator_function
transforms the display_function
function into another function. decorator_function
将display_function
function 转换为另一个 function。 This is done only once.这仅进行一次。 That's it, nothing more.
仅此而已,仅此而已。 From now on, what matters is what
decorator_function
returned, not how decorator_function
was called in order to create this new function.从现在开始,重要的是
decorator_function
返回了什么,而不是如何调用decorator_function
来创建这个新的 function。
Now, what happens behind the scenes in your example?现在,在您的示例中,幕后发生了什么?
@decorator_function
def display_info(name, age):
replaces display_info
with the result of decorator_function(display_info)
.将
display_info
替换为decorator_function(display_info)
的结果。 What does decorator_function(display_info)
return? decorator_function(display_info)
返回什么? This function:这个 function:
def wrapper_function(*args, **kwargs):
#how wrapper accessed the arguments that weren't received on decorator_function
print('wrapper executed before {}'.format(display_info.__name__))
return display_info(*args, **kwargs)
Notice that I replaced originla_function
with display_info
because the originla_function
parameter of decorator_function
is the original display_info
.请注意,我将
originla_function
替换为display_info
,因为decorator_function
的originla_function
参数是原始display_info
。
So running display_info('Bob', 29)
calls the new display_info
, ie wrapper_function('Bob', 29)
.所以运行
display_info('Bob', 29)
调用新的display_info
,即wrapper_function('Bob', 29)
。 When wrapper_function
is called like this, *args
will be ('Bob', 29)
and **kwargs**
will be {}
.当像这样调用
wrapper_function
时, *args
将是('Bob', 29)
并且**kwargs**
将是{}
。
return display_info(*args, **kwargs)
means it returns the result of calling the original display_info
.表示它返回调用原始
display_info
的结果。 More precisely display_info(*('Bob', 29), **{})
which is the same as display_info('Bob', 29)
.更准确地说
display_info(*('Bob', 29), **{})
与display_info('Bob', 29)
相同。
In your decorator just access args[0]
and args[1]
- those are positional arguments you pass as to any other method or function:在您的装饰器中,只需访问
args[0]
和args[1]
- 它们是位置 arguments 您传递给任何其他方法或 function:
print(args[0], args[1])
in the decorator outputs在装饰器输出中
Bob 29
so in your decorator:所以在你的装饰器中:
def decorator_function(originla_function):
def wrapper_function(*args, **kwargs):
# Output positional arguments args[0] and args[1] or implement logic processing them
print(args[0], args[1])
print('wrapper executed before {}'.format(originla_function.__name__))
return originla_function(*args, **kwargs)
return wrapper_function
*args
are your positional (only them you're passing in your case), and **kwargs
are the keyword arguments, which you're not using in your example. *args
是您的位置(只有它们在您的案例中传递), **kwargs
是关键字 arguments,您在示例中没有使用它。
If you want to use a parametrized decorator that uses parameters passed to it, here's an example:如果您想使用使用传递给它的参数的参数化装饰器,这里有一个示例:
def tags(tag_name):
def tags_decorator(func):
def func_wrapper(name):
return "<{0}>{1}</{0}>".format(tag_name, func(name))
return func_wrapper
return tags_decorator
@tags("p")
def get_text(name):
return "Hello "+name
if __name__=='__main__':
print get_text("John")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.