REPL peculiarity

Terry Reedy tjreedy at udel.edu
Thu Mar 11 08:52:23 EST 2021


On 3/11/2021 6:01 AM, Rob Cliffe via Python-list wrote:
> This is a valid Python program:
> 
> def f(): pass
> print(f)
> 
> But at the REPL:
> 
>  >>> def f(): pass
> ... print(f)
>    File "<stdin>", line 2
>      print(f)
>      ^
> SyntaxError: invalid syntax
> 
> It doesn't seem to matter what the second line is.  In the REPL you have 
> to leave a blank line after the "def" line.  Why?

REPL executes *one* statement at a time.  It has always required a blank 
to end a compound statement because ending with a dedented second 
statement violates that.

Something like
 >>> def f():
...      a = 3

is more typical.  A dedented statement looks like a buggy continuation 
line.

-- 
Terry Jan Reedy




More information about the Python-list mailing list