Writing Functions in Script Files
In interactive mode, the interpreter displays ellipses ( . . . ) to indicate that a function definition is incomplete and waiting for the function body.
The Prompt Changes
When you define a function in Python's interactive mode, the interpreter does not treat the function header as a complete statement. After you enter a line beginning with def and ending with a colon, the normal >>> prompt changes to . . . . This change tells you that the interpreter is waiting for the function body and that the definition is not complete yet.
In interactive mode, the ellipses prompt, written as . . . , indicates that a function definition is incomplete and that the interpreter is waiting for the function body.
Building the Definition
The ellipses are not an error message. They are a signal that the interpreter has recognized the start of a multi-line function definition and is collecting the lines that belong to it. The body must be indented. Typically, the indentation is four spaces or one tab, and every line that belongs to the body must use the same indentation level.
Generated example: Imagine defining a function named greet in the REPL. After entering its function header, you see . . . rather than >>>. You then enter an indented body line. Seeing . . . again means the interpreter is still collecting the definition, not that the function has already been completed.
Interactive Mode and Scripts
Interactive mode receives one line at a time, so the interpreter needs a clear signal that the multi-line function definition is finished. The . . . prompt signals that the definition is still being collected, and an empty line signals that collection should end.
A script file works differently. The interpreter can read the entire file and use indentation changes to determine where the function ends. When a line returns to the base indentation level with no indentation, the interpreter knows that the function definition has ended. Therefore, a script file does not require the special empty line used in interactive mode, and it does not display ellipses as interactive feedback.
A REPL Testing Routine
A reliable interactive workflow has four stages. First, enter the function header. Second, enter each body line with the required indentation. Third, enter an empty line to finish the definition and return to the >>> prompt. Fourth, call the function to test it. The return to >>> is the useful confirmation that the interpreter has accepted the definition and is ready for a new command.
Defining and Testing a Function
Use the REPL to define a function and then test it.
Enter the header: At the normal >>> prompt, enter a function definition header ending with a colon. The interpreter changes to the . . . prompt because the definition is incomplete.
Enter the body: At the . . . prompt, enter the function body with indentation. A further . . . prompt indicates that the interpreter is still inside the definition.
End the definition: Press Enter to move to a new line, then press Enter again without entering code or indentation. This empty line tells the interpreter that the definition is complete.
Test the function: Wait for the normal >>> prompt to return, then enter a call to the function. This lets you test the definition immediately in interactive mode.
The function is defined and ready to be called after the interpreter returns to the >>> prompt.
What do you think happens?
After entering the final indented body line, what should you do before entering a function call?
Reveal answer
Answer: Enter an empty line with no indentation or code.
The empty line ends the interactive function definition. The interpreter then returns to the normal >>> prompt, where a new command such as a function call can be entered.
Frequent REPL Mistakes
Treating . . . as an error message.
The ellipses indicate that the function definition is incomplete and that the interpreter is waiting for the body.
Fix:
Enter the indented function body, then finish with an empty line.Entering the body without indentation.
Indentation tells Python which lines belong to the function.
Fix:
Indent each body line, typically by four spaces or one tab, and keep subsequent body lines at the same indentation level.Trying to finish the definition with a nonempty line.
The completion signal is a completely blank line with no indentation and no code.
Fix:
Press Enter again without typing anything.Expecting a script file to require the same empty line.
The interpreter can read the entire script and infer the boundary from indentation changes.
Fix:
In a script, the function ends when the following line returns to the base indentation level.
Use the prompt as a progress indicator. The . . . prompt means keep supplying the function definition. The >>> prompt means the interpreter is ready for a new command, such as a function call.
Practice the Transition
Describe the REPL steps you would use to define a function with one indented body line and then test it. Include what the . . . prompt means and explain exactly how you would return to the >>> prompt.
Hints
- Start with the function header at the >>> prompt.
- The body line must be indented.
- The definition ends with a completely blank line.
- Only after the >>> prompt returns should you enter the function call.
Key Takeaways
- The . . . prompt means an interactive function definition is incomplete.
- The function body must be indented so the interpreter knows which lines belong to the function.
- Press Enter twice, with the second line completely blank, to end a function definition in interactive mode.
- Interactive mode needs this empty-line signal because it receives input one line at a time.
- Script files do not require the empty line because indentation changes reveal where the function ends.
- After the interpreter returns to >>>, you can call the function to test it.
Key Takeaways
- The . . . prompt shows that the interpreter is waiting for the rest of a function definition.
- Indent every line in the function body.
- End an interactive definition with a completely blank line by pressing Enter twice.
- Scripts use their file structure and indentation changes to identify the end of a function, so they do not need this interactive empty-line step.
- Test the function only after the normal >>> prompt returns.