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

前端 未结 17 2876
佛祖请我去吃肉
佛祖请我去吃肉 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 04:53

    If you want to get only few Fibonacci numbers, you can use matrix method.

    from numpy import matrix
    
    def fib(n):
        return (matrix('0 1; 1 1', dtype='object') ** n).item(1)
    

    It's fast as numpy uses fast exponentiation algorithm. You get answer in O(log n). And it's better than Binet's formula because it uses only integers. But if you want all Fibonacci numbers up to n, then it's better to do it by memorisation.

    0 讨论(0)
  • 2020-11-21 04:53

    I wanted to give you an example for using memoization to compute Fibonacci as this will allow you to compute significantly larger numbers using recursion:

    cache = {}
    def fib_dp(n):
        if n in cache:
            return cache[n]
        if n == 0: return 0
        elif n == 1: return 1
        else:
            value = fib_dp(n-1) + fib_dp(n-2)
        cache[n] = value
        return value
    
    print(fib_dp(998))
    

    This is still recursive, but uses a simple hashtable that allows the reuse of previously calculated Fibonacci numbers instead of doing them again.

    0 讨论(0)
  • 2020-11-21 04:54

    It's to avoid a stack overflow. The Python interpreter limits the depths of recursion to help you avoid infinite recursions, resulting in stack overflows. Try increasing the recursion limit (sys.setrecursionlimit) or re-writing your code without recursion.

    From the Python documentation:

    sys.getrecursionlimit()

    Return the current value of the recursion limit, the maximum depth of the Python interpreter stack. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python. It can be set by setrecursionlimit().

    0 讨论(0)
  • 2020-11-21 04:55

    I had a similar issue with the error "Max recursion depth exceeded". I discovered the error was being triggered by a corrupt file in the directory I was looping over with os.walk. If you have trouble solving this issue and you are working with file paths, be sure to narrow it down, as it might be a corrupt file.

    0 讨论(0)
  • 2020-11-21 04:57

    As @alex suggested, you could use a generator function to do this sequentially instead of recursively.

    Here's the equivalent of the code in your question:

    def fib(n):
        def fibseq(n):
            """ Iteratively return the first n Fibonacci numbers, starting from 0. """
            a, b = 0, 1
            for _ in xrange(n):
                yield a
                a, b = b, a + b
    
        return sum(v for v in fibseq(n))
    
    print format(fib(100000), ',d')  # -> no recursion depth error
    
    0 讨论(0)
  • 2020-11-21 04:59

    Use a language that guarantees tail-call optimisation. Or use iteration. Alternatively, get cute with decorators.

    0 讨论(0)
提交回复
热议问题