Using Special Parameters in Real-World Code
00:00 So, do you maybe have an example that you can show us where this is actually used in some Python source code? Yeah, absolutely. It’s a great question because yeah, sometimes seeing all this in the abstract is a bit, well, abstract.
So this is a dictionary, which means we can call a bunch of methods on it. One of them being
.get(), and in IPython, what you can do is you can append the function with a question mark (
?), and that will return its signature and its docstring.
So as you can see here, it’s got the signature, the signature being the function identifier and then the arguments that it accepts. And you’ll see here, the forward slash symbol (
/) as a special parameter for
.get(), which basically means that you can’t call
.get() with any keyword arguments.
You have to call it with just positional arguments. Nice. So I can’t put
default= when I call
dictionary.get(). That’s going to give me that error that we’ve seen before in some of the previous parts. Right, so—. Which are you calling?
So yeah, it just serves as a way to get arguments, and you can—not
default. You used
defail, which kind of made sense. [Laughter.] Let’s just return
0 if there’s not, and let’s try and get a key that doesn’t exist.
But if you try and pass this as a keyword, then you’ll get an error, a
TypeError. Even though when you look at the function signature, you can see that at least like the printout here in the IPython console gives you this, the name also says
default=None, but you can’t pass it because of the
/. Yeah, exactly.
You might use this to call
.get() from within your Python program, and you’d pass the
args as the first argument. And you don’t need any of the other ones because they have default values, but there’s certain ones at the end, which have to be keyword arguments.
Interesting, yeah. And that’s good to see this as examples because it might be confusing otherwise to see a
*, in there and then just be like, “Wait, what’s what can I pass here? Is it like a regex symbol, and I can pass anything that I want?” Right, yeah.
I certainly thought that the first time I saw these symbols, which made me want to look into a bit further. But all it that it really means here is that starting from
user, even though they have default values, the rest of the parameters that are defined here need to be passed in as keyword arguments. That’s right. Cool. Well, thanks for showing me these examples. Yeah, no problem. Okay.
So just to recap that, looking at the
.get() method of a dictionary whose signature has a
/ symbol, which just means that all the arguments that you pass into the
.get() method need to be positional and they cannot be keyword arguments.
Become a Member to join the conversation.