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

Guido van Rossum guido at python.org
Fri Nov 21 19:18:41 CET 2014


On Fri, Nov 21, 2014 at 8:47 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:

> On Fri, 21 Nov 2014 05:47:58 -0800
> Raymond Hettinger <raymond.hettinger at gmail.com> wrote:
> >
> > Another issue is that it breaks the way I and others have taught for
> years that generators are a kind of iterator (an object implementing the
> iterator protocol) and that a primary motivation for generators is to
> provide a simpler and more direct way of creating iterators.  However,
> Chris explained that, "This proposal causes a separation of generators and
> iterators, so it's no longer possible to pretend that they're the same
> thing."  That is a major and worrisome conceptual shift.
>
> I agree with Raymond on this point.
>

Pretending they're the same thing has always been fraught with subtle
errors. From the *calling* side a generator implements the same protocol as
any other iterator (though it also has a few others -- send(), throw(),
close()). However *inside* they are not at all similar -- generators
produce a value is done through "yield", __next__() methods use return.

Even if we end up rejecting the PEP we should campaign for better
understanding of generators. Raymond may just have to fix some of his
examples.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20141121/c080d774/attachment.html>


More information about the Python-Dev mailing list