Nested For and While Statements
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Sep 25 23:09:49 EDT 2007
En Tue, 25 Sep 2007 14:48:29 -0300, <metawilm at gmail.com> escribi�:
> On Sep 25, 7:07 pm, Zentrader <zentrad... at gmail.com> wrote:
>> Note that in list comprehension, [x for x in (1, 2, 3)], the
>> for loop allocates memory the same way, but the scope changes so that
>> "x" is visible outside the for loop,
>
> How is this different? The variable spilling of list comprehension is
> the same as for regular for loops.
The fact that list comprehensions "leak" its loop variable is an
unfortunate accident and will be corrected in Python 3.0
> The (#) and (##) lines below don't
> print the same value of j.
>
>>>> for j in range(3):
> ... print j, "first loop" (#)
> ... for j in range(3):
> ... print " ", j, "2nd loop"
> ... print j, "first loop, again?" (##)
Because neither list comprehensions nor for loops start a new scope. j is
a global here, might be local to the enclosing function if any. Anyway,
all j's are the same variable.
--
Gabriel Genellina
More information about the Python-list
mailing list