what happened when using the same variable in two

2019-02-22 04:42发布

问题:

I test the following code:

for i in range(3):
    for i in range(3,5):
        print "inner i: %d"%(i)
    print "outer i: %d"%(i)

and the output is:

inner i: 3
inner i: 4
outer i: 4
inner i: 3
inner i: 4
outer i: 4
inner i: 3
inner i: 4
outer i: 4

I don't understand why in the outer loop the i is 4 but the outer loop still runs for 3 times. It seems that the the variable i in the print "outer i: %d"%(i) line is the i in the inner loop , but when goes to the for i in range(3) it uses the i in the outer loop.

Anyone can explain this? It's a little confusing to me now.

回答1:

It's the same i, Python doesn't have block scope. At the beginning of each for-loop iteration, you assign the the next value in the iterator to i. Python for-loops aren't like C/Java for-loops, they are foreach loops. The continue until the iterator is exhausted (or you break out somehow). A for-loop is equivalent to the following while-loop:

iterator = iter(my_iterable)
while True:
    try:
        x = next(iterator)
    except StopIteration:
        break
    do_stuff(x)

So, your nested loop is the equivalent of this:

it1 = iter(range(3))
while True:
    try:
        i = next(it1)
    except StopIteration:
        break

    it2 = iter(range(3, 5))
    while True:
        try:
            i = next(it2)
        except StopIteration:
            break
        print "inner i: %d"%(i)

    print "outer i after: %d"%(i)

Note, a C/Java for-loop, e.g.:

for (int i = 0; i < stop; i++){
    do_stuff(i);
}

Would be in Python:

i = 0
while i < stop:
   do_stuff(i)
   i += 1

In other words, the classic-for-loop depends on i, that is, the termination condition depends on the value of i. But in a for-each loop, the termination condition depends on the iterator. And it doesn't matter what you do to the variable inside the body, at the beginning of each iteration, it is assigned the next value of the iterator.



回答2:

In the inner loop you are assigning a different variable to i. Since it is the same variableit always prints 4 (the last value i was assigned in the inner loop. However, when you go to the next iteration of outer loop it will be set to the next value (i.e. 2 for the second outer loop). You should print outer loop before the inner loop to see the effect more clearly:

for i in range(3):
    print "outer i before:%d"%(i)
    for i in range(3,5):
        print "inner i: %d"%(i)
    print "outer i after: %d"%(i)


回答3:

Notice how the output is always

inner i: 3
inner i: 4
outer i: 4

for every outer loop.

The program takes the final value of i in each iteration.

ie. when the outer loop starts execution, i is either 0, 1 or 2 but the value of i is modified in the inner for loop, which is getting printed.

Update:

for i in range(3):
    for i in range(3,5):
        print (id(i))
    print (id(i))

Output

1372154016
1372154032
1372154032
1372154016
1372154032
1372154032
1372154016
1372154032
1372154032


回答4:

There's only one i, not two. When the inner loop is entered, it changes i, and keeps changing it until the inner loop exits. The next iteration of the outer loop then sets i to the next value in its range, but you never see it because you immediately enter the inner loop once more, again changing i.

This is of course very bad practice. You should never modify the variable in a for loop while that loop is active.



回答5:

Its the same i being used in both loops. When the outer loops gets a chance to print i, it will always have the last assigned value from the inner loop.



回答6:

Check @heltonbiker answer :

>>> for i in range(3):
    print "1-",locals()
    for i in range(3,5):
        print "2-",locals()


1- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 0, '__name__': '__main__', '__doc__': None}
2- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 3, '__name__': '__main__', '__doc__': None}
2- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 4, '__name__': '__main__', '__doc__': None}
1- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 1, '__name__': '__main__', '__doc__': None}
2- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 3, '__name__': '__main__', '__doc__': None}
2- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 4, '__name__': '__main__', '__doc__': None}
1- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 2, '__name__': '__main__', '__doc__': None}
2- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 3, '__name__': '__main__', '__doc__': None}
2- {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'i': 4, '__name__': '__main__', '__doc__': None}
>>> 

You read i data on wrong time (place)!

If there is a single BUS or Variable name You must use the FIFO method.

FIFO << Here information



标签: python scope