Exploring Special Function Parameters (Overview)
Have you ever come across the forward slash (/
) and asterisk (*
) symbols in the documentation for your favorite libraries? These represent special parameters, which tell you what kinds of arguments you can use to call a given function. In Python, these parameters can help you ensure that your functions are used correctly.
Maybe you’re a regular at Real Python’s weekly Office Hours meetup, or perhaps you’re curious about what happens there. You’ll get a glimpse in this Code Conversation, which answers one participant’s question of how to interpret and apply special function parameters in writing and calling functions.
In this Code Conversation video course, you’ll learn how to:
- Set default values for arguments
- Write positional-only, keyword-only, and combined functions
- Refer to the forward slash (
/
) and asterisk (*
) accurately - Use special function parameters in real-world code
00:00 Welcome to today’s Code Conversation. I’m Ian Currie, a Real Python team member, and I’m going to be having a conversation with Martin Breuss, who’s another Real Python team member. Hi, everyone.
00:12 Every week for our subscribers, we hold an informal hangout session that we call Office Hours. In these sessions, we hop on a video call and chat about anything Python or programming related.
00:24 Recently, a subscriber was asking about some strange function parameters that they noticed. While chatting about it, we all learned something, and we thought it would be a great topic to discuss in a Code Conversation. So in this conversation, Martin and I will be exploring special function parameters.
00:43 Martin and I will be chatting in a VS Code Live Share session, so sometimes you’ll be able to see Martin’s cursor jump around the screen to highlight something.
00:53
The special function parameters are the forward slash (/
) and asterisk (*
). These allow you to control how you want the function to be used.
01:02 Specifically, it allows you to define what parameters should be called as positional-only arguments, keyword-only arguments, or a mix of both. Now before jumping in, we’re going to disambiguate some terminology.
01:17
The function on-screen now is called hello()
. It’s a function that is defined with one parameter, called name
. This function takes the name and then prints "Hello, {name}"
.
01:28
So if the function is called with an argument of "Pythonista"
, for example, the expected output would be Hello, Pythonista
. Now, did you notice the terminology I used for defining the function and using the function? Technically speaking, when you define the function, you define parameters, and then when you call a function, you call it with arguments. In this case, you define hello()
with a parameter of name
, and then when you call it, you call it with an argument of "Pythonista"
. However, in informal conversation, it’s common to hear the terms parameter and argument used interchangeably, probably leaning towards using just argument for both cases.
02:16 With that, it’s time to get stuck into special function parameters.
Martin Breuss RP Team on Aug. 29, 2024
@Ilya Ratner I’m not sure I understood your question. Are you asking how to start a REPL session inside of VS Code? If so, then you can do that by opening the terminal in VS Code and typing python
, just like you’d start it in another shell.
If you’re wondering how to get access to the variables defined in a script inside a REPL session, then you can do that by running the script in interactive mode, using the -i
option:
$ python -i your_script.py
That command will execute your script (thereby executing the code in it and defining all the variables) and then drop you into a REPL session where you can access all the code objects defined in your script.
Hope that helps, let me know if I missed your point and if so, please add more information.
Become a Member to join the conversation.
Ilya Ratner on Aug. 29, 2024
how do you start the REPL in the terminal that’s running against your vscode file?