REPL with multiple function definitions

Roel Schroeven roel at roelschroeven.net
Sun Jun 26 18:37:55 EDT 2022


Rob Cliffe via Python-list schreef op 27/06/2022 om 0:14:
> This 2-line program
>
> def f(): pass
> def g(): pass
>
> runs silently (no Exception).  But:
>
> 23:07:02 c:\>python
> Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
> bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>   >>> def f(): pass
> ... def g(): pass
>     File "<stdin>", line 2
>       def g(): pass
>       ^
> SyntaxError: invalid syntax
>   >>>
>
> Is there a good reason for this?
The REPL requires an extra empty line to indicate the end of multi-line 
constructs. You can see it by the prompt: as long as the REPL prints 
'... '  as prompt, that means it puts everything you type in the same 
multi-line construct. To enter a new multi-line construct (such as a 
function definition, a for-loop, an if-statement, ...), press enter 
directly at the prompt; the REPL should than use '>>> ' as the prompt again.

(Alternatives like IPython (https://ipython.readthedocs.io/en/stable/) 
are a bit more loose regarding how to enter multi-line constructs)

-- 
"Iceland is the place you go to remind yourself that planet Earth is a
machine... and that all organic life that has ever existed amounts to a greasy
film that has survived on the exterior of that machine thanks to furious
improvisation."
         -- Sam Hughes, Ra



More information about the Python-list mailing list