5

I was curious about what the MRD (maximum recursion depth) is in Python, so I wrote this:

def call(n):
    print (n)
    return call(n+1)

call(1)

The end result was 979, which 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 appreciate it being explained in simple terms.

EDIT: apparently it's supposed to be a 1000, but why this number?

10
  • 3
    Duplicate: stackoverflow.com/questions/3323001/maximum-recursion-depth - Read this, it gives you the answer you want.. Commented Oct 18, 2016 at 18:40
  • 2
    He ask why its there, and i am asking why it is like that. I want to know why my result was 979, not why there is a MRD or how to get around it. Commented Oct 18, 2016 at 18:43
  • Have you read the docs? Commented Oct 18, 2016 at 18:47
  • 1
    Because there isn't a real "standard recursion limit" - it can be set by the implementation (and for CPython, which most people refer to as just Python, it is set to 1000 by default). Also it can be changed (which was originally part of your question) as the documentation describes Commented Oct 18, 2016 at 18:58
  • 7
    RuntimeError: maximum "why question" depth exceeded Commented Oct 18, 2016 at 19:28

1 Answer 1

5

Here is a better test:

n = 0

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

test_recursion_limit()

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

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.

>>> # freshly opened ipython session
>>> import inspect
>>> len(inspect.stack())
10

You can check the current recursion limit by calling sys.getrecursionlimit() 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 __getattr__ implementations, for example.

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

Sign up to request clarification or add additional context in comments.

5 Comments

I did as you instructed and it returned 994
So that explains why its not 1000, but why is recursion the limit 1000?
Why not? How often do you need to recurse deeper than 1000? I've used python for about a decade and have never needed to reconfigure sys.setrecursionlimit even once. Every time I hit the recursion limit it was my own fault or a bug.
So 1000 is more than enough?
Yeah, for most use-cases, 1000 is plenty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.