[Python-ideas] Generators are iterators

Nick Coghlan ncoghlan at gmail.com
Fri Dec 12 11:34:09 CET 2014


On 12 December 2014 at 01:14, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
>
> I think the PEP would be clearer if it properly acknowledged that the
> problem is a problem for all iterators. The question then is why the
> fix is only targeted at generators and what should be done about the
> same problem that occurs in many other forms. The PEP rationale avoids
> these issues by falsely claiming that generators are special.

I believe you're misunderstanding the problem being solved. The
specific problem deemed worthy of being fixed is that the presence of
"yield" in a function body can implicitly suppress StopIteration
exceptions raised elsewhere in that function body (or in functions it
calls). The difference in behaviour between comprehensions and
generator expressions when it comes to embedded function calls that
trigger StopIteration is a special case of that more general
difference.

This is a problem unique to generators, it does not affect any other
iterator (since explicit __next__ method implementations do not use
yield). The change in the PEP is to change that side effect such that
those exceptions are converted to RuntimeError rather than silently
suppressed - making generator function bodies behave less like
__next__ method implementations.

For special methods themselves, the problem of unexpected signalling
exceptions from other called functions being interpreted according to
their defined meaning in the relevant protocol is inherent in their
design - they only have two "normal" outcomes (returning from the
frame, or raising the signalling exception). That limitation applies
to StopIteration & __next__ in the same way that it applies to
KeyError/IndexError and __getitem__, or AttributeError and the various
methods in the attribute access protocols.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list