Keyword-Only Arguments
00:00 Another way that you can control the interface to your functions is through keyword-only arguments and positional-only arguments, and we’ll take a few lessons looking at those.
00:12 Starting with Python 3, you can specify that a parameter can only be supplied with a keyword argument. One instance where you might want to use this is if you have a function that takes a collection as positional arguments, but other parameters have a specific purpose.
00:32 The use of keyword arguments will prevent the ambiguity between which are the special purpose parameters and which are your positional arguments that your function is going to treat as that collection.
00:45 So, here is a simple function that’s going to take a collection, hopefully of strings, and display them with a decimal point between them, and all of that preceded by this prefix, and the prefix is what this example is going to be taking a look at.
01:03
But just to see how this function operates… We’re going to call it concat()
, and it works on a collection, which should be provided as simply a set of positional parameters that will be strings. And we’re going to print it!
01:20
We’re going to use an arrow ('->'
) as a prefix, and then we’re going to use the string method .join()
to take all of the strings provided as a parameter and separate them by the decimal point between the quotes.
01:43
I can concatenate strings such as 'a'
, 'b'
, and 'c'
, and we get the arrow preceding them and then the 'a'
, 'b'
, and 'c'
separated by a decimal point.
02:01 And again, it doesn’t matter how many parameters we supply,
02:08
the .join()
method simply attaches them all together separated by whatever string we provided here, which was the decimal point. But now we would like to allow, should someone using this function wish to, to replace the arrow with some other prefix.
02:31 And so here seems like a reasonable attempt. We will provide a parameter for the prefix followed by the strings we want joined together. And that will work.
02:47 So now we’ll attempt this.
02:56
First, we’ll print out the value of prefix
, and then we will have the same .join()
operation on the remaining args
.
03:11
So now I can say concat()
, and maybe I want a hyphen ('-'
) in front of my strings.
03:24
Hey, that seems to work! Or maybe two slashes ('//'
).
03:39
And that works too. But with this version, the prefix is no longer optional. I can’t just say concat('a', 'b', 'c')
and get the arrow. Now 'a'
has become the prefix. Notice there’s not a dot here, it is our prefix to 'b'
and 'c'
. In fact, if I included some more here, it might be more noticeable that there isn’t a dot between the 'a'
and the 'b'
.
04:12 There’s a dot between the remaining ones. So, this makes the prefix required, but we want the default to be that arrow, the way the function was at the very beginning.
04:27
So let’s try redefining the function, giving a default value to prefix
. And so we’ll set this up so that that’s our default value.
04:45 The body of this function will be the same.
04:59
And so recalling this function call, we get the prefix of the hyphen as expected. And if I leave the prefix out? Well, no, that didn’t work. It took 'a'
to be the prefix, because that was the first parameter.
05:26
So we’re still not using the optional default value for prefix
. Whatever we supply first, that is the parameter, that is the value being used for the parameter prefix
.
05:42
We could try specifying a value for prefix
. So, we say concat(prefix='//', 'a', 'b', 'c')
, but it doesn’t like that. We get a SyntaxError
that the positional argument was following a keyword argument. So the keyword argument has to be last, so let’s try that.
06:25
It didn’t like that either because it already used 'a'
to be the prefix, so when I try to specify another value for prefix
, it didn’t like that either.
06:38
The solution is to specify prefix
after the variable number of positional parameters. That way, Python will know to expect that. We’re going to define concat()
one more time.
06:53
We’ll use the variable number of positional parameters first. Then we will specify prefix
and give it a default value of the arrow.
07:22
And now if we specify just positional arguments, we get the default parameter. But if I specify some other prefix, I have to do it using the keyword, otherwise, in the last example, it would have just taken 'c'
as the prefix, but I can specify using a keyword only that we would like the prefix to be, in this case, '//'
.
07:57 Next, we’ll take a look at another example to specify keyword arguments, and that’s when you want to only have a certain number of positional arguments.
Become a Member to join the conversation.