在Python中,确定一个函数是否调用另一个函数

编程入门 行业动态 更新时间:2024-10-28 14:25:00
本文介绍了在Python中,确定一个函数是否调用另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何以编程方式确定一个函数是否调用另一个函数?我不能修改任何一个功能.

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中,确定一个函数是否调用另一个函数

本文发布于:2023-11-29 00:04:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   一个函数   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!