Why does Python have a recursion limit?

To stop infinite recursion from overflowing the stack and crashing Python.

Start playing free

  1. To make programs run faster
  2. To stop runaway recursion from crashing Python ✔
  3. Because recursion is not allowed in loops

Why: Every function call uses a frame of stack memory. If a function calls itself forever, the stack would overflow and crash the whole interpreter, so Python raises a RecursionError first.

Huh, didn't know that: You can read the limit with sys.getrecursionlimit() and change it with sys.setrecursionlimit(), but too high a limit can crash Python.

The dad joke
My recursive joke would have been hilarious, but Python stopped it at the limit.
Like this card?
Play the game

References

Answer: To stop runaway recursion from crashing Python. Every function call uses a frame of stack memory. If a function calls itself forever, the stack would overflow and crash the whole interpreter, so Python raises a RecursionError first.

  1. Python docs: sys module docs.python.org/3/library/sys.html
    This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.
    Checked 2026-10-10.

Think this answer is wrong? Tap "Challenge this answer" on the card in the game and tell us why.

Test yourself in the game