Now that we have seen the basics of working with positional argument unpacking let’s examine how to use it in a more meaningful way.
Say we wanted to build a function that works similarly to our trusty
print() statement but instead prints all of the arguments in uppercase form. Using our knowledge of iteration, combined with the power of the unpacking operator, our solution might look like this:
def shout_strings(*args): for argument in args: print(argument.upper()) shout_strings('Working on', 'learning', 'argument unpacking!')
WORKING ON LEARNING ARGUMENT UNPACKING!
In our function
shout_strings(), we use a
for loop (although we could use any iterator) to iterate through each element of the tuple that exists inside of
args. Then we use the built-in function
.upper() to capitalize each argument.
The unpacking operator is not limited to being used alone, but rather it can be combined with other positional arguments. Let’s examine a function that truncates (cuts off) sentences based on a provided length:
def truncate_sentences(length, *sentences): for sentence in sentences: print(sentence[:length]) truncate_sentences(8, "What's going on here", "Looks like we've been cut off")
What's g Looks li
Let’s break this down:
We have two parameters that our function
truncate_sentences()defines. The first is a
lengthparameter that will specify how many characters we want to keep. The second is a parameter called
sentencesthat is paired with the unpacking operator, signifying it will take a variable number of arguments.
On each iteration of the function, we are looping through the tuple created by the
sentencesargument (because it is paired with the unpacking operator) and performing a slice on the sentence based on the provided
lengthargument. This forces every value in the
sentencestuple to be cut down in length.
Utilizing iteration and other positional arguments are two common ways we can increase the utility of our functions when using the unpacking operator (*). Let’s practice using these techniques to see how powerful they are!
Jiho is having a lot of success with our restaurant application. Unfortunately, our original design did not account for storing orders for each specific table. Jiho asked us to adjust our application to be able to store the orders that come in for each specific table and also be able to print out the order for the kitchen staff.
Take some time to review the adjusted structure of the program we created earlier. Note that
tables is now dictionary with the table numbers as the keys. It also accounts for a new property called
assign_table function has also been adjusted to account for the changes.
Run the code to move onto the next checkpoint.
To help Jiho implement the ability to store the order in a specific table, let’s define a function called
The function should have two parameters called
order_items. The parameter of
order_items should be grouped with an unpacking operator (*) so we can capture a variable number of order items per table.
For now, our program will error out if we run it. Don’t worry we will fill in the function in the next step!
assign_and_print_order() should then assign an order to a table. Inside of our function, access the nested
order key for the specific table (using the
table_number argument) from
tables and set it to the
In addition to assigning the order to our
tables dictionary, we also want to print every ordered item so the kitchen knows what to cook!
assign_and_print_order() use a
for loop to iterate through
order_items and print each individual order item.
Lastly, let’s see our function in action. Luckily we just had a new customer come in for their reservation. Use the
assign_table() function to add a new customer named
'Arwa' to table
2 with a VIP status set to
Now that Arwa is seated and ready to order, call our
assign_and_print_order() function for table
2 with the order items of
tables to see the result!