当生成器用完所有值时会发生什么?

What Happens when a Generator Runs out of Values to Yield?

为了说明这个问题,假设我们有这个简单的生成器:

def firstn(n):
    num = 0
    while num < n:
        yield num
        num += 1

for i in firstn(10):
    print i

这将打印数字 0 到 9。但是如果我们有:

def firstn(n):
    num = 0
    while num < 5 < n:
        yield num
        num += 1

for i in firstn(10):
    print i

(更改在 while 语句中。)然后它只打印数字 0 到 4。一旦 num >= 5,生成器就不再产生值。

我很好奇的是引擎盖下发生的事情:我使用 PythonTutor 单步执行代码,我的印象是一旦 while 语句是不再 True,函数隐式 returns Nonefor 循环以某种方式检测到,然后也中断。我使用内置的 next 来更仔细地检查它:

>>> def firstn(n):
...     num = 0
...     while num < 5 < n:
...         yield num
...         num += 1
... 
>>> 
>>> mygen = firstn(100)
>>> next(mygen)
0
>>> next(mygen)
1
>>> next(mygen)
2
>>> next(mygen)
3
>>> next(mygen)
4
>>> next(mygen)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

这支持我的理论。我的大问题:StopIteration 是如何工作的,这是否意味着调用具有较大值的生成器等同于使用其最小终止值调用它?在我们的示例中,for i in firstn(5)for i in firstn(9999999999999) 应该是等价的,对吧?

这不是很神秘。当生成器用完要生成的值时,它会引发 StopIteration 异常。您只需要了解 Python 中的 for 循环是如何工作的。本质上相当于下面的代码:

iterator = iter(collection)
while True:
    try:
        x = next(iterator)
        # do something
    except StopIteration as e:
        break

以上等同于:

for x in collection:
    # do something