Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c. You would have to add some logic to map args->c,d,e,f but its a "way" of overloading.
Copydef myfunc(a,b, *args, **kwargs):
for ar in args:
print ar
myfunc(a,b,c,d,e,f)
And it will print values of c,d,e,f
Similarly you could use the kwargs argument and then you could name your parameters.
Copydef myfunc(a,b, *args, **kwargs):
c = kwargs.get('c', None)
d = kwargs.get('d', None)
#etc
myfunc(a,b, c='nick', d='dog', ...)
And then kwargs would have a dictionary of all the parameters that are key valued after a,b
Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c. You would have to add some logic to map args->c,d,e,f but its a "way" of overloading.
Copydef myfunc(a,b, *args, **kwargs):
for ar in args:
print ar
myfunc(a,b,c,d,e,f)
And it will print values of c,d,e,f
Similarly you could use the kwargs argument and then you could name your parameters.
Copydef myfunc(a,b, *args, **kwargs):
c = kwargs.get('c', None)
d = kwargs.get('d', None)
#etc
myfunc(a,b, c='nick', d='dog', ...)
And then kwargs would have a dictionary of all the parameters that are key valued after a,b
Try calling it like: obj.some_function( '1', 2, '3', g="foo", h="bar" ). After the required positional arguments, you can specify specific optional arguments by name.
Videos
I am working on a project where I'm supposed to add new features to an existing codebase. As part of this, I need to add an optional argument to one of the functions but just adding the optional argument is causing some of my unit tests to fail.
The function looks like the following initially:
def function(data1: list,
data2: list,
opt1: Optional[list],
) After adding another optional argument it looks like this:
def function(
data1: list,
data2: list,
opt1: Optional[list],
new: Optional[dict],
)The only change I'm making in the codebase is adding this optional argument and it is causing some of my unit tests to fail. I was wondering if someone knows what might be the reason ?
The mapping of the arguments can presumably be determined based simply on the number of arguments you pass. For example, let's say you had the following, with the "middle" argument being optional/default:
def format_name(first, middle='', last):
return first + ' ' + middle + ' ' + lastIf you passed
format_name('John', 'Smith')the interpreter should be able to deduce that the second argument is referring to the parameter *last*, not the optional parameter *middle*, since there's only two arguments passed. Is this not allowed simply because of the overhead that would be required in implementing this consideration?
Many thanks for your help!