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