简体   繁体   English

如何将带有参数的 function 作为参数传递给 Python 中的另一个 function?

[英]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_functiondisplay_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_functionoriginla_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.

相关问题 如何在Python中传递接收参数作为另一个函数的参数的方法 - How to pass a method that receives parameters as a parameter of another function in Python 如何将参数列表传递给Python中的另一个函数? - How to pass a parameter list to another function in Python? 如何将带有参数(闭包)的 python function 传递给另一个 function? - How to pass a python function with parameters (a closure) into another function? 如何在另一个函数中将Python pandas函数作为变量/参数传递? - How to pass a Python pandas function as a variable/parameter in another function? 将函数参数作为参数传递给python中的另一个函数 - pass a function parameter as an argument to another function in python 如何将可选参数从一个函数传递给另一个函数,另一个函数也是第一个函数的参数? - How to to pass optional parameters from one function to another function, which is also a parameter of the first function? 如何将mean()作为输入参数传递给Python中的另一个函数? - How to pass mean() as input parameter to another function in Python? 如何在python中传递带有参数的函数? - How to pass a function with parameters with a function in python? Python将带有参数的函数作为参数传递给高阶函数? - Python pass function with parameters as parameter to higher order function? 如何在Python中将函数作为函数参数传递 - How to pass a function as a function parameter in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM