If you want to call all your functions in an automatic way, specifically, all at once, then it isn't important that you be able to call them by some specific name like it was in the question you are referencing. You can just keep the functions in a list and call each one in turn.
As for dealing with a variable number of arguments, Python has a "splat" operator you may have seen in some method declarations: def __init__(self, *args, **kwargs)
. This operator can be used to unpack argument lists. (See this SO answer for more info)
If you store the parameters in another list you can iterate through your list of functions and apply the parameters to its corresponding function one-by-one using the same syntax and without specifying the number of arguments:
funclist = [lambda x,y: x+y, lambda x,y,z: x+y+z, lambda x: -x]
paramlist = [(1,2), (1,2,3), (1,)]
for index, func in enumerate(funclist):
print func(*paramlist[index])
How you link the functions to their default parameters is another matter: you could keep them in two lists, or together as tuples in another list or dict, or define a lightweight class to hold them...it depends on your problem. Anyways, it sounds like the important part for you is the *
notation.
Edit:
If you want to pass in keyword arguments to the function, you can use the double-* notation to pass a dictionary and have it expanded into the keyword arguments that are required:
def func1(n_samples, noise, factor, random_state):
print locals()
def func2(n_samples, ratio, noise):
print locals()
def func3(my_key, default_key='world!'):
print locals()
funclist = [func1, func2, func3]
paramlist = [
dict(
n_samples=1000,
noise=0.3,
factor=0.5,
random_state=1
),
dict(
n_samples=1000,
ratio=0.5,
noise=0.1
),
dict(
my_key='hello',
)
]
for index, func in enumerate(funclist):
func(**paramlist[index])