Python Glossary Русский

all Python 3sysfunction

sys.setrecursionlimit()

recursion

Set the maximum recursion depth; too high a value can crash the process with a stack overflow — the limit is a safeguard, not a hard guarantee.

Syntax

sys.setrecursionlimit(limit: int) -> None

Raising the limit does not fix runaway recursion — the RecursionError merely arrives later after eating more memory, and on some platforms a too-high limit can crash the whole process on stack overflow, with no traceback. Lowering it below the current depth fails as well: the call itself raises RecursionError. If an algorithm genuinely needs tens of thousands of levels, rewrite it iteratively with an explicit stack instead of tuning the limit.

Examples

import sys
sys.setrecursionlimit(5000)
print(sys.getrecursionlimit())  # → 5000
sys.setrecursionlimit(1000)  # → возврат к стандарту

See also

Open in the full glossarydocs.python.orgReport an inaccuracy