Python中最大递归深度值的探讨
作者:杰瑞26 时间:2021-01-16 14:21:21
Python对递归函数设置是有默认值。 可以通过下面命令来查看设置的默认值
>>> import sys
>>> sys.getrecursionlimit()
3000
查看该函数的帮助文件就更清晰了:
>>> help(sys.getrecursionlimit)
Help on built-in function getrecursionlimit in module sys:
getrecursionlimit(...)
getrecursionlimit()
Return the current value of the recursion limit, the maximum depth
of the Python interpreter stack. This limit prevents infinite
recursion from causing an overflow of the C stack and crashing Python.
从上面的帮助信息可以看到,如果超过这个默认的最大递归深度,就会导致不可预测的错误,比如C栈溢出或其他错误。 下面用斐波那契数列的递归函数来测试下该方法,来看真正可行的最大递归深度.
>>> def fib(n):
... if n == 1:
... return 1
... else:
... return fib(n-1) + n
>>> fib(2989)
4468555
当执行到默认的3000附近,2989时,上面是可以执行到的。当递归深度到2900时就报错了。
>>> fib(2990)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in fib
File "<stdin>", line 5, in fib
File "<stdin>", line 5, in fib
[Previous line repeated 2985 more times]
File "<stdin>", line 2, in fib
RecursionError: maximum recursion depth exceeded in comparison
maximum recursion depth exceeded in comparison
也就是最大的实际递归深度就是2989了,是否可以设置这个值大点呢? 可以通过这个方法来设置:
>>> import sys
>>> sys.setrecursionlimit(10000)
>>> sys.getrecursionlimit()
10000
通过setrecursionlimit(10000)后再查看就是10000。再来测试下实际上的递归深度可以到多少,看是否在2989上有所增加呢?
可以看到我们设置最大递归深度10000,实际执行递归深度达到3400,不再报RecursionError错误,但会报关闭程序的提示。通过一个个单独调试,到3213还能显示正常答案。到3214就又报上面的提示了。
>>> fib(3213)
5163291
上面的是Python3.6.5得出的实际最大递归数字:3213。
同样一台计算机,用Python2.7.10,同样设置成默认最大递归深度10000,得出实际最大递归深度是4484
>>> fib(4484)
10055370
所以最终这个数字取决于计算机本身的计算能力和Python的版本,如果超过系统堆栈深度,Python无法支撑也就奔溃了。同样的PC,Python的版本不同,这个值都有差异。有的时候差异还很大。
来源:https://blog.csdn.net/Jerry_1126/article/details/85331736
标签:python,最大递归深度,递归深度
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP设计常见问题及解答精要
2009-04-21 11:16:00
Python Selenium自动化获取页面信息的方法
2023-08-22 18:29:31
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
2023-10-07 08:09:10
Python tkinter模块弹出窗口及传值回到主窗口操作详解
2023-09-27 23:03:05
![](https://img.aspxhome.com/file/2023/3/61173_0s.jpg)
Python编写淘宝秒杀脚本
2021-01-31 23:23:42
![](https://img.aspxhome.com/file/2023/8/79418_0s.jpg)
JavaScript中使用Async实现异步控制
2023-08-23 00:55:54
Python趣味挑战之实现简易版音乐播放器
2021-06-25 15:13:43
![](https://img.aspxhome.com/file/2023/7/67867_0s.png)
教你制作IBM LOGO的方法
2007-10-23 13:34:00
![](https://img.aspxhome.com/file/UploadPic/200710/23/20071023133740347s.png)
混乱的标记语言XHTML2/HTML5
2009-07-31 14:27:00
![](https://img.aspxhome.com/file/UploadPic/20097/31/200973114466783s.jpg)
SQL Server正则表达式 替换函数应用详解
2023-07-07 23:53:13
python利用urllib实现爬取京东网站商品图片的爬虫实例
2022-05-01 12:32:56
![](https://img.aspxhome.com/file/2023/7/69457_0s.jpg)
AJAX的jQuery实现入门(一)
2008-05-01 12:55:00
![](https://img.aspxhome.com/file/UploadPic/20085/1/20085113327455s.gif)
简易CSS相册源代码
2008-04-18 12:28:00
apache集成php7.3.5的详细步骤
2023-08-20 16:31:05
![](https://img.aspxhome.com/file/2023/9/55479_0s.png)
MySQL与PHP的基础与应用专题之自连接
2023-11-14 08:52:37
![](https://img.aspxhome.com/file/2023/3/77203_0s.png)
Python 计算任意两向量之间的夹角方法
2022-11-10 07:01:47
![](https://img.aspxhome.com/file/2023/7/72787_0s.jpg)
Python 对象中的数据类型
2022-01-25 00:58:35
系统默认的MySQL用户名消失的解决方法
2008-12-02 14:26:00
用正则表达式找出不包含连续字符串abc的单词
2010-03-02 20:28:00
浅谈ASP自动采集程序及入库
2007-08-17 11:25:00