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

Steven D'Aprano steve at pearwood.info
Fri Nov 21 18:18:57 CET 2014


On Thu, Nov 20, 2014 at 11:36:54AM -0800, Guido van Rossum wrote:

[...]
> That said, I think for most people the change won't matter, some people
> will have to apply one of a few simple fixes, and a rare few will have to
> rewrite their code in a non-trivial way (sometimes this will affect
> "clever" libraries).
> 
> I wonder if the PEP needs a better transition plan, e.g.
> 
> - right now, start an education campaign
> - with Python 3.5, introduce "from __future__ import generator_return", and
> silent deprecation warnings
> - with Python 3.6, start issuing non-silent deprecation warnings
> - with Python 3.7, make the new behavior the default (subject to some kind
> of review)

I fear that there is one specific corner case that will be impossible to 
deal with in a backwards-compatible way supporting both Python 2 and 3 
in one code base: the use of `return value` in a generator.

In Python 2.x through 3.1, `return value` is a syntax error inside 
generators. Currently, the only way to handle this case in 2+3 code is 
by using `raise StopIteration(value)` but if that changes in 3.6 or 3.7 
then there will be no (obvious?) way to deal with this case.


-- 
Steven


More information about the Python-Dev mailing list