10

我在这里阅读 Python 3 的文档:

如果生成器代码直接或间接引发StopIteration,则将其转换为 a RuntimeError(将 保留StopIteration为新异常的原因)。

没看懂,谁能解释一下?

这是我在 Python 3.6 中尝试过的,但似乎没有被捕获:

def gen1():
    yield from [1, 2, 3]
    raise StopIteration

def gen2():
    raise StopIteration

try:
    a = list(gen1())
    # a == [1, 2, 3]
except RuntimeError:
    print("Caught")

try:
    a = gen1()
    next(a), next(a), next(a), next(a), next(a)
except RuntimeError:
    print("Caught")

try:
    gen2()
except RuntimeError:
    print("Caught")

try:
    a = list(gen2())
except RuntimeError:
    print("Caught")

特别是,两个调用都gen2()提出了StopIteration,但仍未转换为RuntimeError

4

1 回答 1

9

您错过了此更改适用于 Python 3.7 及更高版本。您不会在 Python 3.6 或更早版本中看到转换,除非您先通过导入启用该功能from __future__(从 Python 3.5 开始可用)。

从您链接的同一页面:

在 3.5 版更改:通过 引入了RuntimeError转换from __future__ import generator_stop,请参阅 PEP 479。

在 3.7 版更改:默认为所有代码启用 PEP 479:StopIteration生成器中引发的错误将转换为RuntimeError.

PEP 479 -生成器内部的更改 StopIteration 处理进一步详细说明了为什么进行此更改以及如何应用。对于在 Python 3.7 上运行的代码,输出变为:

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
>>> def gen1():
...     yield from [1, 2, 3]
...     raise StopIteration
...
>>> def gen2():
...     yield 42  # make this an actual generator
...     raise StopIteration
...
>>> try:
...     a = list(gen1())
... except RuntimeError:
...     print("Caught")
...
Caught
>>> try:
...     a = gen1()
...     next(a), next(a), next(a), next(a), next(a)
... except RuntimeError:
...     print("Caught")
...
Caught
>>> try:
...     a = list(gen2())
... except RuntimeError:
...     print("Caught")
...
Caught

请注意,我添加了yield 42一行以gen2()使其成为生成器。没有yieldyield from在体内,您将获得常规功能。调用生成器函数会产生一个生成器对象,并且函数体开始暂停,而调用普通函数会立即执行主体:

>>> def normal():
...     raise StopIteration
...
>>> def generator():
...     raise StopIteration
...     yield  # never reached, but this is now a generator
...
>>> normal()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in normal
StopIteration
>>> generator()
<generator object generator at 0x105831ed0>
>>> next(generator())
Traceback (most recent call last):
  File "<stdin>", line 2, in generator
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration

对于 Python 3.6,您将使用from __future__ import generator_stop编译器开关(在编写脚本或模块时在代码顶部使用它):

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
>>> def generator():
...     raise StopIteration
...     yield
...
>>> next(generator())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in generator
StopIteration
>>> from __future__ import generator_stop
>>> def generator():  # re-define it so it is compiled anew
...     raise StopIteration
...     yield
...
>>> next(generator())
Traceback (most recent call last):
  File "<stdin>", line 2, in generator
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration
于 2018-08-06T16:35:38.040 回答