Python无限递归的概念教程

admin 2014年5月13日03:43:57Python评论1,401 次浏览阅读模式


Python程序调用自身的这种方法叫做递归,如果达不到我们需要的条件,而它会永远的继续递归调用下去,而程序也会永远不停止,这种现象叫做Python的无限递归。

下面安云网(www.AnYun.ORG)给大家写一个会引起无限递归的简单函数:

def test():
    test()

很多程序语言中,无限递归的函数方法并不会真正的无休止的运行下去,它们都有一个深度限制,Python编程语言会在递归深度到达上限时,引发一个异常的错误信息:

>>> test()

Traceback (most recent call last):
  File "pyshell#3", line 1, in module
    test()
  File "pyshell#2", line 2, in test
    test()
  File "pyshell#2", line 2, in test
    test()
  File "pyshell#2", line 2, in test
    test()
  File "pyshell#2", line 2, in test
    test()
RuntimeError: maximum recursion depth exceeded

这个调用回溯信息显示了它的错误类型和详细信息,这个版本的Python无限递归最大值为 1000,各版本限制值不太相同。

相关文章
weinxin
我的微信
微信扫一扫
admin
  • 本文由 发表于 2014年5月13日03:43:57
  • 转载请务必保留本文链接:https://anyun.org/a/jishuguanzhu/bianchengchalou/python/2014/0513/1032.html

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: