python递归

 

def story():
  print('从前有座山')
story()
print(111)

story()

RecursionError: maximum recursion depth exceeded while calling a Python object
递归的错误,超过了递归的最大深度

import sys
sys.setrecursionlimit(1000000) #自定义递归数量,根据配置的情况,默认是不超过1000次
n = 0
def story():
  global n
  n += 1
  print(n)
  story()
story()

如果递归次数太多,就不适合使用递归来解决问题
递归的缺点 : 占内存
递归的优点: 会让代码变简单

猜你喜欢

转载自www.cnblogs.com/daoyueweiku/p/9729040.html