I have a Python function which requires a number of parameters, one of which is the type of simulation to perform. For example, the options could be \"solar\", \"view\" or \"bot
Since functions are objects in python, you could actually process *args as a list of methods and pass the types of simulations as arbitratry args at the end. This would have the benefit of allowing you to define new simulations in the future without having to refactor this code.
def func(a, b, c, *args):
for arg in args:
arg(a, b, c)
def foosim(a, b, c):
print 'foosim %d' % (a + b + c)
def barsim(a, b, c):
print 'barsim %d' % (a * b * c)
Use:
func(2, 2, 3, foosim)
func(2, 2, 3, barsim)
func(2, 2, 3, foosim, barsim)
Output:
foosim 7
barsim 12
foosim 7
barsim 12