Python deque中的扩展类超过了RecursionError最大递归深度
我实现了一个扩展类,如下所示Python deque中的扩展类超过了RecursionError最大递归深度,python,python-3.x,deque,Python,Python 3.x,Deque,我实现了一个扩展类,如下所示 from collections import deque class IntCollection(deque): def is_empty(self): return len(self) == 0 def append(self, x): self.append(x) 然后: a = IntCollection() a.is_empty() --> True 好的,这样行。但是,当我发出以下命令时,出现
from collections import deque
class IntCollection(deque):
def is_empty(self):
return len(self) == 0
def append(self, x):
self.append(x)
然后:
a = IntCollection()
a.is_empty() --> True
好的,这样行。但是,当我发出以下命令时,出现了RecursionError
:
a.append(1)
RecursionError Traceback (most recent call last)
<ipython-input-104-da0a5ad497c3> in <module>
----> 1 a.append(1)
<ipython-input-101-7eac8c051433> in append(self, x)
6
7 def append(self, x):
----> 8 self.append(x)
... last 1 frames repeated, from the frame below ...
<ipython-input-101-7eac8c051433> in append(self, x)
6
7 def append(self, x):
----> 8 self.append(x)
RecursionError: maximum recursion depth exceeded:
a.append(1)
递归错误回溯(最近一次调用上次)
在里面
---->1 a.附加(1)
在附加中(self,x)
6.
7 def附加(自我,x):
---->8.自我附加(x)
... 重复最后1帧,从下面的帧开始。。。
在附加中(self,x)
6.
7 def附加(自我,x):
---->8.自我附加(x)
递归错误:超过最大递归深度:
我不知道我为什么会犯这个错误。有人能解释吗?顺便说一下,我使用的是Python 3.9.4。您正在重写deque的
append
方法。当你调用它时,它只会递归地再次调用它。Python有一个递归调用函数的最大次数,然后它会给出一个错误(RecursionError
)。
将方法名称更改为其他名称将起作用:
从集合导入数据
类IntCollection(deque):
def为空(自身):
返回长度(自身)==0
def更新(自我,x):#