8

So I was trying to play with sys.getrecursionlimit() and sys.setrecursionlimit() methods. By default recursion limit was 3000.

I tried to check it using this code:

def recursive(n):
    print(n)
    recursive(n+1)
recursive(0)

It does print the numbers to 2979, it delays for a second, prints 2980 and then raises the RecursionError

RecursionError: maximum recursion depth exceeded while calling a Python object

I know that error should be raised when it exceeds recursion limit that sys.getrecursionlimit() returns, but it doesn't

Seems like it always doing it 20times before the recursion limit

I also tried this:

sys.setrecursionlimit(100)
def recursive(n):
    print(n)
    recursive(n+1)
recursive(0)

It still does the same thing, prints all the numbers to 79, delays for a second, prints 80 and then raises the very same error

Why it does raise the error before it exceeds the real recursion limit that we set or get by sys.getrecursionlimit() ?

DarkSuniuM
  • 2,523
  • 2
  • 26
  • 43

3 Answers3

8

Your recursive() function is not the only component that counts towards the limit. Some Python internals also increase the counter, because Python code can cause them to be called multiple times too. The print() function is one of them.

Bottom line is that the recursion limit doesn't only apply to the Python functions you wrote. It applies to the whole call stack.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
5

There's stuff on the stack before you called your function.

In this case, it seems the call stack was already 20 calls deep before you called recursive.

See here for how to analyze the stack if you're really curious what's making up those 20 calls.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • What kinda stuff are we talking about? Are they some other static recursive methods? Cause it always do it 20 times before the real number – DarkSuniuM Apr 07 '19 at 14:55
  • 1
    @DarkSuniuM It could be stuff your environment/IDE has called, or something else. You could print out the Stack and analyze the bottom if you're really curious. – Carcigenicate Apr 07 '19 at 14:57
3

Actually, the recursive function "ALONE" is invoked exactly max=3000 number of times as long as you remove the "print" function. This is because python built-in functions also contribute to the limit.

import sys
count = 0
def recursive(n):
    global count
    count = count + 1
    recursive(n+1)


sys.setrecursionlimit(3000)
try:
    recursive(0)
except:
    print count  # would print 2999
Soheil__K
  • 642
  • 1
  • 8
  • 17
  • That explains more, Still your code stops at 2984 for me :D Thanks anyway, according to the link @Carcigenicate sent, It's right – DarkSuniuM Apr 07 '19 at 15:06