Search code examples
pythonabstract-syntax-tree

Given an AST, is there a working library for getting the source?


Is there a way to convert a given Python abstract syntax tree (AST) to a source code?

Here is a good example of how to use Python's ast module, specifically a NodeTransformer. I was looking for a way to convert the resulting AST back to source, so the changes can be inspected visually.


Solution

  • The Python source tree contains an implementation of this: unparse.py in the Demo/parser directory

    Editor's note: With the introduction of ast.unparse() in Python 3.9, unparse.py has been removed, so the above link has been updated to point to 3.8.