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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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.

Writing a Keyword-Only Function

00:00 I think the more common use case will be the next one, which is the keyword-only, where you have a star (*), where you want the function to be called only with keyword arguments.

00:10 And this has been around for longer as well, like Ian already mentioned. Yeah, so it won’t let you just do the positional ones here. You get the TypeError. And with pretty nice error messages that just tell you that it takes zero positional arguments, but you gave it three. Exactly.

00:31 So the fix clearly is—.

00:36 You can actually do that. It’s a nice thing about keyword arguments—. Yeah, I was going to to say, just mix them up just because you can. That’s one of the nice things about saying you want all of the following ones to be keyword arguments.

00:52 Say you have loads of arguments, and you want people to use them as that and not try and think about the order. And you might just want to use one or two or not all of them at least, you know, and then you don’t need to remember the exact order of which one followed which one. Right. And I imagine you can also do it with this version as well.

01:16 So you don’t need that syntax to do that. It’s, more of a guard against—. The nice thing about it is that it kind of explains the error that you get if you have a mix of positional-only arguments and keyword arguments. Right.

01:29 Which is that you can’t write any sort of positional arguments after you’ve put in a keyword argument, because then Python wouldn’t know about the order of things, you know? Yeah.

01:40 Which is just a general thing that also doesn’t have anything to do with the syntax, but it still comes into play, I think, into this, in this combined function example that you have next. Right, yeah.

Become a Member to join the conversation.