comprehensions was Re: Switch statements again

Martin Maney maney at
Sat Jan 18 03:18:42 CET 2003

Erik Max Francis <max at> wrote:
> Probably for the same reason that other iterative constructs that
> introduce variables don't make them local to that structure either:
>>>> [x for x in range(10)]
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>> x
> 9
>>>> for y in range(10): print y,
> .... 
> 0 1 2 3 4 5 6 7 8 9
>>>> y
> 9
> If one annoys you, surely the other would, too?

Logically, you may be right.  Nevertheless, I, too, find the continued
existence of x far more surprising than that of y.  I suspect there ar
multiple factors at work here, including similarity to other languages'
handling of loop variables (though there are some that got both ways on
this), as well as the observation that there's not really much chance
of conveying useful information out of the comprehension.  At least
that's my intuition, compared to the frequent use of an early exit from
more general loops where the current value of the loop's state may
carry the information that is the reason for the loop's existence.

invariantly yours, Martin

More information about the Python-list mailing list