Ad Space — Top Banner

RecursionError

Python Programming Language

Severity: Critical

What Does This Error Mean?

A RecursionError means a function called itself so many times that Python ran out of space to track all the calls. This usually means the function has an infinite recursion bug — it keeps calling itself and never stops. Python has a built-in limit (usually 1000 levels deep) to prevent this from crashing the entire computer.

Affected Models

  • Python 3.x
  • Python 2.x (as RuntimeError)

Common Causes

  • A recursive function is missing its base case — the condition that stops the recursion
  • The base case exists but the input never actually reaches it (the logic is wrong)
  • Two functions calling each other in a circle — function A calls B, B calls A, indefinitely
  • Processing deeply nested data (like a very deep JSON tree) that exceeds the recursion limit
  • Accidentally calling the function itself when you meant to call a different function with a similar name

How to Fix It

  1. Find the recursive function — the one that calls itself. Check that it has a base case: a condition that stops the recursion and returns a value without calling itself again.

    Example of a correct base case: 'if n <= 0: return 0'. Without this, the function runs forever.

  2. Trace through the function manually with a small input. Does every call bring you closer to the base case? If not, fix the logic so each recursive call uses a smaller or simpler input.

    Each call should make progress toward the stopping condition.

  3. Add a print statement inside the function to see what values are being passed on each call. This reveals whether the input is actually getting smaller.

    Example: print(f'Called with n={n}'). If you see the same value repeating, the function is stuck.

  4. If the recursion is legitimately very deep (processing large data), consider rewriting the function as a loop (iterative approach). Loops do not have a depth limit.

    Most recursive solutions can be rewritten using a while loop and a stack (a list used as LIFO storage).

  5. As a temporary workaround, you can increase the recursion limit with sys.setrecursionlimit(). But this is not a real fix — it just delays the crash. Fix the logic instead.

    import sys; sys.setrecursionlimit(5000). Use this only for testing, not production code.

When to Call a Professional

RecursionErrors are always something you can fix yourself. Carefully trace through your recursive function to make sure the base case is correct and reachable. If the data is legitimately very deep, consider rewriting the function iteratively using a loop instead.

Frequently Asked Questions

What is recursion and why is it useful?

Recursion is when a function solves a problem by calling a smaller version of itself. For example, to calculate 5! (factorial), you can say: 5! = 5 × 4!. Then 4! = 4 × 3!. And so on until you reach 1! = 1. Recursion is elegant for problems that have a naturally self-similar structure, like tree traversal or sorting algorithms.

Why does Python have a recursion limit at all?

Each function call uses a small amount of memory called a stack frame. If a function calls itself millions of times, it would use all available memory and crash the entire Python process. Python's limit (default 1000) stops this from happening and gives you an error message instead of a silent crash.

Is it better to use loops or recursion?

For most practical tasks, loops are safer and faster in Python because they do not have a depth limit. Recursion is cleaner for certain algorithms (like tree traversal) but can be slower due to function call overhead. If you are new to programming, start with loops — they are easier to debug.