0

The default recursion limit in python in 3000 as per the below code:

import sys
sys.getrecursionlimit()

So when I run the following code, why is it printing 2954 as the last value before RecursionError and not 3000?

def increment(num):
    print(num, end=" ")
    
    increment(num+1)

Last value should have been 3000.

  • 2
    Perhaps there are (about) 46 other functions above the first call to `increment()` on the stack? – Jeremy Friesner Feb 21 '23 at 02:35
  • 1
    Maybe the function was called with an initial value of 45? Hard to say, because you haven't shown us the full code. – John Gordon Feb 21 '23 at 02:36
  • 1
    In the end, it doesn't really matter. If you're going that deep, you've done something wrong. – Tim Roberts Feb 21 '23 at 05:04
  • Most languages don't tell you the recursion limit, because it's system- and implementation-specific. It's an artbitrary number to help safeguard against crashing the interpreter. Don't rely on it and stay well below it. If you have an actual problem, like a recursive algorithm failing, it's better to [ask about that](https://meta.stackexchange.com/a/233676/399876) rather than your workaround attempt. That way, others can guide you to, for example, rewrite it iteratively, or fix a bug. – ggorlen Feb 21 '23 at 17:23

0 Answers0