Penguinator
Penguinator

Reputation: 641

Python for loop implementation

Can someone tell me how exactly Python's for loops are implemented? The reason I'm asking this is because I'm getting different behavior in the following two for loops when I expect the same behavior (assuming cases is just a set of elements):

First for loop:

for case in cases:
    blah

Second for loop:

for i in range(len(cases)):
    case = cases[i]
    blah

I'm running my code in a multi-threaded environment.

Basically, I'm wondering whether Python's for loop's iterating over a set (as in the first for loop) is simply a quickhand way of the second one. What exactly happens when we use the python for loop, and is there any underlying optimization/ implementation that may be causing the behavior difference I'm observing?

Upvotes: 9

Views: 6261

Answers (3)

Martijn Pieters
Martijn Pieters

Reputation: 1123420

No, the second format is quite different.

The for loop calls iter() on the to-loop-over sequence, and uses next() calls on the result. Consider it the equivalent of:

iterable = iter(cases)
while True:
    try:
        case = next(iterable)
    except StopIteration:
        break

    # blah

The result of calling iter() on a list is a list iterator object:

>>> iter([])
<list_iterator object at 0x10fcc6a90>

This object keeps a reference to the original list and keeps track of the index it is at. That index starts at 0 and increments until it the list has been iterated over fully.

Different objects can return different iterators with different behaviours. With threading mixed in, you could end up replacing cases with something else, but the iterator would still reference the old sequence.

Upvotes: 17

Satyajit Das
Satyajit Das

Reputation: 55

l = [1, 2, 3, 4, 5]
l = iter(l)
while True:
    try:
        print l.next()
    except StopIteration:
        exit()

Upvotes: 0

user2049919
user2049919

Reputation: 77

i didn't get any difference,check this below, is it what u exactly trying..

>>> cases = [1,2,3]
>>> for case in cases:
...     print case
...
1
2
3
>>> i=0
>>> for i in range(len(cases)):
...     print cases[i]
...
1
2
3
>>>

Upvotes: -3

Related Questions