Accepting Any Number of args
00:00
Functions Accepting Any Number of Arguments. Before defining a function that accepts any number of arguments, you’ll need to be familiar with the unpacking operator (*
).
00:12
A good place to experiment with new concepts such as these is the Python shell, which is accessed from your command line or terminal by typing python
or python3
.
00:24 Note that the examples in this course have shown running in the Bpython shell, which also has some improved functionality, such as syntax color-coding. Everything you see on-screen will run in the standard Python shell, but Bpython makes seeing the syntax easier.
00:41
You can start with a list such as the one seen on-screen. The variable some_items
points to a list, and the list in turn has four items within it.
00:52
If you use some_items
as an argument to print()
, then you’re passing one variable to print()
. print()
displays the list as you would expect. However, if you use *some_items
within the parentheses of print()
, you get a different outcome.
01:10
This time, print()
displays the four separate strings, rather than the list. This is equivalent to writing the code seen on-screen.
01:23
When the asterisk (*
) is used immediately before a sequence, such as some_items
, it unpacks the sequence into its individual components.
01:33 When a sequence such as a list is unpacked, its items are extracted and treated as individual objects.
01:42
You may have noticed that print()
can take any number of arguments. You’ve used it with one and four input arguments in the examples just seen.
01:50
You can also use print()
with empty parentheses, and it will print a blank line. You’re now ready to define your own functions that can accept a variable number of input arguments. For the time being, you can simplify add_items()
to accept only the names of the items you want in the shopping list.
02:09
You’ll then set the quantity to 1
for each item. You’ll get back to including the quantities as part of the input arguments later on in the course. The function signature that includes the variable number of input arguments using args
looks like this.
02:25
You’ll often see function signatures that use the name args
to represent this type of optional argument. However, this is just a parameter name.
02:33
There is nothing special about the name args
. It’s the preceding star (*
) that gives this parameter its particular properties, which you’ll see next.
02:43 Often it’s better to use a parameter name that suits your needs best to make the code more readable, as you’ll see on-screen.
03:21
The first argument when calling add_items()
is a required argument. Following the first argument, the function can accept any number of additional arguments. In this case, you’ve added four additional arguments when calling the function. On-screen, you can see the output of this code.
03:41
You can understand what’s happening with the item_names
parameter by looking at a simplified example in the REPL.
04:05
When you display the data type, you can see that item_names
is a tuple. Therefore, all the additional arguments are assigned as items in the tuple item_names
.
04:15
This tuple can then be used in the function definition, as it was in add_item()
seen previously, where you iterated through the item names using a for
loop.
04:25
This is not the same as passing a tuple as an argument in the function call. Using *args
allows you to use the function more flexibly, as you can add as many arguments as you wish without the need to place them in a tuple in the function call. If you don’t add any additional arguments when you call the function, then the tuple will be empty, as seen on-screen.
04:49
When you add args
to a function definition, you’ll usually add them after the required and optional parameters. You can have keyword-only arguments that follow the args
. But for this course, you can assume that args
will usually be added after all the other arguments, except for kwargs
, which you’ll see next.
Become a Member to join the conversation.