0

I'm not sure if this is the right place to ask, but I was messing with recursive functions, and noticed that while attempting recursion 997 my script returned a recursion error. The script I wrote was the following,

def main(x):
    print(x)
    x += 1
    return main(x)

main(0)

After looking about online I opened the interpreter(?) in the powershell and tried sys.getrecursionlimit() and it returned 1000, I tried adding the same thing to the above script and it also returned 1000. So why did the error pop up before 1000 recursions? Did I write the function incorrectly or is there something else going on? If it helps, this is the full error message I got,

...
990
991
992
993
994
995
996
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    main(0)
  File "test.py", line 4, in main
    return main(x)
  File "test.py", line 4, in main
    return main(x)
  File "test.py", line 4, in main
    return main(x)
  [Previous line repeated 992 more times]
  File "test.py", line 2, in main
    print(x)
RecursionError: maximum recursion depth exceeded while calling a Python object

Thanks!

  • Your recursive function has no base case. If the stack limit was higher it would keep going above 997 – rdas Nov 18 '21 at 15:02
  • Well in this case I purposefully wrote the program to not have anything that could stop it, just to see how python would handle an infinitely recursive function. I'm just wondering if the stack limit is 1000, why did it stop at 997 recursions? Does it start out with three extra stacks? – dancingvulture Nov 18 '21 at 15:11
  • Does this answer your question? [Why Python raises RecursionError before it exceeds the real recursion limit?](https://stackoverflow.com/questions/55560258/why-python-raises-recursionerror-before-it-exceeds-the-real-recursion-limit) – ggorlen Feb 21 '23 at 17:40

1 Answers1

2

The recursion limit isn’t just the limit of recursive calls, it’s the limit on any nested calls — or, more precisely, the maximum number of stack frames. Here’s what the documentation says:

Return the current value of the recursion limit, the maximum depth of the Python interpreter stack.

In your case, your initial main call also counts, and so does the call to print. That’s why the RecursionError is raised in print. This brings us up to 999. Add the stack frame of the context from which main is called initially and now you’re at 1000.

Here’s a program which counts stack frames more accurately; we start at 1 (for the current stack frame), and add 1 for each call:

def main(n_frames):
    print(n_frames + 1) # + 1 for `print` call
    main(n_frames + 1)

n_frames = 1
main(n_frames + 1)

Incidentally, when I run this program (or yours), my program stops even earlier. This might be because Python inserts some additional frames due to bookkeeping. The value of sys.getrecursionlimit() isn’t intended as an absolutely precise limit that your code can depend on, it’s approximate and purely informative.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks for the response! If I'm understanding you correctly, 997 of the stack frames are from the recursion, one frame is from when `main` was initially called at the start of the script, another frame is for the current call of `print`, and the third frame is from the current call of `main`? – dancingvulture Nov 18 '21 at 15:34
  • @dancingvulture Precisely. – Konrad Rudolph Nov 18 '21 at 15:42
  • Thanks for all of the information! Your explanation was very helpful. – dancingvulture Nov 18 '21 at 15:49