Python RuntimeError: maximum recursion depth exceeded in cmp 超过最大递归深度错误

来源:互联网 发布:力士乐驱动器调试软件 编辑:程序博客网 时间:2024/06/16 04:51

说明:Python运行版本Python2.7.12

最近写了个Python爬虫,运行时出现 RuntimeError: maximum recursion depth exceeded in cmp 错误,上网查资料发现Python遍历深度在最大1000,超过就会报错!

写个程序测试下:

#coding=utf-8import sysdef func(depth):    depth += 1    print "Now the depth is %d" % depth    func(depth)if __name__ == "__main__" :    func(0)

结果:




可以看到当递归深度超过999达到1000的时候,引发了这个异常 ,解决的方式是手工设置递归调用深度,方式为:

import sys

sys.setrecursionlimit(1000000)#设置为一百万


不过,最好优化程序,这是临时解决办法!

0 0