In python I have many functions likes the ones below. I would like to run all the functions whose name matches setup_*
without having to explicitly call them from main. The order in which the functions are run is not important. How can I do this in python?
def setup_1():
....
def setup_2():
....
def setup_3():
...
...
if __name__ == '__main__':
setup_*()
You can use
locals()
Of course you'll want to make sure that your function names don't appear elsewhere in locals.
In addition, you could also do something like this because functions are first class objects (note the function names are not strings):
yields
Here is one possible solution:
This does not get function objects directly but must use eval, I am checking solution with vars() to get rid of eval:
Ok, here the version with vars():