Exploring Special Function Parameters (Summary)
In this Code Conversation video course, you’ve explored:
- Specifying default values for arguments
- Writing functions that are positional only, keyword only, or combined
- Using Python’s documentation to use terminology accurately
- Using special function parameters in real-world code
To learn more about the concepts in this course, you can check out:
- Defining Your Own Python Function
- What Are Python Asterisk and Slash Special Parameters For?
- Using Python Optional Arguments When Defining Functions
- Python args and kwargs: Demystified
- Defining and Calling Python Functions - Video Course
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Thanks for walking me over this example here, Ian. Pleasure. So the takeaway here is that, first of all, our aside discussion about when you define a function and you put in the parameters in here, then you can either write them without the default value or not.
00:18 But that doesn’t define yet whether they’re going to be a positional argument or a keyword argument. That is really just when you call it. Yeah. It’s also signified by the name, I guess, because when you put something in here, it’s a parameter, but when you actually fill it with a value during a function call, then you call that an argument.
00:38 And then Ian showed that you can, if you pass something with default values, you can call it either as a positional argument by just passing a value as an argument, or you can also pass it as keyword arguments by actually giving the name of the parameter and then the value to it.
00:55
Then we talked about the newest addition to this whole story, which is the forward slash (/
), which allows you to define positional-only arguments in your function definition.
01:07 And what happens is that everything that you put before this special parameter, that the docs we learned calls a symbol in this case, these are going to be positional arguments in your function, and you can only use them as positional arguments.
01:20
Even though you’re giving them a name here, you can’t use that name when you’re calling the function. And then somewhat the inverse thing or the thing that ties it back to keyword arguments is the star (*
) that’s been around for longer. And in that case, everything that you put past this special parameter symbol here, the *
, all of these ones are going to have to be passed as keyword arguments when you call the function. And here in this combined example, we see just that: a combined example, where you pass both the /
as well as the *
, and then the function that you make out of that means that the first thing has to be passed as a positional argument, so this cannot use the name, The second argument to the function can be either used as a positional argument or as a keyword argument, and the third argument to the function has to be used as a keyword argument.
02:14 Is that a good sum-up? What do you say? Great. All right. Yeah, exactly. It’s one of those things that is optional. You don’t have to use these symbols in any place.
02:26 I think it’s going to be very common to see in documentation for libraries where the developers of a particular library might want you to use this function in a particular way.
02:36 That’s certainly where I came across these symbols for the first time.
02:41 So it’s good to know what they do so that you can read the code mostly, when you’re digging into some open-source library, for example. Exactly.
Glenn Lehman on Aug. 15, 2022
Ian and Martin well done. My favorite quote was “when describing something abstract is well, abstract”. I missed the office hours where this was discussed, but have always just passed over the * in a function definition. (I assumed incorrectly what it meant)
Thanks for the time involved in putting this together.
Ian RP Team on Aug. 16, 2022
Thanks for the nice comments Jerry and Glenn 🙂 We’re glad that this experimental format seems to be working.
Become a Member to join the conversation.
Jerry C on Aug. 9, 2022
Very nicely explained and easy to understand. I like having two teachers interact with what-if questions.