RecursionError in Python: Causes and Fixes with Simple Examples

 What is RecursionError in Python?

A RecursionError in Python occurs when a function calls itself too many times without stopping properly.

In simple words, Python is saying:

“This function is calling itself endlessly.”

Python limits recursion depth to prevent the program from crashing due to excessive memory usage.


Common RecursionError Message


RecursionError: maximum recursion depth exceeded

Understanding Recursion in Python

Recursion means a function calls itself to solve a problem.

A recursive function must have:

  1. Base case – stops recursion

  2. Recursive case – function calls itself

Without a base case, recursion becomes infinite.


Example 1: Infinite Recursion (No Base Case)

❌ Incorrect Code


def greet():
    print("Hello")
    greet()

greet()

❌ Error


RecursionError: maximum recursion depth exceeded

✔ Why this happens

  • Function keeps calling itself

  • No stopping condition


Example 2: Missing Base Case Condition

❌ Incorrect Code


def countdown(n):
    print(n)
    countdown(n - 1)

countdown(5)

❌ Error


RecursionError

Example 3: Correct Recursion with Base Case

✔ Correct Code


def countdown(n):
    if n == 0:
        return
    print(n)
    countdown(n - 1)

countdown(5)

✔ Recursion stops correctly
✔ No error occurs


Example 4: RecursionError with Wrong Condition

❌ Incorrect Code


def test(n):
    if n < 0:
        return
    test(n + 1)

test(1)

❌ Error


RecursionError

✔ Reason

  • n keeps increasing

  • Base condition never reached


Example 5: Handling RecursionError with try–except


def recurse():
    recurse()

try:
    recurse()
except RecursionError:
    print("Recursion limit exceeded")

✔ Prevents program crash
✔ Useful for debugging


Python Recursion Limit

Python sets a default recursion limit (usually 1000).

You can check it using:


import sys
print(sys.getrecursionlimit())

Increasing Recursion Limit (Use Carefully)


import sys
sys.setrecursionlimit(2000)

⚠ Increasing limit can cause memory issues
⚠ Use only if necessary


How to Avoid RecursionError

✔ Always define a base case
✔ Ensure recursion moves toward base case
✔ Avoid infinite recursion
✔ Use loops when possible
✔ Test with small inputs


Recursion vs Loop

RecursionLoop
Calls itselfRepeats using loop
Can cause RecursionErrorNo recursion limit
Cleaner for some problemsSafer for large inputs

Summary

A RecursionError occurs when a recursive function exceeds Python’s recursion limit.

By defining proper base cases and testing your logic carefully, recursion becomes safe and powerful.


Frequently Asked Questions (FAQ)

Q1: Is RecursionError common?
Yes, for beginners learning recursion.

Q2: Can we increase recursion limit?
Yes, but it’s risky.

Q3: Is recursion better than loops?
Depends on the problem.


📌 Final Tip

If recursion goes too deep, consider converting it into a loop.

Comments

Popular posts from this blog

AttributeError in Python: Causes and Fixes with Simple Examples

SyntaxError in Python: Causes and Fixes with Simple Examples

SystemExit in Python: Causes and Fixes with Simple Examples