Python: get abstract syntax tree of imported function?

Let's say I already imported a python module into the interpreter. How can I get the abstract syntax tree of the imported module (and any functions and classes inside it) in the interpreter? I do not want to rewrite the source files. Thank!

+3
source share
2 answers

You may have found inspiration in this recipe:

A function that displays the human-readable version of Python AST.

Or use compilerin conjunction with inspect(which, of course, 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 ...
+6
source

, AST, . , , , .

0

Source: https://habr.com/ru/post/1788088/


All Articles