Python RecursionError: Causes and Limit Configuration
In Python, a RecursionError occurs when a program
exceeds the interpreter's maximum call stack depth, most commonly caused
by infinite recursion or deeply nested operations. This article covers
why the RecursionError: maximum recursion depth exceeded
exception is triggered, how to inspect and modify Python's built-in
recursion limit using the sys module, and the safest
practices for handling deep recursion.
What Causes a RecursionError?
A RecursionError is raised by the Python interpreter as
a protective measure to prevent a stack overflow in the underlying C
runtime. There are two primary causes:
1. Missing or Faulty Base Case
The most common cause is a recursive function that fails to reach an exit condition, resulting in an infinite loop:
def countdown(n):
# Missing base case: if n <= 0: return
return countdown(n - 1)
countdown(5)
# Raises: RecursionError: maximum recursion depth exceeded2. Valid Recursion That Exceeds the Default Stack Depth
Even with a correct base case, algorithms processing deeply nested structures (such as large trees, graphs, or deep JSON payloads) will trigger this error if the call stack grows beyond Python's limit:
def sum_to_n(n):
if n == 1:
return 1
return n + sum_to_n(n - 1)
sum_to_n(1500) # Exceeds the standard default depth of 1000How the Recursion Limit Is Configured
Python provides tools to inspect and adjust the call stack limit
through the standard sys module. By default, the recursion
limit is typically set to 1000.
Checking the Current Limit
You can check the current recursion depth limit using
sys.getrecursionlimit():
import sys
current_limit = sys.getrecursionlimit()
print(f"Current recursion limit: {current_limit}")Changing the Recursion Limit
You can increase or decrease the limit using
sys.setrecursionlimit():
import sys
# Increase the limit to 3000
sys.setrecursionlimit(3000)
print(f"New recursion limit: {sys.getrecursionlimit()}")Risks and Best Practices
While raising the recursion limit with
sys.setrecursionlimit() is simple, it carries risks and is
rarely the optimal solution.
- Stack Overflow and Crashes: Python's default limit exists because the operating system's C stack is limited. Increasing the Python limit too high can cause a segmentation fault, instantly crashing the Python process without raising a catchable exception.
- Refactoring to Iteration: The recommended fix for
recursion depth issues is to rewrite the recursive algorithm into an
iterative one using a
whileorforloop, often paired with an explicit list as a stack:
def sum_to_n_iterative(n):
total = 0
for i in range(1, n + 1):
total += i
return total- Tail-Call Elimination: Python does not optimize tail calls. A function cannot rely on tail-call optimization to prevent stack growth, making iterative designs necessary for large data processing.