Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Methods and Functions
Lecture: Unpacking dictionaries as named arguments
0:01 The next Pythonic concept I want to explore is the relationship
0:03 between dictionaries and keyword arguments.
0:08 Over here we have the same function that we explored previously,
0:11 it takes 3 arguments, a name, a greeting and a number of times
0:16 to greet that person with that greeting.
0:18 So we could say "Michael", I'd like to say "Hello" to Michael 3 times,
0:22 something like that.
0:24 And we saw that we could use keyword arguments
0:26 even to change the order or to skip over top some of the default values,
0:30 so here we can say greeting and then name,
0:32 using the keyword arguments and skip the times.
0:36 So let's just run this really quick to see how it works,
0:39 "Hey, you are out of order Michael!", brilliant.
0:42 So suppose that I had a dictionary instead, something like this,
0:51 where we have a name a greeting and times and the keys in the dictionary
0:55 correspond to the keyword arguments that we are able to use in the method,
0:59 so there is a way to take this dictionary and turn it into a function call
1:04 or rather parameters to a function call via keyword arguments,
1:09 so we could say display_greeting() and we can unpack the dictionary
1:13 saying **, so it says instead of pass it as a dictionary, unpack it a keyword arguments
1:18 where you have the names are the keys and the values are the values.
1:24 Let's see how that works.
1:27 All right, so "Hey you are out of order, Michael!"
1:29 from up here and then we said "Ted, long time no see", do that 6 times.
1:33 That's excellent.
1:35 Now this concept of ** representing a transformation of a dictionary
1:40 works in both directions as well,
1:42 so up here I could say **kwagrs,
1:46 now this is much like the *args we had before,
1:50 that was allowing us to pass an arbitrary number of additional parameters,
1:54 this lets us pass an arbitrary number of additional keywords
1:58 so if we come over here now,
2:00 and let's go and just print out what kwargs, we'll say, "kwargs ="
2:08 so if we'd run it, you can see it's empty but it looks kind of like a dictionary and
2:12 in fact it is a dictionary. How do we use this?
2:16 Let's come over here and let's wrap this a little bit, let's say additional=2, mode=7.
2:23 So now, these do not correspond to any parameters
2:27 named or otherwise in our display greeting,
2:30 they will be gathered up into this additional kwargs here and pass along
2:33 and we can do whatever we want with it,
2:35 we can check for the existence of mode and do something with that, theoretically.
2:40 All right, let's see how this runs.
2:42 So now we pass our items: the greeting, the name, the times,
2:46 we've also got these two additional arguments to pass in here, so in functions,
2:51 you'll see this **name and the convention is for that name to be kwargs
2:57 but it doesn't have to be, you'll see this serving two roles,
2:59 either this takes keyword arguments and turns them into a dictionary in this case,
3:04 or the reverse, it takes a dictionary and turns it into keyword arguments.
3:10 Let's see that in a graphic.
3:11 So here we have the same function, you can see we have the **kwargs,
3:16 here you can see we are passing the greeting,
3:18 the name the times as they are via its default value,
3:20 we are passing another=2 and you can see we get our greeting
3:24 and kwargs is a dictionary with another as it's one of its keys.
3:29 In the reverse, we can take a dictionary and use the **dictionary name
3:36 or a **pointer to unpack the dictionary
3:39 as a set of keyword arguments to a method.