5

I have different functions, but each takes different number of arguments and different arguments. I want to be able to execute only one of them and to do this based on the name of the function as string. I tried this:

def execute_function(function_name, arg1, arg2, arg3):
    return {
        'func1': func1(arg1),
        'func2': func2(arg1, arg3),
        'func3': func3(arg2),
    }[function_name]

but it executes all functions :( no matter what is the string function_name.

Do you have any ideas why this happens and how to achieve what I want. (I'm new to Python). Thank you very much in advance! :)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Faery
  • 4,552
  • 10
  • 50
  • 92
  • Add a default function also with `{ .... }.get(function_name, default)()` if suppose you wrong function name pass! `default(){ print("Exception: Undefined function called..")}` – Grijesh Chauhan Apr 17 '14 at 11:29

1 Answers1

9

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Oooh, thank you so much! I'm ashamed now, it seems so obvious why they are all called :$ Thank you very much for removing my confusion and the solutions! – Faery Apr 17 '14 at 11:24
  • Amazing! Could you please explain a bit the first one? – Lerner Zhang Apr 24 '18 at 03:17
  • @lerner: nut sure what part you are missing; each `lambda` is just a function, and each function object takes no arguments. The `arg1`, `arg2` and `arg3` values are passed into the `func1`, `func2` and `func3` calls as closures. – Martijn Pieters Apr 24 '18 at 06:56
  • @MartijnPieters Thanks very much. What confuses me most is that {..}[function_name] is the function and the () followed means its execution, then parameters should be passed through here(the last ()). – Lerner Zhang Apr 24 '18 at 08:15
  • 1
    @lerner no, the lambda takes zero arguments here. Each of the wrapped functions takes a different number of arguments, after all. – Martijn Pieters Apr 24 '18 at 08:31
  • @MartijnPieters Oh, I get it, just as you said "the lambda takes zero arguments". – Lerner Zhang Apr 24 '18 at 09:08