如何解决为什么覆盖__iter__会引发RecursionError?
我想覆盖外部库中定义的__iter__
的{{1}}方法,所以我不能更改ExternalIter
类的源代码。我在ExternalIter
和MyIter2
中实现了两种不同的覆盖方法。
MyIter3
class ExternalIter:
def __init__(self):
self.x = iter([
(1,2),(3,4),(5,6),(7,8),(9,10)
])
def __iter__(self):
return self
def __next__(self):
return next(self.x)
class MyIter2(ExternalIter):
def __iter__(self):
while True:
try:
i,j = super().__next__()
yield i
yield j
except StopIteration:
break
class MyIter3(ExternalIter):
def __iter__(self):
for i,j in super().__iter__():
yield i
yield j
的原始行为如下:
ExternalIter
我要覆盖for i in ExternalIter():
print(i)
# (1,2)
# (3,4)
# (5,6)
# (7,8)
# (9,10)
的行为,如以下示例所示:
__iter__
for i in MyIter2():
print(i)
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10
以例外方式工作。但是,像我在MyIter2
中所做的那样,另一种替代方法会抛出MyIter3
。
RecursionError
您能解释为什么第二种重写方法在第一种方法起作用时会引发错误吗?如何在不获取for i in MyIter3():
print(i)
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-8-4e1114201cac> in <module>
----> 1 for i in MyIter3():
2 print(i)
<ipython-input-5-82da3324a8a3> in __iter__(self)
30
31 def __iter__(self):
---> 32 for i,j in super().__iter__():
33 yield i
34 yield j
... last 1 frames repeated,from the frame below ...
<ipython-input-5-82da3324a8a3> in __iter__(self)
30
31 def __iter__(self):
---> 32 for i,j in super().__iter__():
33 yield i
34 yield j
RecursionError: maximum recursion depth exceeded while calling a Python object
的情况下使用__iter__
覆盖super().__iter__()
?
您能解释RecursionError
在继承的上下文中如何工作以了解为什么__iter__
引用super().__iter__()
而不是父级吗?
解决方法
super().__iter__()
返回self
之前的 for
循环进行自己的隐式调用iter(self)
,从而开始了无限递归。>
由于for
循环的工作原理,我在这里看不到任何使用循环的方法。您可以使用显式while
循环来避免调用__iter__
。
def __iter__(self):
while True:
try:
i,j = next(self)
except StopIteration:
break
yield i
yield j
这不会将self
视为可迭代对象,而只会将其视为迭代器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。