该sys模块提供对解释器使用或维护的某些变量以及与解释器强烈交互的功能的访问。它提供有关python解释器的常量,函数和方法的信息。它可以用于操纵Python运行时环境。
sys.setrecursionlimit()方法用于将Python解释器堆栈的最大深度设置为所需的限制。此限制可防止任何程序进入无限递归,否则无限递归将导致C堆栈溢出并使Python崩溃。
注意:可能的最高限制是platform-dependent。这应该小心进行,因为too-high限制可能会导致崩溃。
用法: sys.setrecursionlimit(limit)
参数:
limit:它是整数类型的值,表示python解释器堆栈的新限制。
返回值:此方法不返回任何内容。
示例1:
# Python program to explain sys.setrecursionlimit() method
# Importing sys module
import sys
# Using sys.getrecursionlimit() method
# to find the current recursion limit
limit = sys.getrecursionlimit()
# Print the current limit
print('Before changing, limit of stack =', limit)
# New limit
Newlimit = 500
# Using sys.setrecursionlimit() method
sys.setrecursionlimit(Newlimit)
# Using sys.getrecursionlimit() method
# to find the current recursion limit
limit = sys.getrecursionlimit()
# Print the current limit
print('After changing, limit of stack =', limit)
输出:
Before changing, limit of stack = 1000
After changing, limit of stack = 500