On Sep 24, 10:10 pm, Zentrader <[EMAIL PROTECTED]> wrote:
> > Your for loops both use the same counting index.
>
> Since those variables are local to the for loop,

No, "for" loops don't introduce a scope. The one variable named "j" is
shared.

> for j in range( 10 ):
>   print j, "first loop"
>   for j in range( 5 ):
>      print "     ", j, "2nd loop"

The reason it does work as hoped, is that the generators, including
those created by the call to "range", keep internal state to know the
next value to yield. The existence or current value of iteration
variables like "j" is completely irrelevant.

Here, the first the call to "range" creates a generator, and binds j
to the first value yielded, which is 0. Then the second range-
generator is created, iterated over using the same variable. Then the
first generator is asked for its second value (= 1), and j is bound to
that regardless its current value.

- Willem

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

Reply via email to