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

Chris Angelico rosuav at gmail.com
Sun Nov 16 00:06:29 CET 2014


On Sun, Nov 16, 2014 at 5:20 AM, Steve Dower <Steve.Dower at microsoft.com> wrote:
> Since this changes the behavior of an object instance, how can __future__
> help? If the generator definition is in a library but the code that raises
> StopIteration to terminate it is passed in from the users code, how is the
> user supposed to select the behavior they want? (This sounds to me like a
> similar problem to adding 'from __future__ import py3_string' to Py2, which
> we discussed a while ago. Happy to be shown that it isn't.)

The behaviour selection would have to be based on the generator's
definition. This proposal, in all its variants, is about what happens
as the generator terminates; if you call on someone else's generator,
and that someone hasn't applied the __future__ directive, you'll be in
the current situation of not being able to distinguish 'return' from
'raise StopIteration'. But for your own generators, you can guarantee
that they're distinct.

ChrisA


More information about the Python-ideas mailing list