[Python-ideas] PEP 479: Change StopIteration handling inside generators

Nick Coghlan ncoghlan at gmail.com
Wed Nov 19 17:45:27 CET 2014


On 20 November 2014 02:24, Chris Angelico <rosuav at gmail.com> wrote:

> Does that make for more justification? Unexpected exceptions bubbling
> up is better than unexpected exceptions quietly terminating loops?
>

The part I found most compelling was when you pointed out that in the
special method implementations, the normal return path was always spelled
with "return", while the "value missing" result was indicated with a
special kind of exception (StopIteration, AttributeError, IndexError or
KeyError), and then any other exception was consider unexpected.

Generators add the third notion of being able to suspend execution via
"yield", which then left them with two different ways of spelling
termination inside the frame: "return" OR "raise StopIteration". The second
spelling ("raise StopIteration") is then inherently surprising, as it's
entirely redundant, *except* in that it allows you to effectively have a
"hidden return" in a generator frame that can't be done anywhere else.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141120/8e2221e2/attachment-0001.html>


More information about the Python-ideas mailing list