Get startedGet started for free

How to pass a variable number of arguments to a function?

1. How to pass a variable number of arguments to a function?

Good job working with iterable objects! In this chapter we'll go through common questions on Python functions. We'll start with the problem of passing a variable number of arguments to a function.

2. Argument types

Functions can have the following argument types:

3. Argument types

positional arguments

4. Argument types

and keyword arguments.

5. Argument types

Let's start with the first type.

6. Positional arguments

Positional arguments are just simple arguments we pass to a function. For example, let's define a function that multiplies two numbers and returns the result. Here is the output of the call.

7. *args

Actually, we can have a variable number of positional arguments in our function definition. We can do this by using a name prefixed with an asterisk as an argument. In this case, we can call our function with as many positional arguments as we want.

8. *args

Let's see what happens if our function body will simply print our input. The arguments are interpreted as a tuple!

9. *args

It means we can iterate through it. Calling the function will print each single item.

10. Redefining multiply()

Let's redefine our multiply() function now. Calling it with a variable number of arguments works! Notice that the name args is just a convention: we can use any name we like.

11. Redefining multiply()

Substituting args with nums won't change anything.

12. Another use of single asterisk *

The single asterisk also has another use. Assume we have a function with a predefined number of positional arguments. Instead of precisely defining each argument in the call,

13. Another use of single asterisk *

we can first create an iterable object, for example a tuple. Then, we pass the tuple prefixed with an asterisk to the function call. It unwraps into the corresponding arguments. We can also define only part of the necessary arguments, and add some more in the function call.

14. Another use of single asterisk *

We can actually do the unwrapping with a variable number of arguments as well. We define our Iterable and call the function with it prefixed with an asterisk.

15. Argument types

Let's get to the keyword arguments!

16. Keyword arguments

Keyword arguments are positional arguments having default values. For example, let's define a function. We can call it as usual. We can also skip the arguments forcing the function to use its default values.

17. Keyword arguments

Or we can precisely specify the names in any sequence we want. It is also possible to use a variable amount of keyword arguments.

18. **kwargs

This is achieved by using a name prefixed with a double asterisk as an argument. If we define the following function and call it with a variable number of named arguments, the output will represent a dictionary. Passing an argument without a name will raise an error.

19. Redefining multiply()

Let's redefine our multiply() function with the **kwargs argument. The difference to our previous implementation is that we iterate through the key-value pairs instead of simple values.

20. Calling multiply_kwargs()

Let's check how our function works! Here is the output we get.

21. Another use of double asterisk **

As with the example on single asterisk, double asterisk can have another meaning. Assume, we have a function with a predefined number of keyword arguments. By default it returns this.

22. Another use of double asterisk **

Let's define the following dictionary with the key names matching the argument names in our function. Then, let's pass it prefixed with double asterisk to the function call.

23. Another use of double asterisk **

We can have a dictionary defining only the part of the arguments. In this case the output looks like this. The argument that was not defined in the dictionary takes its default value.

24. Another use of double asterisk **

Inserting other keys in the dictionary will result in TypeError.

25. Another use of double asterisk **

Unwrapping works if a function has a variable number of arguments. We just define our dictionary and pass it to the function prefixed with double asterisk.

26. Argument order

A function can combine different types of arguments. But the order should be followed.

27. Argument order

First, positional arguments.

28. Argument order

Then, their extension.

29. Argument order

Then, keyword arguments.

30. Argument order

And their extension. Of course we can skip some arguments if they are not needed. However, it is better to follow the order. Breaking this rule can cause unexpected behavior and errors.

31. Let's practice!

Let's practice our skills on using different argument types.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.