-: Hi guys and welcome back!
In this video we're going to learn
about unpacking keyword arguments.
Last video was a little bit confusing.
Now it's going to get a little bit more confusing
as we learn about keyword arguments.
Once these two videos are over though
you're going to have all of the tools necessary in Python
to create any type of function,
and use it in many different ways.
So, up to now we've been learning about
all these fundamental building blocks
and this is one of the last ones.
After this we can learn about object oriented programming
and that's another type of beast altogether now.
Let's start by defining a simple function
that will use this unpacking of keyword arguments.
So I'm gonna do star star, kwargs, now,
args and kwargs, when you use star args and star star kwargs
are the usual names for these things,
but they don't have to be.
You can use whatever variable name you want here
and indeed with star args as well.
We're going to just print them out,
so I want you to take a look at what happens
when I call my named function, and I say something like
name equal Bob and age equal 25.
If I run this code here,
you can see that what we get out is a dictionary.
So, we called our named function
that does this dictionary unpacking
with two keyword arguments, and what happens in Python
is that these keyword arguments all get collected
into this kwargs variable.
That is what the star star does,
it collects keyword arguments.
So, all of these keyword arguments get collected
and put into a dictionary where the dictionary key
is equal ti the name of the keyword argument.
So you end up with a name key and a Bob value
for this one, and an age key and a 25 value for this one.
The same way that you can collect named arguments
into a dictionary you can go the other way round,
you can unpack a dictionary into named arguments.
So I'm going to make a small change to my named function.
I'm going to pass in name
and age and I'm going to print them out, name and age.
And now I'm going to define a details dictionary
which has a name of Bob, and an age of 25.
Now I'm going to call my named function with my details.
We know that this won't work because details is a dictionary
and I'm passing it as a positional argument
which means that the details dictionary
will become a value for the name parameter.
But the age parameter will not have a value.
We need to pass that in separately, something like this.
But of course that's not what we want to do.
We don't want to pass in this whole thing as a name.
That would be a bit weird, and if you run this
you'll see that you get the entire dictionary printed out
because that's the name variable, and then 25 printed out
because that's the age variable.
Instead what we want to do is we want to pass in
the name key as the name parameter, and the age key
as the age parameter, and we saw how to do this
in the last video, just by unpacking the dictionary
into two named arguments like that.
When you do that, it treats the key as the name
for the argument, and age as the key for the argument
and assigns to them the value associated with those.
So you do end up now with Bob and 25.
Note that if you had star star kwargs
which is the collection of multiple named arguments,
you can print them out and you can still do this.
What'll happen now is instead of doing
name equal Bob and age equal 25,
you're doing exactly the same thing
but using this dictionary to pass in multiple
keyword arguments and then they are being collected
into this dictionary up here,
and then you're gonna print a dictionary out.
So that is how these two stars work.
They can be used in a function to collect named arguments
into a dictionary, or they can be used in a function call
to unpack a dictionary into keyword arguments.
Let's say we have another function called print nicely
that is going to print out his dictionary into a nice format
so, let's call the named function first
to print the arguments out,
and then we are going to print them nicely.
So we're gonna do for arg, and value, in kwargs dot items.
Then we're gonna print arg and the value.
Notice that kwargs is a dictionary
so you can use the items method on it
so that you can iterate over two of these things.
Notice that here we're using a whole bunch of things
that we've learned so far,
including normal data types like dictionaries.
We are calling functions, we are de-structuring the tuple
that that returns into two values.
We are even unpacking and repacking named arguments.
So there's a lot of stuff going on here
and I know it's a little bit confusing.
Now with this function we can do print nicely
and we can pass name is Bob and age is 25
and what's gonna happen is we're going to collect
these named arguments into this dictionary.
Then we're gonna call the named function,
but we're going to unpack that dictionary
into named arguments so we're gonna pass in
name equal Bob and age equal 25.
Those are then gonna be collected into this dictionary,
and they're gonna be printed out,
so we're gonna print a dictionary.
And then we're gonna come back out here
and we're going to iterate over the items,
getting arg and value for each item in the dictionary
which is gonna give us name and Bob and then age and 25.
And finally we're going to print those out
in a bit of a nicer way.
So we're gonna print the dictionary first,
and then two more lines.
And that is indeed what we get back.
You can use both actually,
star args and star star kwargs if you want.
And nothing changes, you're gonna collect
all the positional arguments into args
and all the named arguments into kwargs.
So you can do something like both.
One, three, five, name equal Bob, and age equal 25.
One, three and five will be collected into args
because they are positional arguments.
Name equal Bob and age equal 25
will be collected into kwargs.
So if we run that, you can see that you get
a tuple of arguments, and then a dictionary of arguments.
This syntax here, args and kwargs with the stars
is normally used to accept an unlimited number of arguments.
So you can pass in anything you want to this function,
any positional arguments and any named arguments
and it will accept them, and that is normally used
so that some of those arguments or all of those arguments
can be passed onto another function.
Here's an example of something that is done quite frequently
and we haven't written this code,
don't worry about how it's implemented.
But you have a post function that takes in
a URL, some data, some variable called json,
and then any number of keyword arguments.
And it returns just calling another function.
And it passes the URL, the data, the json,
and all of the keyword arguments.
So it essentially seems to do the same thing,
but it also adds an extra argument.
So this is very common pattern to do in Python.
To have a function that seemingly just calls another
but adds something more to it.
And that is used to simplify calling this initial function
so that you don't have to pass in this argument as well.
So just an example of the kwargs being used
to essentially pass arguments from one function to another
until they get used, so that's very common thing to do.
A final example, if you define a function
that used this unpacking, you need to call it with a mapping
if you're going to pass in this star star.
So if you do star star Bob, you're gonna get an error
because this is not a dictionary.
Similarly with star star none, you're going to get an error
because that's not a dictionary.
So if you're working with variables in your code
and then eventually you're passing them unpacked
to a function then you must make sure
that they are dictionaries.
If they are anything else then you're gonna get an error
when this line runs, som that's something that you'll see
quite frequently in Python code.
The variable has an unexpected value or something like that
and students sometimes struggle to determine
why you get an error.
That's because sometimes the variables that we use
may not be what we expect.
That's it for this video, thanks for joining me in this one.
I hope we've enjoyed it, and I'll see you in the next one.