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.