Why is the maximum recursion depth in python 1000?
<p>I was curious about what the MRD (maximum recursion depth) is in python, so i wrote this:</p>

<pre><code>def call(n):
    print (n)
    return call(n+1)

call(1)
</code></pre>

<p>The end result was 979, wich is a peculiar number for me. I could not find anywhere why this number is the standard. As i am a self taught programmer i would apreciate it being explained in simple terms.</p>

<p>EDIT: apperantly it's supposed to be a 1000, but why this number?</p>

<p>Here is a better test:</p>

<pre><code>n = 0

def test_recursion_limit():
    def call():
        global n
        n += 1
        call()
    try:
        call()
    except RuntimeError:
        print(n)

test_recursion_limit()
</code></pre>

<p>If you put it in <code>spam.py</code> and execute that, it should return 998 for both python2 and python3.  It's one stack frame short because of the initial <code>test_recursion_limit</code> frame.  </p>

<p>If you're running in a REPL such as ipython, you are already inside a few frames, so you will see a lower count - it's not that the recursion limit is undershot, it's that the implementation of the REPL itself uses some stack frames.  </p>

<pre><code>&gt;&gt;&gt; # freshly opened ipython session
&gt;&gt;&gt; import inspect
&gt;&gt;&gt; len(inspect.stack())
10
</code></pre>

<p>You can check the current recursion limit by calling <code>sys.getrecursionlimit()</code> function.  The default value of 1000 is chosen as a sensible default, it's a safeguard against eclipsing system resources when you accidentally execute an infinitely recursive call.  That's very easy to do when mucking around with custom <code>__getattr__</code> implementations, for example.  </p>

<p>If you're blowing the stack legitimately and you need to increase the limit, it can be modified with <code>sys.setrecursionlimit</code>.</p>

python