In some of the recent articles we were talking about different aspects of argument passing in Python. Time for a drill. In this article you’ll have an opportunity to practice what you have learned about arguments. Although some of the examples are pretty artificial (it’s not common practice to pass all types of arguments into one function for example), they are just an exercise so that you can check your understanding of the topic.
Here’s the video version of this article:
If you haven’t read my articles on argument matching modes in Python, keyword-only arguments and the order of arguments, I would definitely recommend to read them first.
And now the drill. You will see some function definitions followed by function calls. Your task is to predict the output, so just stop reading before the solution part and give it a try.
Table of Contents
Function Arguments – Exercise 1
Let’s start with something simple:
>>> def func(a, b, c):
... print(a, b, c)
...
>>> func(1, c = 5, b = 8)
Solution to Exercise 1
And the output is…
1 8 5
As we know, we can use keyword arguments in any order.
Function Arguments – Exercise 2a
Fine, here’s the next one:
>>> def func(a, b = 3, c = 5):
... return a + b * c
...
>>> func(5)
Solution to Exercise 2a
What’s the output now?
20
Here b and c default to 3 and 5 respectively.
Function Arguments – Exercise 2b
Let’s call this function like so:
>>> func(5, 4)
Solution to Exercise 2b
What’s the output now?
25
Well, the default value of b was overwritten by 4 and the value of c is still the default one of 5.
Function Arguments – Exercise 3
Here’s our next example:
>>> def func(a, *b, **c):
... print(a, b, c)
...
>>> func(5, 5, x = 5)
Solution to Exercise 3
It’s slightly trickier than the previous one. Now the output is…
5 (5,) {'x': 5}
Yes, the first argument is assigned to a, all the remaining positional arguments (which in our case is just the second argument in the function call) are packed in a tuple, so the tuple contains just one element, and all the keyword arguments (which again in our case is just the single keyword argument) are packed in a dictionary.
Function Arguments – Exercise 4
Here’s another example:
>>> def func(a, b, c, d, e, f):
... print(a, b, c, d, e, f)
...
>>> func(1, e = 2, *(3, 4), **{'d': 5, 'f': 6})
Solution to Exercise 4
And the output is…
1 3 4 5 2 6
First there’s a positional argument, 1. Then we have the keyword argument e. After that the tuple is unpacked into the remaining two positional arguments. Then the dictionary is unpacked into the two keyword arguments, d and f. As we know positional arguments come before keyword arguments.
Function Arguments – Exercise 5
Now, it can only get harder. Try this final example:
>>> def func(a, *b, c, d = 7, **e):
... print(a, b, c, d, e)
...
>>> func(1, 2, 3, 4, 5, c = 6, **dict(m = 8, n = 9))
Solution to Exercise 5
And now the output is…
1 (2, 3, 4, 5) 6 7 {'m': 8, 'n': 9}
– a is assigned 1,
– 2, 3, 4 and 5 are packed into a tuple,
– c and d are keyword-only arguments: c is mandatory, d is optional, so as we didn’t use d in the function call, the default value of 7 is used,
– the dict function returns a dictionary, which is then unpacked into individual keyword arguments m and n, which are next packed into a dictionary again.
Especially the last example seems quite artificial, but hopefully you had fun trying to figure out how the arguments work. And, what’s even more important than fun is that you now understand how to pass arguments to functions.