Python etc / sys.setrecursionlimit

Published: 23 June 2020, 18:00

Python doesn’t support tail recursion. Hence, it’s easy to face RecursionError when implementing recursive algorithms. You can get and change maximum recursion depth with sys.getrecursionlimit and sys.setrecursionlimit functions:

sys.getrecursionlimit()
# 3000

sys.setrecursionlimit(4000)
sys.getrecursionlimit()
# 4000

However, it’s a dangerous practice, especially because every new frame on the call stack is quite expensive. Luckily, any recursive algorithm can be rewritten with iterations.