我正在阅读 M.Lutz的Learning Python,发现了一段奇怪的代码块:
>>> 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
为什么当我调用
next(I2)
时,迭代已经结束了?
难道我没有创建两个独立的I1
和I2
实例吗?为什么它的行为就像一个static
对象的实例一样?