Dangerous behavior of list(generator)

Antoine Pitrou solipsis at pitrou.net
Mon Dec 14 16:30:00 EST 2009


Le Mon, 14 Dec 2009 15:21:09 +0000, exarkun a écrit :
> 
> I'm asking about why the behavior of a StopIteration exception being
> handled from the `expression` of a generator expression to mean "stop
> the loop" is accepted by "the devs" as acceptable.

It's not "accepted as acceptable", it's just a side effect of how various 
means of iterating (including for loops and generators) are implemented 
in CPython.
Seeing how it doesn't seem to prevent or promote any useful programming 
idiom, there was no incentive to either 1) codify it as official spec or 
2) change it.

In other words, it should be considered undefined behaviour, and perhaps 
other Python implementations behave differently.

Regards

Antoine.




More information about the Python-list mailing list