Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Argument Tuple Packing

00:00 You’ve already seen how Python gives you a lot of flexibility at providing arguments to a function—positional arguments, keyword arguments, default arguments, and so on.

00:09 Python provides even more flexibility through the use of variable-length argument lists, and we’ll spend some time taking a look at that now.

00:18 To illustrate how this might be useful, consider writing a function that’s going to take the average of a bunch of numbers. How many parameters should it have? Three? Well, that’s useful if you’re taking the average of three numbers, but if you try to use more or less, you’re going to get an error. Specify some default values?

00:37 Okay, but what do we divide by? How many numbers are we actually given to know which numbers we actually wanted the average of? Three? Two? It’s tough to tell.

00:54 Provide a list! That would work, but it would require someone using your function to know to put things into a list first, and so that might not be the best solution at all times.

01:08 Python provides a mechanism to solve this, and we’re going to take a look at that now. It’s called argument tuple packing. The syntax is when you precede a parameter name in a function definition with an asterisk (*).

01:22 It’s an indication that you’re going to use this feature of argument tuple packing. What happens is the function can then provide as many arguments as it needs and they will all be packed into a single tuple using that particular parameter name. Now, conventionally, we use the word args for that parameter.

01:43 That’s not a requirement, but it’s what every other Python programmer does and so you’ll see that in the programs that you read and it would be best practice to use that in the programs you write.

01:55 So, to see how that works, take a look at this function. This function f() takes one parameter, args, using argument tuple packing. And just to see how it all fits together, we’re going to print the value of args, that tuple.

02:12 We will see that it is actually of type tuple and what its length is, and then we will see how we can use a for loop to get to the individual arguments that were provided.

02:27 Here, I’ve put this function into a file and we’ll come back to the avg() (average) function starting at line 10 in a moment. But again, here’s our parameter variable using argument tuple packing.

02:42 Here’s where we print the tuple, see its type and its length, and then one at a time iterate through the tuple, getting at each of the individual arguments that were provided.

02:56 So, let’s import this. from argument_tuple_packing import I’m going to go ahead and import both of them since we’re going to be using both of them, the avg() one pretty soon.

03:11 So, we’re going to call function f() and we’re just going to provide it three arguments, 1, 2, 3. These are going to get packed as a tuple, and so that’ll be the first line of output that we see.

03:26 Then we are told that its type is of <class 'tuple'> and that its length was 3. And then we can see individually the three arguments that we provided. 1, 2, and 3. Let me try this again, and this time, let me use some strings, and I’ll use a different number of strings, so 'foo', 'bar', 'baz',

03:50 and I won’t embarrass myself trying to pronounce these last two.

03:56 So, we’ve provided five separate argument values. They’re packed into a single tuple. The type of that parameter is a tuple and its length was 5, and here are the individual argument values that we gave it in the function call.

04:16 This would be very useful for our avg() function. Pack all of the numbers into a single tuple, accumulate them, and then dividing by however many values were in that particular tuple.

04:28 So we have a simple accumulating loop, start an accumulator variable total set to 0, and as we iterate through our tuple of arguments, we increase the total by that amount. Once we’re done, total has the sum of all of the values that were given as parameters and we divide by that length. There were a couple of sample runs,

04:53 but as you can see, I have this function here in this same file,

05:01 so I can go ahead and call avg() on 1, 2, 3, and it computes the average of those three numbers. I can find the average of 1 through 5.

05:16 Any number of numeric arguments that can be provided, and this function will find the average of all of them, packing them all into a single tuple, then in a for loop accumulating the sum of each of the values in that tuple, and then dividing by the length of the tuple, which contains the number of argument values given.

05:37 Python also provides a mechanism to do unpacking in a function call, and you will see that in the next lesson.

Become a Member to join the conversation.