当一个生成器运行超出值以产生什么时会发生什么?

时间:2016-08-23 20:34:35

标签: python generator yield-keyword

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

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,函数隐式返回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)应该是等效的,对吗?

1 个答案:

答案 0 :(得分:2)

这不是很神秘。当生成器耗尽值以产生时,它会引发StopIteration异常。您只需要了解for循环如何在Python中工作。基本上,它等同于以下代码:

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

以上相当于:

for x in collection:
    # do something