Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: get the abstract syntax tree of imported function?

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!

like image 532
Heinrich Schmetterling Avatar asked Jan 27 '11 02:01

Heinrich Schmetterling


1 Answers

Maybe you find some inspiration in this recipe:

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

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 ...
like image 175
miku Avatar answered Nov 15 '22 18:11

miku