Python:获取导入函数的抽象语法树? [英] python: get the abstract syntax tree of imported function?

查看:234
本文介绍了Python:获取导入函数的抽象语法树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经在解释器中导入了python模块。如何在解释器中获取导入模块(及其中的任何函数和类)的抽象语法树?我不想重新解析源文件。谢谢!

Let's say I've already imported a python module in the interpreter. How can I get the abstract syntax tree of the imported module (and any functions and classes within it) within the interpreter? I don't want to have to re-parse the source files. Thanks!

推荐答案

也许您会在此食谱中找到一些启发:

Maybe you find some inspiration in this recipe:

  • http://code.activestate.com/recipes/533146-ast-pretty-printer/

输出人类的函数可读版本的Python AST。

A function that outputs a human-readable version of a Python AST.

或使用编译器检查(当然,仍使用源代码):

Or use compiler combined with inspect (which, of course, still uses the source):

>>> import compiler, inspect
>>> import re # for testing 
>>> compiler.parse(inspect.getsource(re))
Module('Support for regular expressions (RE). \n\nThis module provides ...

这篇关于Python:获取导入函数的抽象语法树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆