RecursionError in Python: How to Fix and When to Rewrite Iterative
Share
TL;DR: RecursionError indicates your call stack got too deep. Fix base cases and progress, then consider rewriting to loops or a manual stack for large inputs. You can raise the limit, but it is a temporary patch.
Why RecursionError happens
- No base case or the base case is not reached.
- Progress is not guaranteed, so the function cycles forever.
- Input size is large enough that even correct recursion overflows.
Quick checks
def sum_to(n):
if n <= 0:
return 0
return n + sum_to(n - 1)
Verify base case and that n actually decreases.
Temporary fix: increase recursion limit
import sys
sys.setrecursionlimit(10_000)
Only use this while refactoring. Large inputs are better handled iteratively.
Lasting fix: iterative rewrite
def sum_to_iter(n):
total = 0
while n > 0:
total += n
n -= 1
return total
DFS example: recursion to stack
def dfs_recursive(graph, start):
seen = set()
def visit(node):
if node in seen:
return
seen.add(node)
for nxt in graph.get(node, []):
visit(nxt)
visit(start)
return seen
from collections import deque
def dfs_stack(graph, start):
seen = set([start])
stack = [start]
while stack:
node = stack.pop()
for nxt in graph.get(node, []):
if nxt not in seen:
seen.add(nxt)
stack.append(nxt)
return seen
Memoization for expensive recursion
from functools import lru_cache
@lru_cache(maxsize=1024)
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
Testing large inputs
- Benchmark both versions and compare results.
- Fuzz test with random graphs or sequences.
- Add assertions about bounds and progress.
FAQ
Is increasing the recursion limit safe
Use it sparingly. It can cause crashes if you go too high. Prefer iterative solutions for large inputs.
How do I detect depth issues early
Log the depth, cap input size, or add guard conditions to stop early for suspicious inputs.
Cast this in your project
- RecursionError T-shirt for the loop tamers.
- Python Flashcards to drill clean recursion and iterations.
Image credit: Photo by Pixabay: https://www.pexels.com/photo/empty-corridor-60573/