1

I am testing the recursion limit with

In [19]: ??foo          
Signature: foo()
Docstring: <no docstring>
Source:   
def foo():
    global count
    count += 1
    print(count)
    return foo()
File:      ~/<ipython-input-6-9b0629ed4ce8>
Type:      function

Then run

count = 0; foo()

and got the error:

2979
2980
2981
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-20-7b6881a8dc94> in <module>
----> 1 count = 0; foo()

<ipython-input-6-9b0629ed4ce8> in foo()
      3     count += 1
      4     print(count)
----> 5     return foo()
      6 
      7 

... last 1 frames repeated, from the frame below ...

<ipython-input-6-9b0629ed4ce8> in foo()
      3     count += 1
      4     print(count)
----> 5     return foo()
      6 
      7 

RecursionError: maximum recursion depth exceeded while calling a Python object

the recursion limit is

In [22]: sys.getrecursionlimit()                
Out[22]: 3000

Since the recursion limit is 3000, why it always stops at 2981?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • Seems to be a protection mechanism against stack overflow, see [what is the maximum recursion depth in python](https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it) – ldz Dec 03 '19 at 11:34
  • recursion depth is a bit of a misnomer, it's the stack depth. You're always a few frames in, but more-so in an Ipython session because Ipython does a lot of stuff underneath the hood. – juanpa.arrivillaga Dec 03 '19 at 12:49

0 Answers0