For example, I've got functions, funcA()
, funcB()
, funcC()
. I'd like to call each of these functions, but after each one, perform a series of other functions, like func1()
, func2()
, func3()
, func4()
.
I know that I could:
funcA()
func1()
func2()
func3()
func4()
funcB()
....
Or that I could:
def funcCombined():
func1()
func2()
func3()
func4()
funcA()
funcCombined()
funcB()
funcCombined()
...
But is there a better way? I tried putting them in a list, like:
funcs = [funcA(), funcB(), funcC()]
for func in funcs:
x = func
func1()
func2()
func3()
func4()
But it seems to be executing all of the functions in the list and then executing them in the for loop.
What's the best way to do this?
If your functions take different arguments, store them in a dictionary or a 2nd list
funcs = {
funcA: (1, 2, 3),
funcB: ({'a' : 1}, [1, 2, 3]),
funcC: None
}
for func, args in funcs.items():
if args is None:
func()
else:
func(*args)