1 00:00:00,720 --> 00:00:02,430 Speaker 1: Let's talk about something called 2 00:00:02,430 --> 00:00:04,500 arguments and keyword arguments. 3 00:00:04,500 --> 00:00:07,173 I mean, we've already seen that before, right? 4 00:00:08,100 --> 00:00:11,310 With a function we actually have the special characters that 5 00:00:11,310 --> 00:00:16,309 we can use called args and star star keyword args. 6 00:00:18,540 --> 00:00:19,833 How can we use these? 7 00:00:21,330 --> 00:00:23,310 Well, let's have a look. 8 00:00:23,310 --> 00:00:26,760 Let's try and have a function, let's say define, 9 00:00:26,760 --> 00:00:31,620 and we'll call this a super_func, for super function, 10 00:00:31,620 --> 00:00:34,593 that receives some sort of arguments. 11 00:00:35,670 --> 00:00:38,130 And these arguments that we're gonna receive, 12 00:00:38,130 --> 00:00:41,940 and remember, although I'm calling these args, 13 00:00:41,940 --> 00:00:44,880 this itself is a parameter, remember? 14 00:00:44,880 --> 00:00:47,850 But let's say that we wanna just return 15 00:00:47,850 --> 00:00:51,240 sum of the arguments. 16 00:00:51,240 --> 00:00:53,910 And you might be wondering, wait, whoa, sum? 17 00:00:53,910 --> 00:00:55,530 Don't we have to define that function? 18 00:00:55,530 --> 00:00:58,080 Well, sum actually exists in Python, 19 00:00:58,080 --> 00:00:59,430 as you can see over here. 20 00:00:59,430 --> 00:01:03,060 So we can just return the sum of the arguments. 21 00:01:03,060 --> 00:01:04,530 So let's try this. 22 00:01:04,530 --> 00:01:08,937 Let's say super_func and give it arguments (1,2,3,4,5). 23 00:01:11,580 --> 00:01:12,693 If I click run, 24 00:01:15,870 --> 00:01:18,060 hmm, I get an error. 25 00:01:18,060 --> 00:01:21,180 Super_func takes one positional argument, 26 00:01:21,180 --> 00:01:23,850 but five were given. 27 00:01:23,850 --> 00:01:25,110 And that makes sense, right? 28 00:01:25,110 --> 00:01:29,040 Like I only have one parameter here, 29 00:01:29,040 --> 00:01:32,190 which is a positional argument that it accepts. 30 00:01:32,190 --> 00:01:34,260 And I'm trying to sum this, 31 00:01:34,260 --> 00:01:36,420 so it only receives one but I'm giving it 32 00:01:36,420 --> 00:01:39,390 to all these things that it doesn't know about. 33 00:01:39,390 --> 00:01:43,770 And this is where we can use something like this 34 00:01:43,770 --> 00:01:47,010 by adding a star to here. 35 00:01:47,010 --> 00:01:51,630 We're saying hey, this can accept any number 36 00:01:51,630 --> 00:01:54,093 of positional arguments like this. 37 00:01:54,930 --> 00:01:56,730 As many as I want. 38 00:01:56,730 --> 00:01:58,800 As a matter of fact, let's print this out. 39 00:01:58,800 --> 00:02:03,800 If I do print(*args) and we run this function, 40 00:02:05,940 --> 00:02:08,940 we see that the print(*args) 41 00:02:08,940 --> 00:02:12,750 gives us one, two, three, four, five. 42 00:02:12,750 --> 00:02:17,460 These are all the parameters or the arguments that we get. 43 00:02:17,460 --> 00:02:20,883 And if I actually remove the star here and click run, 44 00:02:22,350 --> 00:02:23,183 look at that. 45 00:02:23,183 --> 00:02:26,340 I actually get this as a tuple. 46 00:02:26,340 --> 00:02:31,340 So args inside of this function is a tuple of arguments 47 00:02:32,280 --> 00:02:33,760 that I give it. (1, 2, 3, 4, 5). 48 00:02:34,740 --> 00:02:38,460 So sum of the tuple (1, 2, 3, 4, 5) is going to 49 00:02:38,460 --> 00:02:40,590 give us the right answer. 50 00:02:40,590 --> 00:02:42,970 It's going to print for us 51 00:02:46,290 --> 00:02:48,123 the answer, 15. 52 00:02:50,130 --> 00:02:51,630 Very, very cool. 53 00:02:51,630 --> 00:02:56,630 So this way we can extend and use our *args to have 54 00:02:57,340 --> 00:03:02,223 in a function that can accept any number of arguments. 55 00:03:03,360 --> 00:03:06,090 So what is this one now? 56 00:03:06,090 --> 00:03:10,950 Well, this one allows us to use keyword arguments. 57 00:03:10,950 --> 00:03:15,087 For example, let's say I have the **kwargs like this. 58 00:03:17,820 --> 00:03:20,760 And by the way, this can technically be anything. 59 00:03:20,760 --> 00:03:22,530 So this is a variable that we're creating, 60 00:03:22,530 --> 00:03:26,430 so I can name it hoooooo if I wanted to, 61 00:03:26,430 --> 00:03:31,430 but the standard is to name it args and kwargs. 62 00:03:31,500 --> 00:03:34,500 Because other developers are using it this way 63 00:03:34,500 --> 00:03:37,800 and it's just the way it's done in the Python community. 64 00:03:37,800 --> 00:03:40,620 Now, with the kwargs, as you might have guessed, 65 00:03:40,620 --> 00:03:42,540 I can add keywords 66 00:03:42,540 --> 00:03:45,653 like num1=5 67 00:03:47,880 --> 00:03:51,123 and then num2=10. 68 00:03:52,620 --> 00:03:56,470 So that if I print the kwargs and I click run 69 00:03:58,410 --> 00:04:02,580 I get a dictionary of num1 equals to five 70 00:04:02,580 --> 00:04:05,340 and num2 equals to 10. 71 00:04:05,340 --> 00:04:10,080 So I can actually do something like sum plus sum 72 00:04:10,080 --> 00:04:14,813 of items in the kwargs 73 00:04:16,350 --> 00:04:19,680 and grab the values, right? 74 00:04:19,680 --> 00:04:20,829 We've seen that before. 75 00:04:22,200 --> 00:04:26,700 And let's actually grab this and do a for loop 76 00:04:26,700 --> 00:04:31,700 and say items in kwargs.values, remember? 77 00:04:32,580 --> 00:04:37,580 To grab the values over here, and in here just 78 00:04:37,740 --> 00:04:38,850 add up the totals. 79 00:04:38,850 --> 00:04:42,350 So let's say that in here 80 00:04:42,350 --> 00:04:47,350 total equals or plus equals items. 81 00:04:48,240 --> 00:04:53,240 And we'll add a total in here that is initially zero. 82 00:04:55,560 --> 00:05:00,560 So that sum will be sum(args) plus total. 83 00:05:03,210 --> 00:05:04,293 If I run this, 84 00:05:06,660 --> 00:05:07,803 I get 30, 85 00:05:09,660 --> 00:05:11,670 which looks about right. 86 00:05:11,670 --> 00:05:15,960 Now, this does look a little confusing. So let's go over it. 87 00:05:15,960 --> 00:05:20,190 We have the *args which allow us to grab these 88 00:05:20,190 --> 00:05:23,760 positional arguments and just sum everything. 89 00:05:23,760 --> 00:05:28,320 And we also have kwargs which allow us to grab any number of 90 00:05:28,320 --> 00:05:32,100 keyword arguments and get a dictionary, 91 00:05:32,100 --> 00:05:36,210 which comes as kwargs, and then use them however we want. 92 00:05:36,210 --> 00:05:38,880 In our case, we're looping over all the values, 93 00:05:38,880 --> 00:05:42,790 so items in kwargs.values. 94 00:05:42,790 --> 00:05:46,020 And then I'm just going to total all those items, 95 00:05:46,020 --> 00:05:48,483 have a total and just return the sum. 96 00:05:50,400 --> 00:05:52,560 Now this is, again, extremely useful 97 00:05:52,560 --> 00:05:56,910 because our super_func can now take as many arguments, 98 00:05:56,910 --> 00:06:00,663 as many positional and keyword arguments as we want. 99 00:06:01,770 --> 00:06:03,660 Now, one final thing. 100 00:06:03,660 --> 00:06:07,500 There is a rule of the ordering that we can do 101 00:06:07,500 --> 00:06:10,890 of our parameters here, right? 102 00:06:10,890 --> 00:06:14,940 So the order, the rule is this. 103 00:06:14,940 --> 00:06:19,940 First in our parameters we have, well, our actual params. 104 00:06:22,140 --> 00:06:24,240 Then we can do *args, 105 00:06:24,240 --> 00:06:27,033 then default parameters, 106 00:06:27,960 --> 00:06:29,073 then **kwargs. 107 00:06:35,490 --> 00:06:37,110 Let me show you what I mean. 108 00:06:37,110 --> 00:06:40,590 If we wanna define this function, we wanna make sure 109 00:06:40,590 --> 00:06:44,253 that if we give it a parameter of, let's say name, 110 00:06:46,230 --> 00:06:49,500 that should come before *args. 111 00:06:49,500 --> 00:06:53,760 And if we have default parameters, that should usually come 112 00:06:53,760 --> 00:06:56,280 after args, but before kwargs. 113 00:06:56,280 --> 00:07:01,280 So if I do, let's say i='hi' 114 00:07:01,680 --> 00:07:03,423 and make sure we add a comma here. 115 00:07:04,470 --> 00:07:07,560 We now have, following the rule, and by the way 116 00:07:07,560 --> 00:07:09,810 you would never actually write a function like this 117 00:07:09,810 --> 00:07:12,870 because, well, frankly, it looks super confusing. 118 00:07:12,870 --> 00:07:16,230 Usually you're only using two of these 119 00:07:16,230 --> 00:07:19,050 or maybe just one of these. 120 00:07:19,050 --> 00:07:23,313 But this way I can call this function with name, Andy, 121 00:07:24,600 --> 00:07:28,920 then my args, then my default parameter, 122 00:07:28,920 --> 00:07:31,470 which will be i equals to 'hi', 123 00:07:31,470 --> 00:07:33,840 which let's say we don't even pass in. 124 00:07:33,840 --> 00:07:35,013 And then we have this. 125 00:07:36,630 --> 00:07:40,200 So if we run here, this still works. 126 00:07:40,200 --> 00:07:42,870 We are just not using name and i, 127 00:07:42,870 --> 00:07:46,080 but you see that I'm following the rule of params, 128 00:07:46,080 --> 00:07:49,503 args, default parameters, and then kwargs. 129 00:07:50,640 --> 00:07:52,650 All right, hopefully your head doesn't hurt too much 130 00:07:52,650 --> 00:07:53,550 after this one. 131 00:07:53,550 --> 00:07:55,770 Take a break and I'll see you in the next one. 132 00:07:55,770 --> 00:07:56,603 Bye-bye.