En Tue, 25 Sep 2007 14:48:29 -0300, <[EMAIL PROTECTED]> escribi�:

> On Sep 25, 7:07 pm, Zentrader <[EMAIL PROTECTED]> 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

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to