I'm trying to wrap my head around using args
and kwargs
in Python 3 (Python 3.7.0) but I'm running into some issues with my understanding.
Here is a simple function I have:
def add_args(y=10, *args, **kwargs):
return y, args, kwargs
And test it to see what is returned:
print(add_args(1, 5, 10, 20, 50))
print(add_args())
>>
(1, (5, 10, 20, 50), {}) # What happened to my default y=10?
(10, (), {})
What I don't understand is, what happened to y=10
in the first print statement? I can see it is being overridden by the 1
in args
but I'm unsure why.
How can I rewrite this function so the default value is not overridden, or am I missing something with how the parameters are passed from the function signature to the return statement?
I tried looking here and here but did not find the answers I was looking for. As I thought putting the default values before the args
and kwargs
would prevent the overwriting.