Lie wrote:
When you've got a nested loop a StopIteration in the Inner Loop would break the loop for the outer loop too:a, b, c = [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5] def looper(a, b, c): for a_ in a: for b_ in b: for c_ in c: print a_, b_, c_ looper(a, b, c) # Intended behavior [1] a, b, c = iter(a), b, iter(c) # b is intentionally not iter()-ed looper(a, b, c) # Inner StopIteration prematurely halt outer loop [2]
iterators are once-only objects. there's nothing left in "c" when you enter the inner loop the second time, so nothing is printed.
>>> a = range(10) >>> a = range(5) >>> a = iter(a) >>> for i in a: ... print i ... 0 1 2 3 4 >>> for i in a: ... print i ... >>> > This is a potential problem since it is possible that a function that > takes an iterable and utilizes multi-level looping could be > prematurely halted and possibly left in intermediate state just by > passing an iterator. it's a problem only if you confuse iterators with sequences. </F> -- http://mail.python.org/mailman/listinfo/python-list
