Let's say we have the following list and we are creating an iterator for it:
lst = [1,2,3]
itr = iter(lst)
lst = ['a', 'b', 'c']
for x in itr:
The iterator itself contains a reference to the list. Since
lst is rebound instead of mutated, this reference does not change.
>>> lst = [1, 2, 3] >>> itr = iter(lst) >>> lst[:] = ['a', 'b', 'c'] >>> for x in itr: ... print x ... a b c