Accepting Any Number of args
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 (
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
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.
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.
If you use
some_items as an argument to
print(), then you’re passing one variable to
print() displays the list as you would expect. However, if you use
*some_items within the parentheses of
print(), you get a different outcome.
print() displays the four separate strings, rather than the list. This is equivalent to writing the code seen on-screen.
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.
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.
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.
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.
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.
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.
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.
You can understand what’s happening with the
item_names parameter by looking at a simplified example in the REPL.
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
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
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.
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.