python - 了解生成器内部的 StopIteration 处理非平凡情况
问题描述
我正在帮助维护一些现在包含自动化 Python 3.7 测试的代码。这导致我遇到了一些与PEP 479 “更改生成器内部的停止迭代处理”相关的问题。我天真的理解是,您可以使用 try-except 块来修改旧代码以与所有 python 版本兼容,例如
旧代码:
def f1():
it = iter([0])
while True:
yield next(it)
print(list(f1()))
# [0] (in Py 3.6)
# "RuntimeError: generator raised StopIteration" (in Py 3.7;
# or using from __future__ import generator_stop)
变成:
def f2():
it = iter([0])
while True:
try:
yield next(it)
except StopIteration:
return
print(list(f2()))
# [0] (in all Python versions)
对于这个简单的示例,它可以工作,但我发现对于一些更复杂的代码,我正在重构它并没有。这是 Py 3.6 的一个最小示例:
class A(list):
it = iter([0])
def __init__(self):
while True:
self.append(next(self.it))
class B(list):
it = iter([0])
def __init__(self):
while True:
try:
self.append(next(self.it))
except StopIteration:
raise
class C(list):
it = iter([0])
def __init__(self):
while True:
try:
self.append(next(self.it))
except StopIteration:
return # or 'break'
def wrapper(MyClass):
lst = MyClass()
for item in lst:
yield item
print(list(wrapper(A)))
# [] (wrong output)
print(list(wrapper(B)))
# [] (wrong output)
print(list(wrapper(C)))
# [0] (desired output)
我知道A
andB
示例是完全等价的,并且C
case 是与 Python 3.7 兼容的正确方式(我也知道重构为for
循环对于许多示例都是有意义的,包括这个人为的示例)。
但问题是为什么这些例子A
会B
产生一个空列表[]
,而不是[0]
?
解决方案
前两个案例StopIteration
在课堂上提出了一个未被发现的问题__init__
。构造list
函数在 Python 3.6 中处理得很好(可能会出现警告,具体取决于版本)。但是,异常在有机会迭代之前 wrapper
传播:有效失败的行是lst = MyClass()
,并且循环for item in lst:
永远不会运行,导致生成器为空。
print
当我在 Python 3.6.4 中运行此代码时,我在两行(forA
和)上都收到以下警告B
:
DeprecationWarning: generator 'wrapper' raised StopIteration
这里的结论是双重的:
- 不要让迭代器自行耗尽。检查它何时停止是你的工作。使用循环很容易做到这一点
for
,但必须使用while
循环手动完成。案例A
是一个很好的例证。 - 不要重新引发内部异常。
None
改为返回。案例B
不是要走的路。Abreak
orreturn
将在except
块中正常工作,就像您在C
.
鉴于for
循环是 try-except 块中的语法糖C
,我通常建议使用它们,即使手动调用iter
:
class D(list):
it = iter([0])
def __init__(self):
for item in it:
self.append(item)
此版本在功能上等同于C
,并为您完成所有簿记工作。很少有需要实际while
循环的情况(跳过调用next
成为想到的一种,但即使是这些情况也可以用嵌套循环重写)。
推荐阅读
- vue.js - Nuxtj - 在身份验证过程后加载用户所有信息的最佳实践
- python - 以升序和逆序打印正整数范围 - Python
- javascript - 意外的令牌 / 在位置 0 Nodejs 的 json 中
- javascript - 我的 javascript 代码有问题 - 计算机必须猜测我输入的数字值
- python - Simpy 获取正在等待资源释放的元素
- emacs - tmux 覆盖 emacs 快捷方式
- php - ( sdonate) 1067 /var/www/donate/install.php 中“过期时间”的默认值无效:
- vuex - 页面加载突变 [Nuxt] [Vuex]
- batch-file - 如何使用批处理文件解压缩 ZIP 存档文件?
- angular - ngx-chips:无法从 autocompleteItems 添加第二个标签