Recursion limit
suggest changeThere is a limit to the depth of possible recursion, which depends on the Python implementation. When the limit is reached, Python raises RecursionError (a subclass of RuntimeError since Python 3.5):
def cursing(depth):
try:
cursing(depth + 1) # actually, re-cursing
except RecursionError:
print('I recursed {} times!'.format(depth))
cursing(0)
# Out: I recursed 998 times!
The exact count is a little below sys.getrecursionlimit() (1000 by default) because frames are already on the stack. except RuntimeError also catches RecursionError.
It is possible to change the recursion depth limit by using sys.setrecursionlimit(limit) and check this limit by sys.getrecursionlimit().
import sys
sys.setrecursionlimit(2000)
cursing(0)
# Out: I recursed 1998 times!
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents