Python 3, an Illustrated Tour Transcripts
Chapter: Functions
Lecture: Keyword-only Arguments
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
In this video we are going to discuss keyword only arguments. These came out in Python 3 PEP 3102.
0:07
The motivation from this can be gleaned from reading the pep it says one can easily envision a function which takes a variable number of arguments
0:14
but also takes one or more options in the form of keyword arguments. Currently, the only way to do this is to provide both
0:22
a varagrs argument and a keywords argument **kwargs and then manually extract the desired keywords from the dictionary.
0:30
In Python 2 you can't have a *args argument and then put named parameters after you can only put the **kwargs after it.
0:39
So this PEP introduces 2 syntactical changes that you can have named arguments after *vargs in Python
0:45
and that you can also use a bare star * and have named arguments after them. Let's look at some examples.
0:52
Here we have *args here and we also have a keyword argument name is equal to Joe following that,
0:58
in Python 2 this would be a syntax error, you cannot do this. But Python 3 supports it.
1:03
We're also using the fstring here and we're just printing out Hey name whatever is passed in for name. Let's look at some invocations here
1:10
if we call foo by itself name defaults to Joe so there's no change there to the name value.
1:17
Note that if I call foo with Matt, Matt comes in as a positional argument because it's not a keyword argument. So I also get Hey Joe.
1:26
Finally here, if I say name is equal to Matt, name gets overridden and the result is Hey Matt.
1:30
Here's an example using the new star just by itself syntax. Again, this is Python 3 syntax only, it won't work in Python 2.
1:39
I've got a star there, what that is indicating is that anything following this is a keyword only argument and must have a keyword to update it
1:49
and because there are no arguments proceeding it, positional arguments or otherwise, if you want to change the value of name,
1:56
you need to provide keyword arguments. Note that I can also call foo2 by itself and name will default to Joe,
2:03
because this keyword argument has a default value. Let's look at some invocations here.
2:08
Here I'm calling it with no parameters and name defaults to Joe. Here I'm calling it with Matt as a parameter, but this is a positional parameter
2:16
and because I haven't allowed in my function definition to have positional parameters,
2:21
I'm going to get an error, I am going to get a type error that says foo2 takes 0 positional arguments, but I gave it one,
2:28
and finally here, I'm going to call it with a keyword argument name is equal to Matt and name overrides the value that is defaulted to Joe
2:36
and I get hey Matt as an output. Here, I've got another example, I've got a bare star by itself,
2:43
and then I also have a keyword argument that doesn't have a default value.
2:48
In essence, what this is telling Python is that I don't have any positional arguments that I will support but you need to require a keyword name
2:56
when you invoke this, if you don't, you're going to get an error and we'll see here in the calls below.
3:02
Here I call foo3 with no arguments and I get a type error. It says I'm missing a keyword only argument name so I am missing one keyword only argument.
3:11
Here I'm calling it with a positional argument and I get an error that says I take 0 positional arguments, but you gave me one.
3:18
And finally, I'm calling it with a keyword argument and name gets overridden to Matt.
3:23
So again, in essence, this is requiring any invocation of foo3 to type out name.
3:31
The motivation for this change in Python 3 is to improve the readability. If you have a function that says send 404, 200 and 100,
3:39
these are all magic numbers and it's not clear what these mean. If these are positional named arguments,
3:45
we can provide the names of them if we want to but it's not required. What this is doing is it's forcing us to provide the name to them
3:53
and one could make an argument here at this second line of code here send code is equal to 404, amount is equal to 200, timeout is equal to 100
4:03
is clear and explicit about what the intent of the code is. So if you have code that looks like this
4:09
where you have a bunch of numbers or configuration parameters and you're not clear what they're doing
4:15
or when you come back to them it's not clear to you what's going on there, consider using keyword only arguments to make your code more readable.