【发布时间】:2021-01-26 23:56:29
【问题描述】:
我定义一个无限递归函数为:
>>>def f():
>>> f()
>>>
然后我调用了这个函数,这发生了:
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
>>>
接下来我会这样做:
>>>import sys
>>>sys.getrecursionlimit()
1000
>>>sys.setrecursionlimit(2147483647) #as 2147483647 is the highest number I can set for recursion in Python 3.8.5
然后我再次调用该函数,但是...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
[Previous line repeated 997 more times]
MemoryError: Stack overflow
我想知道,在将递归限制更改为 2147483647 之后,为什么 Python 仍然将递归限制为 1000?
【问题讨论】:
-
你能看到两次错误的不同吗?这意味着递归已更改为你定义的方式
-
第二个错误不是递归错误。这是内存错误。您应该运行您的程序并检查任务管理器(在 Windows 上),或者通常您的内存使用情况。也许你没有安装足够的内存。
-
好的,我看到了 @ Yeshwin Verma 程序员,谢谢
-
我有 8 GB RAM。 1000次递归树够吗? @Foxcric
-
@ChristopherPeisert 一致认为标签不属于标题(在常规句子中提及技术是可以的)。
标签: python