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

前端 未结 17 2923
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  -上瘾入骨i
    2020-11-21 05:11

    Use generators?

    def fib():
        a, b = 0, 1
        while True:
            yield a
            a, b = b, a + b
    
    fibs = fib() #seems to be the only way to get the following line to work is to
                 #assign the infinite generator to a variable
    
    f = [fibs.next() for x in xrange(1001)]
    
    for num in f:
            print num
    

    above fib() function adapted from: http://intermediatepythonista.com/python-generators

提交回复
热议问题