当通过解包参数调用函数时,递归深度似乎增加了两倍。我想知道为什么会发生这种情况。
通常:
depth = 0
def f():
global depth
depth += 1
f()
try:
f()
except RuntimeError:
print(depth)
#>>> 999
通过拆包调用:
depth = 0
def f():
global depth
depth += 1
f(*())
try:
f()
except RuntimeError:
print(depth)
#>>> 500
理论上两者都应该达到1000左右:
import sys
sys.getrecursionlimit()
#>>> 1000
这发生在 CPython 2.7 和 CPython 3.3 上。
在 PyPy 2.7 和 PyPy 3.3 上存在差异,但要小得多(1480 与 1395 和 1526 与 1395)。
从反汇编中可以看出,除了调用的类型(CALL_FUNCTION
vs CALL_FUNCTION_VAR
)之外,两者几乎没有区别:
import dis
def f():
f()
dis.dis(f)
#>>> 34 0 LOAD_GLOBAL 0 (f)
#>>> 3 CALL_FUNCTION 0 (0 positional, 0 keyword pair)
#>>> 6 POP_TOP
#>>> 7 LOAD_CONST 0 (None)
#>>> 10 RETURN_VALUE
def f():
f(*())
dis.dis(f)
#>>> 47 0 LOAD_GLOBAL 0 (f)
#>>> 3 BUILD_TUPLE 0
#>>> 6 CALL_FUNCTION_VAR 0 (0 positional, 0 keyword pair)
#>>> 9 POP_TOP
#>>> 10 LOAD_CONST 0 (None)
#>>> 13 RETURN_VALUE
最佳答案
message 异常实际上为您提供了提示。比较非解包选项:
>>> import sys
>>> sys.setrecursionlimit(4) # to get there faster
>>> def f(): f()
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in f
File "<stdin>", line 1, in f
File "<stdin>", line 1, in f
RuntimeError: maximum recursion depth exceeded
与:
>>> def f(): f(*())
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in f
File "<stdin>", line 1, in f
RuntimeError: maximum recursion depth exceeded while calling a Python object
注意在调用 Python 对象时添加的 。此异常特定于
PyObject_CallObject()
function .当您设置 odd 递归限制时,您不会看到此异常:
>>> sys.setrecursionlimit(5)
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in f
File "<stdin>", line 1, in f
RuntimeError: maximum recursion depth exceeded
因为这是 ceval.c
frame evaluation code 中提出的特定异常。 PyEval_EvalFrameEx()
内:
/* push frame */
if (Py_EnterRecursiveCall(""))
return NULL;
注意那里的空消息。这是一个至关重要的区别。
对于您的“常规”函数(无变量参数),会选择一个优化路径;不需要元组或关键字参数解包支持的 Python 函数直接在 fast_function()
function 中处理。的评估循环。创建并运行具有函数的 Python 字节码对象的新框架对象。这是一个递归检查。
但是对于带有可变参数(元组或字典或两者)的函数调用,不能使用 fast_function()
调用。相反,ext_do_call()
(extended call)使用,它处理参数解包,然后使用 PyObject_Call()
调用函数。 PyObject_Call()
进行递归限制检查,并“调用”函数对象。函数对象通过 function_call()
function 调用。 , 调用 PyEval_EvalCodeEx()
, 调用 PyEval_EvalFrameEx()
,这使得 second 递归限制检查。
TL;DR 版本
调用 Python 函数的 Python 函数经过优化并绕过 PyObject_Call()
C-API 函数,除非 发生参数解包。 Python 帧执行和 PyObject_Call()
都会进行递归限制测试,因此绕过 PyObject_Call()
可以避免增加每次调用的递归限制检查。
更多具有“额外”递归深度检查的地方
您可以 grep Py_EnterRecursiveCall
的 Python 源代码以查找进行递归深度检查的其他位置;各种库,例如 json
和 pickle
使用它来避免解析嵌套太深或递归的结构,例如。其他检查放在 list
和 tuple
__repr__
实现中,丰富的比较(__gt__
、__lt__
、__eq__
等),处理 __call__
可调用对象 Hook 并处理 __str__
调用。
因此,您可以更快地达到递归限制:
>>> class C:
... def __str__(self):
... global depth
... depth += 1
... return self()
... def __call__(self):
... global depth
... depth += 1
... return str(self)
...
>>> depth = 0
>>> sys.setrecursionlimit(10)
>>> C()()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in __call__
File "<stdin>", line 5, in __str__
RuntimeError: maximum recursion depth exceeded while calling a Python object
>>> depth
2
关于python - 参数拆包浪费堆栈帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23879319/