简体   繁体   English

使用“隐式”参数传递 function

[英]Passing a function with an “implicit” argument

I'm using a class from a package in Python to which I pass functions that must have only one argument , eg :我在 Python 中使用 package 中的 class ,我将必须只有一个参数的函数传递给它,例如:

def exampleFunction1(only_argument_1):
    
    doSomething1(only_argument_1) #perform some operations

Then, once defined all the needed functions in the above way, I need to pass them in this way:然后,一旦以上述方式定义了所有需要的函数,我需要以这种方式传递它们:

python_package_class.function1 = exampleFunction1
python_package_class.function2 = exampleFunction2
python_package_class.function3 = exampleFunction3

Now let's imagine that the doSomething is equal for all my functions, except for the fact that it requires another parameter that change, eg (1, 2 and 3 are just examples, the argument can be anything):现在让我们假设 doSomething 对于我的所有函数都是相等的,除了它需要另一个改变的参数,例如(1、2 和 3 只是示例,参数可以是任何东西):

def exampleFunction1(only_argument_1):
    
    doSomething(1, only_argument_1) #perform some operations

def exampleFunction2(only_argument_2):
    
    doSomething(2, only_argument_2) #perform some operations

def exampleFunction3(only_argument_3):
    
    doSomething(3, only_argument_3) #perform some operations

This could be greatly simplified by defining only one exampleFunction to which I pass two parameters :这可以通过只定义一个我传递两个参数exampleFunction来大大简化:

def exampleFunction(a, only_argument_1):
    
    doSomething(a, only_argument_1) #perform some operations

But, unfortunately, this would give me an error since, as I said, the Python package that I'm using strictly requires functions accepting only one argument.但是,不幸的是,这会给我一个错误,因为正如我所说,我正在使用的 Python package 严格要求函数只接受一个参数。

So my question is, is there an "implicit way" to pass the argument a to exampleFunction in the following code line?所以我的问题是,在下面的代码行中是否有一种“隐式方式”将参数a传递给 exampleFunction?

python_package_class.function1 = exampleFunction

You could pass your parameters in a tuple which is one argument but would contain inside it any number of elements.您可以在一个元组中传递参数,该元组是一个参数,但其中包含任意数量的元素。 you can then pass this to your second function expanding it into its individual parameters.然后,您可以将其传递给您的第二个 function 将其扩展为单独的参数。 Example:例子:

def example_function(arg_list):
    other_function(*arg_list)


def other_function(arg_one, arg_two):
    print(f"received args: '{arg_one}', '{arg_two}'")


args = (1, "some_arg")
example_function(args)
args2 = (2, "some_arg")
example_function(args2)

OUTPUT OUTPUT

received args: '1', 'some_arg'
received args: '2', 'some_arg'

You should use functools.partial :您应该使用functools.partial

from functools import partial

def exampleFunction(a, only_argument_1):
    doSomething(a, only_argument_1)

python_package_class.function1 = partial(exampleFunction, 1)
python_package_class.function2 = partial(exampleFunction, 2)
python_package_class.function3 = partial(exampleFunction, 3)

The partial function is used for partial function application which “freezes” some portion of a function's arguments and/or keywords resulting in a new object with a simplified signature. The partial function is used for partial function application which “freezes” some portion of a function's arguments and/or keywords resulting in a new object with a simplified signature.

Here is an example:这是一个例子:

from functools import partial

def f(arg1, arg2):
    print(f'{arg1} {arg2}')

f1 = partial(f, 1)
f2 = partial(f, 2)
f3 = partial(f, 3)

print(f1('a')) # '1 a'
print(f2('a')) # '2 a'
print(f3('b')) # '3 b'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM