TFC
TFC

Reputation: 546

Python: Would it be possible to call a function specifying by string variable?

Given there is a list of function's names as strings, would it be possible to call corresponding function with random sampling from the list? Currently I am hard coding all the name by if statement so that

def a(x):
    print(x)

def b(y):
    print(y)
# there are more functions

func_list = ['a', 'b', ...] # all function's names are stored

chosen_function = random.choice(func_list)
if chosen_function == 'a':
    a(1)
elif chosen_function == 'b':
    b(2)
# elif continues...

I want to eliminates all if statements so that whenever I add new functions in func_list, I do not need to modify the if statements.

However, I can not contain the function itself in the list as the actual functions have randomness in them so I need to call it after the function is sampled.

Upvotes: 1

Views: 90

Answers (4)

Swadhikar
Swadhikar

Reputation: 2200

You may consider using eval function. In a nutshell, it evaluates a string into a Python snippet.

For example, in the below code, the eval function evaluates any as a string and interprets to python's any built-in function.

>>> eval('any')
>>> <built-in function any>

On similar grounds, you could evaluate the intended function from a string as below.

def function_a(): 
    print('function A')

def function_b(): 
    print('function B')

function_to_run = eval('function_b')    # function_to_run is a callable now
function_to_run()    

Result

function B

Upvotes: 0

Jarvis
Jarvis

Reputation: 8564

Why not use a dictionary?

def a(x):
    print(x)

def b(y):
    print(y)

func_dict = {'a': a, 'b': b}

Call it like this:

x = 3 # your int input
func_dict['a'](x) # calls a(x)
func_dict['b'](x) # calls b(x)

If you want to directly specify arguments, you can do so in the dictionary like this:

func_dict = {
    'a': lambda: a(1),
    'b': lambda: b(2)
}

Call the default methods like:

func_dict['a']() # calls a(1)
func_dict['b']() # calls b(2)

Upvotes: 3

Chayim Friedman
Chayim Friedman

Reputation: 70850

The answer of @Jarvis is the right way to go, but for completeness sake, I'll show a different way: inspecting the global variables.

def a(x):
    print(x)

def b(y):
    print(y)
# there are more functions

func_list = ['a', 'b', ...] # all function's names are stored

chosen_function = random.choice(func_list)

globals()[chosen_function](x)

Upvotes: 3

sammy
sammy

Reputation: 437

you can set the default value in the function itself

def a(x=1):
    print(x)

def b(y=2):
    print(y)


chosen_function = random.choice(func_list)

chosen_function()

Upvotes: -1

Related Questions