[Python-ideas] Change how Generator Expressions handle StopIteration
Steven D'Aprano
steve at pearwood.info
Fri Nov 7 12:20:30 CET 2014
On Thu, Nov 06, 2014 at 10:54:51AM -0800, Guido van Rossum wrote:
> TL;DR :-(
That's how I feel about this whole thread ;-)
[...]
> If I had had the right foresight, I would have made it an error to
> terminate a generator with a StopIteration, probably by raising another
> exception chained to the StopIteration (so the traceback shows the place
> where the StopIteration escaped).
>
> The question at hand is if we can fix this post-hoc, using clever tricks
> and (of course) a deprecation period.
Do we need "clever tricks"? In my earlier email, I suggested that if
this needs to be fixed, the best way to introduce a change in
behaviour is with the __future__ mechanism. 3.5 could introduce
from __future__ stopiteration_is_an_error
(in my earlier post, I managed to get the suggested behaviour completely
backwards) and then 3.6 could raise a warning and 3.7 could make it
the default behaviour.
We're still breaking backwards compatibility, but at least we're doing
it cleanly, without clever and/or ugly hacks. There will be a transition
period during which people can choose to keep the old behaviour or the
new, and then we transition to the new behaviour.
--
Steven
More information about the Python-ideas
mailing list