The method of obtaining a different number of arguments and the ability to choose the right one is to use the * args and ** keyword_args parameters. From Mark Lutz's Learning Python book:
* and ** are intended to support functions that take any number of arguments. Both can be displayed either in a function definition or in a function call, and they have related goals in two places.
* and ** in function definition
If you define a function:
def f1(param1, *argparams, **kwparams): print 'fixed_params -> ', param1 print 'argparams --> ', argparams print 'kwparams ---->,', kwparams
you can call it like this:
f1('a', 'b', 'c', 'd', kw1='keyw1', kw2='keyw2')
Then you will get:
fixed_params -> a argparams --> ('b', 'c', 'd') kwparams ---->, {'kw1': 'keyw1', 'kw2': 'keyw2'}
So you can send / receive any number of parameters and keywords. One typical idiom for recovering args keywords is as follows:
def f1(param1, **kwparams): my_kw1 = kwparams['kw1'] ---- operate with my_kw1 ------
Thus, your function can be called with any number of parameters, and it uses those that it needs.
This type or arguments are often used in some graphical interface, such as the wxPython class and subclass definition, as well as for currying functions, decorators, etc.
* and ** in function call
* and ** params in the function call are unpacked when the function is executed:
def func(a, b, c, d): print(a, b, c, d) args = (2, 3) kwargs = {'d': 4} func(1, *args, **kwargs)
Excellent!