The most widely known usage of the asterisk operator, also known as destructuring operator, in Python is its usage in functions. Show
Let’s say you have a function that can add values and returns the sum of these values: What if you want to sum up an arbitrary number of values? You can add an asterisk in front of the argument’s name like so:
As you might have already noticed from the function’s body, we expect 0 to be an iterable. Indeed, if you check the type, 0 is a 2. And, indeed, we can now call the function with an arbitrary number of arguments:def add(number_1, number_2, number_3): return number_1 + number_2 + number_36 in the function callWe have seen so far that we can define a function so that it takes a list of parameters. But what if we have a function with a fixed set of arguments and we want to pass a list of values to it. Consider this function:
This function takes exactly 3 parameters. Let’s assume we have a list with exactly three elements. Of course, we could call our function like this:
Luckily the destructuring operator ( 6) works in both ways. We have already seen the usage in a function’s definition, but we can use it also to call a function:
def add(number_1, number_2, number_3): return number_1 + number_2 + number_38 in function definitionIn the same way we can destructure lists with the asterisk ( 6) operator, we can use the double-asterisk ( 7) operator to destructure dictionaries in Python functions.Consider a function like this:
If we call it with keyword arguments ( 8), a function call might look like this:
In that case we could rewrite our function like this to accept an arbitrary number of keyword arguments that are then represented as a dictionary called 8:
Of course, we can use the 8 dictionary like any other dictionary in Python, so that the function might become a bit cleaner if we actually utilize the dictionary data structure like so:
my_list = [1, 2, 3] add(my_list[0], my_list[1], my_list[2])7 in the function callOf course, the 7 operator works for calling a function as well:
Restricting how functions are calledKeyword arguments onlyOne of the most surprising features of the asterisk in function definitions is that it can be used standalone, i.e. without a variable (parameter) name. That being said, this is a perfectly valid function definition in Python:
But what does the standalone asterisk do in that case? The asterisk catches all (non-keyword) arguments in a list as we have seen above. There is no variable name in our case that would make the list. After the 6 we have a variable called 4. Since the 6 has already matched all positional arguments, we’re left with 4, which must be used as a keyword argument.Calling the above function with 7 will raise an error: 0Positional arguments onlyWhat if we want to force the users of our function to use positional arguments only – as opposed to keyword arguments only in the last example? Well, there is a very Pythonic way. We interpret the 8 sign (the opposite of multiplication) to do the trick: 1Surprisingly few Python developers know about this trick which has been introduced to Python 3.8 through PEP 570. If you call the last function with 9, this will raise a TypeError: 2Usage of def add(number_1, number_2, number_3): return number_1 + number_2 + number_36 and my_list = [1, 2, 3] add(my_list[0], my_list[1], my_list[2])7 in literalsThe asterisk ( 6) and double asterisk ( 7) operators do not only work for function definitions and calls, but they can be used to construct lists and dictionaries.Constructing listsLet’s say, we have two lists and want to merge them. 3Of course, we could merge these lists with the 4 operator: 4However, the 6 operator gives us a bit more flexibility. Let’s say, we want to include a scalar value in the middle, we could use:Constructing dictionariesAgain, what is true for lists and the asterisk ( 6) operator is true for dictionaries and the double-asterisk operator ( 7).Destructuring listsYou might already know, that you can split elements of a list to multiple variables like so: But did you know that you can use the asterisks ( 6) to assign to variables when you have a list of arbitrary length?Say, you want the first element and the last one of a list in a specific variable. You can do this by using the asterisk like so: 5In this example 9 now contains the first element, and 0 contains the last element of 1. What is in 2, however?In 2, there is the entire list, excluding the first and last element, i.e. 4. Note that 2 is a list now. |