[Python-ideas] possible extension to how the new StopIteration is handled (python >=3.3)

Chris Angelico rosuav at gmail.com
Thu Oct 2 19:50:37 CEST 2014


On Fri, Oct 3, 2014 at 3:39 AM, Stephan Sahm <Stephan.Sahm at gmx.de> wrote:
> Any feedbacks?
> Are there others thinking that this would be a straightforward extension to
> the newly introduced raise StopIteration(return_value) feature?

So basically, what you're saying is that the rule should change from
"once StopIteration has been raised, any call to next() should also
raise StopIteration" to "... raise StopIteration with the same
payload". I think that's not unreasonable in the simple form; but it
would mean that the generator would have to retain a reference to its
return value, which is contrary to what most people will expect of
function return values. It might make for unnecessary object
longevity.

Is there a backward compatibility issue here? It's theoretically
possible for code to actively expect that a repeated StopIteration
won't have a payload (eg to distinguish between the initial return and
any attempt to re-next() the generator), but is that at all a
reasonable thing to have done?

ChrisA


More information about the Python-ideas mailing list