Semantics of return statement in Python cell

51 views
Skip to first unread message

Tyler Caraza-Harter

unread,
Mar 6, 2019, 11:43:59 AM3/6/19
to Project Jupyter
I was recently surprised by a return statement in the cell of a notebook I was reading, something like this (I simplified the original to highlight the strange behavior):

def f():
   
pass


return 5

To my surprise, running this cell did not cause an exception.  Instead, 5 went to the Out.  Is this a bug, or is this behavior expected?

Surprisingly, I do get an error (SyntaxError: 'return' outside function) when I have a return without a proceeding function:

return 5

Thanks!

Matthias Bussonnier

unread,
Mar 6, 2019, 11:52:27 AM3/6/19
to jup...@googlegroups.com
That is an oversight and a bug in IPython, it is likely due to the fact that we attempt to parse top-level async-code.
We fixed similar bugs in recent IPython that allowed top level `break`, and `continue`.
This should raise a SyntaxError.
-- 
Matthias


--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/5df252cb-f76d-4972-887b-37346df82fdc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tyler Caraza-Harter

unread,
Mar 6, 2019, 12:10:38 PM3/6/19
to Project Jupyter
Thanks for confirming!  Is this a known problem, or should I go ahead and report an issue on GitHub?  (I didn't see anything when I checked before, but I may have overlooked it).

Matthias Bussonnier

unread,
Mar 6, 2019, 1:49:16 PM3/6/19
to jup...@googlegroups.com

And we would need to figure out why it does not raise a SyntaxError.
-- 
M


Reply all
Reply to author
Forward
0 new messages