Recently I've been developing some debugging utilities in python. Due to a number of desires to have multiple ways of interfacing with these tools, I've gone down a number of rabbit holes in how the internals of Python work. One such rabbit how is in regards to the interactive console or REPL. Note: REPL is a "read-eval_exec-print loop". I use console and REPL interchangeably.
As most know, when you start python without any parameters it drops into a interactive console:
Python 3.13.5 (main, Jun 25 2025, 18:55:22) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
When you type a (single-line) command in the terminal, it completes.
>>> print("text here")
text here
>>>
Now, the print("text here")
code above is considered a complete block (or a complete expression). Python was able to confidently assume that your command was complete and it could be run. But what if we're doing a multi-line function?
>>> def func():
... print("first")
... print("second")
...
The console will change the prompt to indicate that there is a continuance of the current python code block. The console will not run until it can assume that you are done with your code block. This assumption is based on the empty line at the end. Ok, great, but what about functions that have newlines in the middle of them? For example, what happens if you copy the following into an interactive python console? For example:
def func():
print("first")
print("second")
Magic!