Nested For and While Statements
metawilm at gmail.com
metawilm at gmail.com
Tue Sep 25 02:25:45 EDT 2007
More information about the Python-list mailing list
Tue Sep 25 02:25:45 EDT 2007
- Previous message (by thread): Nested For and While Statements
- Next message (by thread): Nested For and While Statements
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sep 24, 10:10 pm, Zentrader <zentrad... at gmail.com> 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
- Previous message (by thread): Nested For and While Statements
- Next message (by thread): Nested For and While Statements
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list