Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Bare Variable Argument Parameter

00:00 Let’s take a look at another example using keyword-only arguments. Here’s an example of a calculation function that we want to provide two arguments and an operation code, and it will perform that operation on the two numbers provided.

00:17 If we provide an operation code of plus ('+'), it will add the two numbers, if I provide an operation code of minus ('-'), it will subtract, and this one just has addition, subtraction, and division.

00:29 You can imagine this being more complicated if we were wanting to write a practical calculation function, but we’re just using this function to demonstrate keyword-only arguments, so we don’t need to implement a full version of this function.

00:46 Let’s go ahead and see how this works.

00:53 So when used properly, this works as expected. I call oper() on, say, 3, 4, and specify an operation code of "-".

01:05 It will subtract the two numbers. If I specify division, it’ll divide. And if I don’t specify an operation code, anything for that third parameter, it defaults to addition.

01:24 But right now, I’ve not done anything that specifies the operation code must be provided with a keyword. I can say 3, 4, and then use the division symbol ("/") for the third argument, and it performs the division. So, what can we do? Well, something we can try: Remember from our last lesson, if we, after specifying some positional parameters, specify some tuple packing, any remaining positional parameters will be packed into this tuple called ignore, and then the operation code would have to be specified using a keyword.

02:10 I’m calling this ignore instead of args because I am not actually going to use what’s here in the body of my function. So I don’t need a common name for it. In fact, saying ignore highlights the fact that whatever remaining positional parameters there are are going to be ignored. Let me go ahead and save this, and let me restart my REPL here.

02:45 Things are a little bit better.

02:53 I can still perform division

03:01 and subtraction. Addition works by default. But now I can do some unusual things that really don’t make sense. I can put in an additional positional parameter that’s going to be packed into the tuple that’s ignored, so I still am going to do division in this case.

03:29 But even more unusual, if I accidentally try to specify the operation code argument positionally, I’m going to get addition again because in this case, the slash ("/") was also packed into that tuple.

03:48 Here’s the solution. Starting with Python version 3, you have available what’s called a bare variable argument parameter, where you just use the asterisk (*) but don’t give it a parameter name.

04:01 And so the header of our function would look like this. The parameter list would be x, y, *, and then op= and then the default value for the operation code.

04:16 So let me change my function to do that, and what this symbol means is there are no more positional parameters. What follows must be specified by a keyword.

04:34 Let’s restart this again.

04:45 Again, the things that are supposed to work

04:52 still work. I can still specify subtraction. I can still not specify a value for op and it defaults to addition. But now if I try to do the things that don’t make sense,

05:14 I get an error message. This extra positional parameter "what" doesn’t belong there. And so instead of the function working and wondering what this third positional parameter meant, I actually do get an error message informing me that I’ve tried to call that function incorrectly.

05:41 And again, same thing if I try to provide the operation code as a positional parameter. So now we have a function that requires two positional parameters for the two argument values we want to operate on, and as a keyword parameter, the value for the operation code, and now that operation code must be specified using a keyword.

06:11 Next, we look at how to specify that some arguments must be provided positionally.

Become a Member to join the conversation.