【发布时间】:2016-03-18 13:13:12
【问题描述】:
我正在阅读Learning Python by M.Lutz 并发现了奇怪的代码块:
>>> 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 对象的实例?
【问题讨论】:
标签: python python-3.x iterator