13

What I need is something that gets the variable and if the variable is the name of a function, the function will be called. I am not trying to get a function from a module but from within the program itself

Example:

def foo():
    print("Foo")
callfunction = input("What function do you want to call? ")

callfunction() ## Not sure what to put here but but lets say callfunction is foo.

I don't want something like this because I have many functions:

if callfunction == "Foo"
    Foo()
else:
    print("No function with that name")

My question is something like this, but I ask for the Python syntax. I am using Python 3.5.1

1

5 Answers 5

24

Use a dict mapping names to functions.

call_dict = {
    'foo': foo,
    'bar': bar
}
call_dict[callfunction]()
Sign up to request clarification or add additional context in comments.

1 Comment

@drascom What do you mean? That code looks fine. Are the functions actually methods? Then it might get tricky.
7

You can do this :

eval(input("What function do you want to call? ") + '()')

2 Comments

This allows the persons using it to execute arbitrary python code (just so long as whatever they do ends with a callable). For example they might just choose to erase all your files.
@KalkidanTadesseZewdieBeyene: At the first glance this might look like the easiest solution but it's a very very very dangerous solution. Read Eval really is dangerous.
5

It is quite common in Python to use the command pattern. First move all of your functions into a class, and give them names which have a prefix that isn't used in the input. Then use getattr() to find the correct function and call it.

class Commands():
   def cmd_foo(self):
       print("Foo")

   def callFunction(self, name):
       fn = getattr(self, 'cmd_'+name, None)
       if fn is not None:
            fn()

This has a couple of advantages over Daniel's call_dict: you don't have to list the name of the functions a second time, and you don't have to list the callable functions a second time either.

The 'cmd_' prefix is there to ensure you can have other methods in the class but still control exactly which ones are directly callable.

Comments

2

This should do it

import inspect
import your_module

list_of_functions = inspect.getmembers(your_module, inspect.isfunction)

Now list_of_functions is a list of tuples with the name of each function at position 0 and the function itself at position 1.

Comments

1

Very late to the game here, but if I understand the objective correctly I think you can use locals(), assuming that the functions you want to call are already imported or defined in the same module.

def foo():
    print("foo has been called")

call_function_name = input("What function do you want to call? ")

function_to_call = locals()[call_function_name] # can also use locals().get(call_function_name) if you want to be a bit more defensive
function_to_call()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.