Skip to content Skip to sidebar Skip to footer

Calling Functions With Parameters Using A Dictionary In Python

I'm making a program which has a main menu that asks the user to input an option and store it in integer option1, which is looked up in dictionary options. The corresponding functi

Solution 1:

I would do this using functools.partial to specify the arguments when the dictionary is created:

fromfunctoolsimportpartialoptions= {0:FunctionZero,   
           1:FunctionOne,    
           2:FunctionTwo,
           3:partial(FunctionThree, True)} 

Note that this also allows additional parameters to be passed when the function is called (as long as all the functions in the dictionary have the same parameters missing after partial has been called):

deftest(one, two, three=None, four=None):
    ...

deftest2(one, two, three=None):
    ...

options = {1: partial(test, 1, three=3, four=4),
           2: partial(test2, 1, three=3)}

...

options[choice](2) # pass the 'two' argument both functions still require

Solution 2:

Sure. In Python, functions can take positional or keyword arguments. For most functions, arguments can be passed in either way, but that’s not necessarily the case for all functions, so we do need to keep them separate. Positional arguments are in an iterable (often list or tuple), and keyword arguments are in a dictionary from strings to values.

We could then represent each function as a tuple of function, positional arguments, and keyword arguments:

options= {
    0:(function_zero, [], {}),
    1:(function_one, [], {}),
    2:(function_two, [], {}),
    3:(function_three, [True], {}),
    4:(function_four, [], {'kwarg':True}),  # takes a keyword argument
}

Then you could call them like this:

func, args, kwargs = options[option1]func(*args, **kwargs)

But if you’re always going to just pass in a constant, there’s a better way: just create little no-argument wrappers for each function that call the function how you want it to be called:

options= {
    0:function_zero,
    1:function_one,
    2:function_two,
    3: lambda:function_three(True),
    4: lambda:function_four(kwarg=True),
}

Then use your first method:

options[option1]()

As detailed in jonrsharpe’s answer, you can also use functools.partial rather than a lambda. As he notes, this has the advantage of being able to append some of your own arguments:

options[option1]('hello')  # adds 'hello' to previously-specified arguments

If you don’t need this functionality, though, a zero-parameter lambda will serve you just fine.

Solution 3:

To add to icktoofay's answer, if you want to pass an argument to the lambda just do the following:

def printDouble( number ):
    print number * 2

options = {
    1: lambda num: printDouble(num)
}

options[1](4)    #this prints 8

By adding the parameter for lambda before the ":" you state that the lambda receives a parameter and it is used then in the function it calls.

Also if you don't want to use lambdas you can use the usual way

defprintDouble( num ):
    print num * 2defprintHalf( num ):
    print half / 2

functionDictionary = {
    'Double': printDouble,
    'Half'  : printHalf
}

functionDictionary['Double'](2)    #This prints 4

Post a Comment for "Calling Functions With Parameters Using A Dictionary In Python"