[Python-Dev] Module Suggestion: ast

Armin Ronacher armin.ronacher at active-4.com
Tue Apr 29 21:30:07 CEST 2008

Hi all,

I would like to propose a new module for the stdlib for Python 2.6
and higher:  "ast".  The motivation for this module is the pending
deprecation for compiler.ast which is widely used (debugging,
template engines, code coverage etc.).  _ast is a very solid module
and is without a doubt easier to maintain then compiler.ast which
was written in Python, it's lacking some features such as pretty
printing the AST or traversing it.

The idea of "ast" would be adding high level functionality for
easier working with the AST.  It currently provides these features:

-   pretty printing AST objects
-   a parse function as easier alias for compile() + flag
-   operator-node -> operator symbol mappings (eg: _ast.Add -> '+')
-   methods to modify lineno / col_offset (incrementing or copying
    the data over from existing nodes)
-   getting the fields of nodes as dicts
-   iterating over all child nodes
-   a function to get the docstring or an AST node
-   a node walker that yields all child-nodes recursively
-   a `NodeVistor` and `NodeTransformer`

Additionally there is a `literate_eval` function in that module that
can safely evaluate python literals in a string.

Module and unittests are located in the Pocoo Sandbox HG repository:


A slightly modified version of the ast.py module for Python 2.5
compatibility is currently in use for the Mako template engine to
achieve support for Google's AppEngine.

An example module for the NodeVisitor is in the repository which
converts a Python AST back into Python source code:



More information about the Python-Dev mailing list