You need to add just the functions to the dictionary; you added their result.
To support variable arguments, use either lambdas or functools.partial()
to wrap your functions instead:
def execute_function(function_name, arg1, arg2, arg3):
return {
'func1': lambda: func1(arg1),
'func2': lambda: func2(arg1, arg3),
'func3': lambda: func3(arg2),
}[function_name]()
or
from functools import partial
def execute_function(function_name, arg1, arg2, arg3):
return {
'func1': partial(func1, arg1),
'func2': partial(func2, arg1, arg3),
'func3': partial(func3, arg2),
}[function_name]()
In both cases you call the resulting object (partial
or lambda
function) after retrieving it from the dictionary.