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: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.