简体   繁体   中英

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 :

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):

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 :

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.

So my question is, is there an "implicit way" to pass the argument a to exampleFunction in the following code line?

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. 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

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

You should use 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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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