Skip to content Skip to sidebar Skip to footer

How To Return Default Values With *args, And **kwargs In Function Signature

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

Solution 1:

*args only captures any positional arguments not otherwise defined; y=10 does not mean y can't be used as a positional argument. So y is assigned the first positional argument.

You can prevent y being used as a positional argument by making it a keyword-only argument. You do this by placing the argument after the *args var-positional catch-all parameter, or if you don't have a *name parameter, after a * single asterisk:

defadd_args(*args, y=10, **kwargs):
    return y, args, kwargs

or

defkeyword_only_args(*, y=10, **kwargs):
    return y, kwargs

Now y won't capture positional arguments any more:

>>>defadd_args(*args, y=10, **kwargs):...return y, args, kwargs...>>>add_args(1, 5, 10, 20, 50)
(10, (1, 5, 10, 20, 50), {})          # y is still 10
>>>add_args(1, 5, 10, 20, 50, y=42)  # setting y explicitly 
(42, (1, 5, 10, 20, 50), {})

You don't have to have a **kwargs keyword catch-all either:

defadd_args(*args, y=10):
    return y, args

but if it is present, it needs to be listed last.

Keyword-only arguments do not have to have a default value, the =10 can be omitted, but then the parameter becomes mandatory, and can only be specified in a call by using y=value:

>>>defadd_args(*args, y):  # mandatory keyword-only argument...return y, args...>>>add_args(1, 5, 10, 20, 50)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: add_args() missing 1 required keyword-only argument: 'y'
>>>add_args(1, 5, 10, 20, 50, y=42)
(42, (1, 5, 10, 20, 50))

Solution 2:

Hmm, y is the first positional argument in your function definition, so naturally it binds to the first actual positional argument at callsite, which is 1.

Post a Comment for "How To Return Default Values With *args, And **kwargs In Function Signature"