Green Tree Snakes - the missing Python AST docs
Abstract Syntax Trees, ASTs, are a powerful feature of Python. You can write programs that inspect and modify Python code, after the syntax has been parsed, but before it gets compiled to byte code. That opens up a world of possibilities for introspection, testing, and mischief.
The official documentation for the ast module
used to be rather brief. A large part of the material from Green Tree Snakes,
describing all of the AST node classes, has now been merged into the ast
module docs. The remainder here aims to serve as a field guide (or forest
guide?) to working with ASTs in practice.
To contribute to the guide, see the source repository.
Contents:
See also
- Thonny
A Python IDE with AST explorer built in (Main menu => View => AST)
- showast
An IPython extension to show ASTs in Jupyter notebooks
- Instrumenting the AST
Using AST tools to assess code coverage
- awesome-python-ast
A longer list of resources about Python ASTs (but not recently updated as of 2025)