我正在运行一个递归循环以实现对某些事物的最大优化,并且正在达到代码命中 RuntimeError: maximum recursion depth exceeded while calling a Python objec
t 的点。这是预料之中的,但我想在遇到该错误之前停止代码,以便我可以在该点查看我的数据。
是否可以使用 while 循环来表达类似的内容?类似于“嘿,运行此循环,直到达到最大递归深度。一旦达到,停止并返回。”
感谢您的帮助!
最佳答案
您可以执行以下操作
def recurse():
try:
recurse()
except RuntimeError as e:
if e.message == 'maximum recursion depth exceeded while calling a Python object':
# don't recurse any longer
else:
# something else went wrong
http://docs.python.org/tutorial/errors.html
注意:找出最大递归深度错误的错误编号并检查它而不是错误字符串可能是值得的。
关于Python 可以运行 while 循环直到 RuntimeError : maximum recursion depth exceeded while calling a Python object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12114590/