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.
- 1Enter 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.
- 2Enter the body
At the . . . prompt, enter the function body with indentation. A further . . . prompt indicates that the interpreter is still inside the definition.
- 3End 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.
- 4Test 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
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
Common mistake Treating . . . as an error message.
What it looks likeThe interpreter displays . . . after the function header.
Why it failsThe ellipses indicate that the function definition is incomplete and that the interpreter is waiting for the body.
Do this insteadEnter the indented function body, then finish with an empty line.
Common mistake Entering the body without indentation.
What it looks likeA body line is entered at the base indentation level.
Why it failsIndentation tells Python which lines belong to the function.
Do this insteadIndent each body line, typically by four spaces or one tab, and keep subsequent body lines at the same indentation level.
Common mistake Trying to finish the definition with a nonempty line.
What it looks likeText or code is entered on the line intended to end the function.
Why it failsThe completion signal is a completely blank line with no indentation and no code.
Do this insteadPress Enter again without typing anything.
Common mistake Expecting a script file to require the same empty line.
What it looks likeA learner adds an empty line specifically to tell a script that the function has ended.
Why it failsThe interpreter can read the entire script and infer the boundary from indentation changes.
Do this insteadIn 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
- 1The . . . prompt means an interactive function definition is incomplete.
- 2The function body must be indented so the interpreter knows which lines belong to the function.
- 3Press Enter twice, with the second line completely blank, to end a function definition in interactive mode.
- 4Interactive mode needs this empty-line signal because it receives input one line at a time.
- 5Script files do not require the empty line because indentation changes reveal where the function ends.
- 6After the interpreter returns to >>>, you can call the function to test it.
At a glance
- 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.