[Python-ideas] Generators are iterators

Nick Coghlan ncoghlan at gmail.com
Wed Dec 10 17:21:07 CET 2014


On 11 December 2014 at 00:18, Steven D'Aprano <steve at pearwood.info> wrote:
>
> If generators are to cease to be iterators, what will they be?

What the PEP is trying to say is that generators are not the same
thing as __next__ method implementations (and I agree that shortening
the latter to "iterators" is incorrect).

__next__ method paths for leaving the frame:

* "return value" = produce next value
* "raise StopIteration" = end of iteration

Current generator paths for leaving the frame:

* "yield value" = produce next value
* "return" = end of iteration
* "raise StopIteration" = end of iteration

PEP 479 generator paths for leaving the frame:

* "yield value" = produce next value
* "return" = end of iteration
* "raise StopIteration" = RuntimeError

The only change is to eliminate the redundant method of leaving the
frame, as that behaviour means that adding a "yield" to a function
*implicitly* suppresses StopIteration exceptions raised elsewhere in
that frame.

That implicit side effect on exception handling behaviour doesn't
exist for __next__ method implementations, or for ordinary functions
used in iteration operations that accept arbitrary callables, as
ordinary functions won't include any yield expressions (by
definition).

Regards,
Nick.

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


More information about the Python-ideas mailing list