What is the maximum recursion depth in Python, and how to increase it?

前端 未结 17 2891
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:14

I have this tail recursive function here:

def recursive_function(n, sum):
    if n < 1:
        return sum
    else:
        return recursive_function(n-1         


        
17条回答
  •  無奈伤痛
    2020-11-21 05:01

    Many recommend that increasing recursion limit is a good solution however it is not because there will be always limit. Instead use an iterative solution.

    def fib(n):
        a,b = 1,1
        for i in range(n-1):
            a,b = b,a+b
        return a
    print fib(5)
    

提交回复
热议问题