Why does Python have a recursion limit?
To stop infinite recursion from overflowing the stack and crashing Python.
- To make programs run faster
- To stop runaway recursion from crashing Python ✔
- 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.
My recursive joke would have been hilarious, but Python stopped it at the limit.
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.
- 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.
More stuff everyone should know
That's stuff everyone should know.