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

Putting It All Together

00:00 Now that you’ve seen argument tuple packing and argument dictionary packing, let’s take a look at how they can be used together for creating a function. If you were going to use positional parameters, argument tuple packing, and argument dictionary packing in a single function definition, you must apply them in that order. Required positional parameters would have to be first, then additional positional arguments, followed by any additional keyword arguments.

00:35 So here is an example function definition, which is going to take two required parameters, a and b, and then any additional positional arguments are going to be packed into args, and then following that we can allow for any number of keyword arguments to be passed and packed into the dictionary kwargs.

01:00 And then this function is simply going to display each of the parameter variables, and so we will see the specific values we named for a and b, we will see the tuple created for the additional positional parameters, and then we will lastly see the dictionary created with any keyword arguments that were provided.

01:29 And so a typical function call would first ask us for the required argument a, then a required argument b, and then any number of additional positional parameters.

01:42 We can use strings, we can use additional numbers, and so on and so forth. And then once we have all of the additional positional parameters, we can start specifying keyword parameters.

01:57 So maybe x, that is equal to a number 100, then a keyword parameter y, which is equal to a string,

02:07 and then maybe another keyword argument z.

02:11 And we can see how the positional parameters after a and b are packed into a tuple. We can see how the keyword arguments x, y, and z are passed into that dictionary.

02:25 And this provides just about as much flexibility as you could ever need in a function interface. Next, we’ll take a look at how you can use multiple unpackings in a function call: the opposite end of using multiple packings in a function definition.

Become a Member to join the conversation.