Convert string into a function call

python

Solution

Sure, you can use `globals`:

func_to_run = globals()[ran_test_opt]
func_to_run()

Or, if it is in a different module, you can use `getattr`:

func_to_run = getattr(other_module, ran_test_opt)
func_to_run()

Problem

I have a string variable with the exact name of a function, e.g. ``` ran_test_opt = "random_aoi" ``` The function looks like this: ``` def random_aoi(): logging.info("Random AOI Test"). ``` The string is received from a config file and therefore can't be changed. Is there a way I can convert the string into an actual function call so that ``` ran_test_opt() ``` would run the `random_aoi` function?

Original source

Related problems