如何在Python

时间:2016-06-06 15:53:49

标签: python python-3.x parsing abstract-syntax-tree

我对查找(以行和列位置和值的形式)感兴趣在Python源文件中调用特定函数的所有引用

我调查了ast module和这些docs,但我还没有找到实现目标的方法。我所能做的就是找到函数定义和每个调用的节点。

有没有人知道如何获得特定功能的引用?理想情况下,我也想为做同样的事情。

1 个答案:

答案 0 :(得分:2)

<`# this should get you the functions import ast  

def get_function(node):  
    for stmt in node.body:  
        if isinstance(stmt, ast.FunctionDef):  
            print('This is a Function: {}'.format(stmt.name))  
        elif isinstance(node, ast.ClassDef):  
            print('This is a Class: {}'.format(stmt.name))  

if __name__ == '__main__()':  
    with open('/examples/filename.py').read() as data:  
        tree = ast.parse(data)  
        get_function(tree)  

#I believe this should help.&GT;

相关问题