Static behavior of iterators in Python

Viewed 96

I am reading Learning Python by M.Lutz and found bizarre block of code:

>>> M = map(abs, (-1, 0, 1))
>>> I1 = iter(M); I2 = iter(M)
>>> print(next(I1), next(I1), next(I1))
1 0 1
>>> next(I2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Why when I call next(I2) it happens that iteration is already over? Didn't I create two separate instances of I1 and I2. Why does it behave like an instances of a static object?

2 Answers
Related