-1

I have a lot of possible functions I can call, however I don't always need to call every function.

Is there a way I can pass a list of functions I would like to call, then dynamically call them?

In other words:

def GetDate():
    ..does stuff..
    return Date

def GetTime():
    ..does stuff..
    return Time

def GetLocation():
    ..does stuff..
    return Location


List_of_functions_to_call = ["GetDate","GetTime"]

def Call_functions(List = List_of_Functions_to_call):
    for i in List:
        ...????...
        ..# Function would then call GetDate() and GetTime()

1 Answers1

0

You could do something like this:

def main_function(action):
    return { '1': func_a, '2': func_b, '3': func_c, '4': func_d}.get(action)()

def func_a():
    return "a"
def func_b():
    return "b"
def func_c():
    return "c"
def func_d():
    return "d"

if __name__=="__main__":
   print(main_function('1'))

This code snippet print a as expected.

lmln
  • 137
  • 1
  • 10