[英]python code recursion: how many compilation you can get of number as sum, but i don't get how i can print this compilation
[英]Why i don't get the maximum recursion number in python?
try:
start = 0
def recursion():
global start
def repeat():
global start
print('hello world')
start += 1
print(recursion())
print(repeat())
print(recursion())
except RecursionError:
print('you eached the limit')
print(' ')
print('recursion :',start, 'times')
结果:
hello world
hello world
you eached the limit
recursion : 497 times
为什么我没有得到 python 中的最大递归数,或者这是最大数?
默认递归限制为 1000。
>>> sys.getrecursionlimit()
1000
具有单个递归 function 的程序将达到999
:
start = 0
try:
def recursion():
global start
start += 1
recursion()
recursion()
except RecursionError:
print('recursion :', start, 'times')
打印出来
recursion : 999 times
您的程序正在创建一个堆栈,该堆栈具有recursion()
,然后是repeat()
,然后是recursion()
等,以及一个print()
调用,因此您达到其中的一半以下是有道理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.