RecursionError:比较时超出最大递归深度

新手上路,请多包涵

我希望这不是重复的,如果是这样,我深表歉意,但是已经进行了一些谷歌搜索并查看了堆栈溢出,但目前还没有发现任何东西……

MCVE

我知道,如果一个函数不断调用自身,那么如果没有堆栈溢出,这种情况就不会无限期地发生,因此在一定限制后会引发错误。例如:

 def foo():
    return foo()

foo()

这会导致以下错误:

 RecursionError: maximum recursion depth exceeded

但是,如果我编写如下函数:

 def count(n):
    if n == 0:
        return 0
    else:
        return count(n-1)+1

count(1000)

我得到一个稍微不同的错误:

 RecursionError: maximum recursion depth exceeded in comparison

问题

上述错误中的“比较”指的是什么。我想我要问的是这两种情况之间有什么区别,这会导致两种不同的错误。

原文由 tim-mccurrach 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

当引发 RecursionError 时,python 解释器还可能为您提供导致错误的调用的上下文。这仅用于调试,以提示您应该在代码中的哪个位置查看以解决问题。

例如,参见此通告 str 导致不同消息的调用设置:

 >>> class A:
...     def __str__(self):
...         return str(self.parent)
>>> a = A()
>>> a.parent = a
>>> str(a)
RecursionError: maximum recursion depth exceeded while calling a Python object


在引入 RecursionError 的问题讨论 中没有关于此行为的文档,但您可以只搜索 cpython 代码以查找 Py_EnterRecursiveCall 的出现。然后你可以看到根据错误发生的位置返回的实际上下文:

 Py_EnterRecursiveCall(" while encoding a JSON object")
Py_EnterRecursiveCall(" while pickling an object")
Py_EnterRecursiveCall(" in __instancecheck__")
Py_EnterRecursiveCall(" in __subclasscheck__")
Py_EnterRecursiveCall(" in comparison")
Py_EnterRecursiveCall(" while getting the repr of an object")
Py_EnterRecursiveCall(" while getting the str of an object")
Py_EnterRecursiveCall(" while calling a Python object")
Py_EnterRecursiveCall("while processing _as_parameter_") # sic
# .. and some more that I might have missed

原文由 Arne 发布,翻译遵循 CC BY-SA 4.0 许可协议

我试了一下,发现了一些有趣的结果。

据我们所知:

 def foo():
    foo()

引起

RecursionError: maximum recursion depth exceeded

我发现的是

def bar():
    if False:
        return 0
    else:
        bar()

def baz():
    if True:
        baz()
    else:
        return 0

bar()baz() 都会产生

RecursionError: maximum recursion depth exceeded

然后

def ding():
    if 1 == 2:
        return 0
    else:
        ding()

def dong():
    if 1 != 2:
        dong()
    else:
        return 0

ding()dong() 都会产生

RecursionError: maximum recursion depth exceeded in comparison

我的直觉是 python 知道您正在使用比较器 =,!,<,> 进行比较,并且这种比较永远不会达到“基本情况”条件(在最大深度的限制内)。所以 python 让你知道你的比较永远不会收敛以满足条件。

当您尝试时,这种帮助开始失效

def oops():
    if 1 == 2:
        oops()
    else:
        oops()

但最后 python 只能对错误信息有帮助。

原文由 Jake 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题