From something like:
strings = ["f1", "f2"]
I would like to obtain a list of functions to be called later, like:
functions = [f1, f2]
f1
an f2
are the names of 2 methods defined in a python script (not included in a class)
CodePudding user response:
I think you can have something like:
def f1():
a =1
return a
def f2():
b = 2
return b
l = [f1(), f2()]
will return you: [1, 2]
CodePudding user response:
given:
def f1(): pass
def f2(): pass
you could just look them up in the globals
dictionary:
available = globals()
function_names = ["f1", "f2"]
functions = [available[name] for name in function_names]
but note that this is somewhat unsafe, as there's nothing to stop function_names
referring to functions you're not expecting.