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 between dictionaries and keyword arguments.
0:09 Over here we have the same function that we explored previously, it takes 3 arguments, a name, a greeting and a number of times
0:17 to greet that person with that greeting. So we could say "Michael", I'd like to say "Hello" to Michael 3 times, something like that.
0:25 And we saw that we could use keyword arguments even to change the order or to skip over top some of the default values,
0:31 so here we can say greeting and then name, using the keyword arguments and skip the times. So let's just run this really quick to see how it works,
0:40 "Hey, you are out of order Michael!", brilliant. So suppose that I had a dictionary instead, something like this,
0:52 where we have a name a greeting and times and the keys in the dictionary correspond to the keyword arguments that we are able to use in the method,
1:00 so there is a way to take this dictionary and turn it into a function call or rather parameters to a function call via keyword arguments,
1:10 so we could say display_greeting() and we can unpack the dictionary
1:14 saying **, so it says instead of pass it as a dictionary, unpack it a keyword arguments
1:19 where you have the names are the keys and the values are the values. Let's see how that works. All right, so "Hey you are out of order, Michael!"
1:30 from up here and then we said "Ted, long time no see", do that 6 times. That's excellent.
1:36 Now this concept of ** representing a transformation of a dictionary works in both directions as well, so up here I could say **kwagrs,
1:47 now this is much like the *args we had before, that was allowing us to pass an arbitrary number of additional parameters,
1:55 this lets us pass an arbitrary number of additional keywords so if we come over here now,
2:01 and let's go and just print out what kwargs, we'll say, "kwargs =" so if we'd run it, you can see it's empty but it looks kind of like a dictionary and
2:13 in fact it is a dictionary. How do we use this? Let's come over here and let's wrap this a little bit, let's say additional=2, mode=7.
2:24 So now, these do not correspond to any parameters named or otherwise in our display greeting,
2:31 they will be gathered up into this additional kwargs here and pass along and we can do whatever we want with it,
2:36 we can check for the existence of mode and do something with that, theoretically. All right, let's see how this runs.
2:43 So now we pass our items: the greeting, the name, the times, we've also got these two additional arguments to pass in here, so in functions,
2:52 you'll see this **name and the convention is for that name to be kwargs but it doesn't have to be, you'll see this serving two roles,
3:00 either this takes keyword arguments and turns them into a dictionary in this case,
3:05 or the reverse, it takes a dictionary and turns it into keyword arguments. Let's see that in a graphic.
3:12 So here we have the same function, you can see we have the **kwargs, here you can see we are passing the greeting,
3:19 the name the times as they are via its default value, we are passing another=2 and you can see we get our greeting
3:25 and kwargs is a dictionary with another as it's one of its keys. In the reverse, we can take a dictionary and use the **dictionary name
3:37 or a **pointer to unpack the dictionary as a set of keyword arguments to a method.