Recursion is a process by which a function calls itself, directly or indirectly. Every time a function is invoked, a new frame is added to the stack.
To avoid infinite recursion leading to the overflowing of the stack, every programming language has a recursion limit. This limit varies is programming languages and is platform dependent.
sys
moduleThe sys
module provides access to the system-specific parameters and functions that interact and are maintained by the Python interpreter.
setrecursionlimit()
methodThe setrecursionlimit()
method of the sys
module sets the maximum recursion depth of the Python interpreter.
Note: Depending on the program and platform, we can set a higher recursion limit. However, very high limits can cause the interpreter to crash.
This method raises a RecursionError
when the given limit is too low at the current recursion depth.
setrecursionlimit(limit)
limit
: This is the new value of the recursion limit.import sysplatform = sys.platformrecur_limit = sys.getrecursionlimit()print("The recursion limit for %s platform is %s" % (platform, recur_limit))sys.setrecursionlimit(2000)new_recur_limit = sys.getrecursionlimit()print("The new recursion limit for %s platform is %s" % (platform, new_recur_limit))
sys
module.sys.platform
constant.sys.getrecursionlimit()
method.2000
.sys.getrecursionlimit()
method.