您错过了此更改适用于 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()
使其成为生成器。没有yield
或yield 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