1 00:00:00,440 --> 00:00:04,670 Packing in Python is a way to condense a series of variable arguments into a single 2 00:00:04,670 --> 00:00:05,550 dictionary. 3 00:00:05,550 --> 00:00:07,820 Where each variable name becomes a key, and 4 00:00:07,820 --> 00:00:11,140 each variable reference becomes the corresponding value. 5 00:00:11,140 --> 00:00:13,600 It's a lot like packing with Python sequences. 6 00:00:13,600 --> 00:00:18,450 But instead of passing multiple positional arguments and capturing them into a tuple 7 00:00:18,450 --> 00:00:23,750 you're passing multiple variable arguments and capturing them into a dictionary. 8 00:00:23,750 --> 00:00:24,740 So that's a lot of words. 9 00:00:24,740 --> 00:00:25,640 Let's back up. 10 00:00:25,640 --> 00:00:27,490 What's a variable argument? 11 00:00:27,490 --> 00:00:31,470 Well, oftentimes when we call functions we pass positional arguments. 12 00:00:31,470 --> 00:00:34,910 These are just comma separated values that are received by the function 13 00:00:34,910 --> 00:00:36,650 in the order in which they're sent. 14 00:00:36,650 --> 00:00:38,390 That's why they're called positional. 15 00:00:38,390 --> 00:00:41,860 The key difference between variable arguments and positional arguments 16 00:00:41,860 --> 00:00:46,060 is that, for variable arguments, the order that they're sent doesn't matter. 17 00:00:46,060 --> 00:00:48,830 And unlike positional arguments, they're named. 18 00:00:48,830 --> 00:00:50,710 So, take a look at my workspace. 19 00:00:50,710 --> 00:00:54,807 You can see I have a simple function and then a call to that function. 20 00:00:54,807 --> 00:00:57,880 I'm using standard positional arguments here. 21 00:00:57,880 --> 00:01:01,850 Now, if this function call is not placed close to the function definition 22 00:01:01,850 --> 00:01:06,420 in my code, it becomes much less obvious what these positional arguments are for. 23 00:01:06,420 --> 00:01:09,140 This can make it difficult to get up to speed if the developer is 24 00:01:09,140 --> 00:01:12,040 picking up this code base for the first time. 25 00:01:12,040 --> 00:01:16,260 Using variable arguments, however, which are also referred to as keyword arguments 26 00:01:16,260 --> 00:01:21,280 or named arguments, can add clarity and simplicity to your code. 27 00:01:21,280 --> 00:01:24,712 So, to convert these positional arguments to variable arguments, 28 00:01:24,712 --> 00:01:27,206 I simply make them look like variable assignments 29 00:01:41,278 --> 00:01:44,590 The variable names I use here have to match the parameter names in 30 00:01:44,590 --> 00:01:46,013 the function definition. 31 00:01:48,197 --> 00:01:53,730 This is how Python matches arguments and parameters when the order doesn't matter. 32 00:01:53,730 --> 00:01:57,510 So, just like when our code can call for passing an unknown or 33 00:01:57,510 --> 00:02:02,080 arbitrary number of positional arguments to a function, it might also call for 34 00:02:02,080 --> 00:02:06,940 passing an unknown or arbitrary number of variable arguments to a function. 35 00:02:06,940 --> 00:02:09,430 In both cases, that's when we use packing. 36 00:02:09,430 --> 00:02:13,177 In order to change our example here to a function that takes advantage 37 00:02:13,177 --> 00:02:17,003 of packing will change the function definition so it looks like this. 38 00:02:17,003 --> 00:02:21,243 We'll replace the individual parameters with a single parameter kwargs, 39 00:02:24,235 --> 00:02:26,570 Which stands for keyword arguments. 40 00:02:26,570 --> 00:02:30,390 Which of you recall is another word for variable arguments. 41 00:02:30,390 --> 00:02:32,650 And then we'll precede that with two asterisks. 42 00:02:34,379 --> 00:02:39,070 This differs just slightly from when we pack positional arguments into a tuple. 43 00:02:39,070 --> 00:02:43,890 For that purpose, we use a single asterisk often followed by the parameter name args. 44 00:02:44,950 --> 00:02:48,771 Now, I'm also gonna edit the body of the function to print out the content of 45 00:02:48,771 --> 00:02:52,965 kwargs by using a for loop, and I'll use an fstring to make the formatting pretty. 46 00:03:03,373 --> 00:03:05,477 I'm gonna use the .items method. 47 00:03:08,574 --> 00:03:09,309 And then I'll print. 48 00:03:22,782 --> 00:03:24,765 Okay, now I'll save and run. 49 00:03:29,821 --> 00:03:33,893 Awesome, so by converting our function definition to use packing, 50 00:03:33,893 --> 00:03:38,720 any amount of variable or keyword arguments can be passed to a function. 51 00:03:38,720 --> 00:03:41,200 I can send or not send arguments as needed. 52 00:03:42,230 --> 00:03:45,090 In this example, I can add an additional keyword argument and 53 00:03:45,090 --> 00:03:48,270 our function will still be able to process it without difficulty. 54 00:03:48,270 --> 00:03:50,480 This is why packing can be so useful. 55 00:03:50,480 --> 00:03:53,650 It allows for more seamless flexibility, which means your code and 56 00:03:53,650 --> 00:03:55,830 your app can grow with fewer problems. 57 00:03:56,950 --> 00:03:59,577 So, let's try to add another variable argument. 58 00:04:08,161 --> 00:04:09,602 Now, if we save and run this, 59 00:04:09,602 --> 00:04:12,619 we should see the newly added second topic keyword argument. 60 00:04:15,970 --> 00:04:17,900 There it is, awesome work. 61 00:04:17,900 --> 00:04:21,556 Okay, let's move on to the final lesson in this course to learn about dictionary 62 00:04:21,556 --> 00:04:22,159 unpacking.