David542 David542 - 3 months ago 6
Python Question

Call imported function in python using string

I have a list of the names of imported functions, which I can call as follows:

from myfile import function1
from myfile import function2

function1()
function2()


How would I call the functions from a list of names? For example:

fns = ['function1', 'function2']
for fn in fns:
fn()


How would I do the above properly?

Answer

don't use a list of strings, just store the functions in the list:

fns = [function1, function2]
for fn in fns:
    fn()
Comments