好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在python里递归最多达到多少次

在python里递归最多达到多少次?因为在跑程序的时候,次数有时多有时少,以前没有想过这个问题。那就自己动手在验证验证, 代码如下:

def recursion(n):
    if(n  

当在我自己的机器运行以上代码时,发现最多能打印到998,然后就会抛出 “RuntimeError: maximum recursion depth exceeded” 的错误了。 嘿,还真有限制。但转念一想,python不会这么弱吧。经过一番查找,发现这是python专门设置的一种机制用来防止无限递归造成Python溢出崩溃, 最大递归次数是可以重新调整的。 (http://docs.python.org/2/library/sys.html#sys.setrecursionlimit),修改代码如下:

import sys
sys.setrecursionlimit(1500)  # set the maximum depth as 1500
  
def recursion(n):
    if(n  

查看更多关于在python里递归最多达到多少次的详细内容...

  阅读:46次