在Python中,确定一个函数是否调用另一个函数 [英] In Python, determine if a function calls another function
问题描述
如何以编程方式确定一个函数是否调用另一个函数?我不能修改任何一个功能.
How can I determine programmatically if one function calls another function? I cannot modify either function.
这就是我想要的(source_calls_target
):
>>> def check():
>>> pass
>>> def test_check():
>>> check()
>>> def source_calls_target(source, target):
>>> # if source() calls target() somewhere, return True
>>> ???
>>> source_calls_target(test_check, check)
True
>>> source_calls_target(check, test_check)
False
理想情况下,我不想实际呼叫target()
.
Ideally, I do not want to actually call target()
.
理想情况下,我想检查对target()
的调用是否出现在source
的定义内.它可能会或可能不会实际调用它,具体取决于条件语句.
Ideally, I want to check if a call to target()
appears within the definition for source
. It may or may not actually call it depending on conditional statements.
推荐答案
如果可以保证可以访问源代码,则可以使用ast.parse
:
If you can guarantee having access to the source code, you can use ast.parse
:
import ast
call_names = [c.func.id for c in ast.walk(ast.parse(inspect.getsource(source)))
if isinstance(c, ast.Call)]
return 'target' in call_names
请注意,调用始终都是按名称进行的,因此很难(而且可能是不可能)分辨出调用是针对特定功能还是相同名称的另一个功能.
Note that calls are always by name, so it's difficult (and potentially impossible) to tell whether a call is to a particular function or another of the same name.
在没有源代码的情况下,唯一的方法是通过反汇编:
In the absence of source code, the only way is via disassembly:
import dis
def ops(code):
i, n = 0, len(code)
while i < n:
op = ord(code[i])
i += 1
if op == dis.EXTENDED_ARG:
ext = ord(code[i]) + ord(code[i+1])*256
op = ord(code[i + 2])
i += 3
else:
ext = 0
if op >= dis.HAVE_ARGUMENT:
arg = ord(code[i]) + ord(code[i+1])*256 + ext*65536
i += 2
yield op, arg
else:
yield op, None
source_ops = list(ops(source.func_code.co_code))
问题在于,实际上不可能分辨一个函数是调用还是只是加载对该函数的引用;如果将另一个函数传递给map
或reduce
等,则将调用它,但可能不会传递给另一个函数.实际上,明智的做法是假设如果函数位于source.func_code.co_names
中,则可以将其称为:
The problem is that it's in practice impossible to tell whether a function is calling another function or just loading a reference to it; if the other function is passed to map
or reduce
etc. then it will be called but passed to another function it might not be. Practically the sensible thing is to assume that if the function is in source.func_code.co_names
then it might be called:
'target' in source.func_code.co_names
这篇关于在Python中,确定一个函数是否调用另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!