0

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)

Sam Mason
  • 15,216
  • 1
  • 41
  • 60

2 Answers2

0

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]

divyashie
  • 9
  • 3
  • Thank you, but I don't want to call the functions at the moment. I just want to memorize them in a list like in the example : https://pythonexamples.org/python-list-of-functions/ – Rustyhubob Sep 20 '22 at 10:57
0

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.

Sam Mason
  • 15,216
  • 1
  • 41
  • 60