*args and **kwargs
There a few things to note:
kwargsare used by convention, they are not a part of the language specification. Thus, these are equivalent:
You may not have more than one
argsor more than one
kwargsparameters (however they are not required)
If any positional argument follow
*args, they are keyword-only arguments that can only be passed by name. A single star may be used instead of
*argsto force values to be keyword arguments without providing a variadic parameter list. Keyword-only parameter lists are only available in Python 3.
**kwargsmust come last in the parameter list.
**kwargs and default values
To use default values with **kwargs
Keyword-only and Keyword-required arguments
Python 3 allows you to define function arguments which can only be assigned by keyword, even without default values. This is done by using star * to consume additional positional parameters without setting the keyword parameters. All arguments after the * are keyword-only (i.e. non-positional) arguments. Note that if keyword-only arguments aren't given a default, they are still required when calling the function.
Populating kwarg values with a dictionary
Using **kwargs when calling functions
You can use a dictionary to assign values to the function's parameters; using parameters name as keys in the dictionary and the value of these arguments bound to each key:
Using **kwargs when writing functions
You can define a function that takes an arbitrary number of keyword (named) arguments by using the double star
** before a parameter name:
When calling the method, Python will construct a dictionary of all keyword arguments and make it available in the function body:
Note that the **kwargs parameter in the function definition must always be the last parameter, and it will only match the arguments that were passed in after the previous ones.
Inside the function body,
kwargs is manipulated in the same way as a dictionary; in order to access individual elements in
kwargs you just loop through them as you would with a normal dictionary:
print_kwargs(a="two", b=1) shows the following output:
Using *args when calling functions
A common use case for
*args in a function definition is to delegate processing to either a wrapped or inherited function. A typical example might be in a class's
a parameter is processed by the child class after all other arguments (positional and keyword) are passed onto - and processed by - the base class.
What happens here is the class
__init__ function sees the arguments
1, 2, 3. It knows it needs to take one positional argument (
a), so it grabs the first argument passed in (
1), so in the scope of the function
a == 1.
Next, it sees that it needs to take an arbitrary number of positional arguments (
*args) so it takes the rest of the positional arguments passed in (
1, 2) and stuffs them into
*args. Now (in the scope of the function)
args == [2, 3].
Then, it calls class
__init__ function with
*args. Python sees the
* in front of args and "unpacks" the list into arguments. In this example, when class
__init__ function calls class
__init__ function, it will be passed the arguments
2, 3 (i.e.
Finally, it sets its own
x property to the first positional argument
a, which equals
Using *args when calling functions
The effect of using the
* operator on an argument when calling a function is that of unpacking the list or a tuple argument
Note that the length of the starred argument need to be equal to the number of the function's arguments.
A common python idiom is to use the unpacking operator
* with the
zip function to reverse its effects:
Using *args when writing functions
You can use the star * when writing a function to collect all positional (ie. unnamed) arguments in a tuple:
In that call, farg will be assigned as always, and the two others will be fed into the args tuple, in the order they were received.