REPL peculiarity

Alan Gauld alan.gauld at yahoo.co.uk
Thu Mar 11 08:27:18 EST 2021


On 11/03/2021 11:01, 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?

I guess just because that's how the interpreter is written. It looks for
a blank line to terminate the function definition. It does the same with
multi-line definitions too. I agree it would be possible in the case of
single line definitions to forego the blank line but it doesn't.
It does remind you that you need another line since it shows the
secondary prompt ... instead of the primary >>>

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Python-list mailing list