且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:获取导入函数的抽象语法树?

更新时间:2022-06-01 01:37:11

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

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 ...