Path: blob/master/03-Methods and Functions/07-args and kwargs.ipynb
666 views
*args
and **kwargs
Work with Python long enough, and eventually you will encounter *args
and **kwargs
. These strange terms show up as parameters in function definitions. What do they do? Let's review a simple function:
This function returns 5% of the sum of a and b. In this example, a and b are positional arguments; that is, 40 is assigned to a because it is the first argument, and 60 to b. Notice also that to work with multiple positional arguments in the sum()
function we had to pass them in as a tuple.
What if we want to work with more than two numbers? One way would be to assign a lot of parameters, and give each one a default value.
Obviously this is not a very efficient solution, and that's where *args
comes in.
*args
When a function parameter starts with an asterisk, it allows for an arbitrary number of arguments, and the function takes them in as a tuple of values. Rewriting the above function:
Notice how passing the keyword "args" into the sum()
function did the same thing as a tuple of arguments.
It is worth noting that the word "args" is itself arbitrary - any word will do so long as it's preceded by an asterisk. To demonstrate this:
**kwargs
Similarly, Python offers a way to handle arbitrary numbers of keyworded arguments. Instead of creating a tuple of values, **kwargs
builds a dictionary of key/value pairs. For example:
*args
and **kwargs
combined
You can pass *args
and **kwargs
into the same function, but *args
have to appear before **kwargs
Placing keyworded arguments ahead of positional arguments raises an exception:
File "<ipython-input-8-fc6ff65addcc>", line 1
myfunc(fruit='cherries',juice='orange','eggs','spam')
^
SyntaxError: positional argument follows keyword argument
As with "args", you can use any name you'd like for keyworded arguments - "kwargs" is just a popular convention.
That's it! Now you should understand how *args
and **kwargs
provide the flexibilty to work with arbitrary numbers of arguments!