Dangerous behavior of list(generator)
Terry Reedy
tjreedy at udel.edu
Mon Dec 14 13:00:07 EST 2009
On 12/14/2009 10:21 AM, exarkun at twistedmatrix.com wrote:
> 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.
Any unhandled exception within a loop stops the loop,
and the exception is passed to the surrounding code.
> To continue your
> comparison to for loops, it's as if a loop like this:
>
> for a in b:
> c
>
> actually meant this:
>
> for a in b:
> try:
> c
> except StopIteration:
> break
No it does not. If c raises any exception, the loop stops *and* the
exception is passed up to the surrounding code.
> Note, I know *why* the implementation leads to this behavior.
You do not seem to know what the behavior is.
Read what I wrote last night.
Terry Jan Reedy
More information about the Python-list
mailing list