[Python-ideas] Generators are iterators

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Dec 11 16:14:58 CET 2014

On 10 December 2014 at 20:46, Guido van Rossum <guido at python.org> wrote:
> I have replaced the confusing paragraph with this:
> """
> The proposal does not change the relationship between generators and
> iterators: a generator object is still an iterator, and not all
> iterators are generators.  Generators have additional methods that
> iterators don't have, like ``send`` and ``throw``.  All this is
> unchanged.  Nothing changes for generator users -- only authors of
> generator functions may have to learn something new.
> """

Generators are affected and this therefore affects authors of both
generator functions and generator expressions.

In the case of a generator function you can achieve the old behaviour
post-PEP 479 simply by wrapping the entire body of the function with:

    # body
except StopIteration as e:
   return e.value

The only difference is that the generated StopIteration has a
different traceback attached so you can't see where it was originally

Generator expressions would need to be converted to generator
functions to achieve the same effect.

> Let us know if there is still confusion about what the PEP means.

The PEP is still very confused. Primarily the fact is that the PEP is
attempting to address an issue which affects all iterators but
proposing a solution which is only about generators. The PEP seems to
suggest that there is something special about generators in this
respect when there really isn't. For example:

The interaction of generators and StopIteration is currently somewhat
surprising, and can conceal obscure bugs. An unexpected exception
should not result in subtly altered behaviour, but should cause a
noisy and easily-debugged traceback. Currently, StopIteration can be
absorbed by the generator construct.

There is no interaction between generators and StopIteration. The
issue isn't about generators it is about the iterator protocol.
StopIteration cannot be absorbed by the generator construct.

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.

More information about the Python-ideas mailing list