AST objects, regardless of the input used to create them, may be
converted to parse trees represented as list- or tuple- trees, or may
be compiled into executable code objects. Parse trees may be
extracted with or without line numbering information.
ast2list(
ast[, line_info])
This function accepts an AST object from the caller in
ast and returns a Python list representing the
equivalent parse tree. The resulting list representation can be used
for inspection or the creation of a new parse tree in list form. This
function does not fail so long as memory is available to build the
list representation. If the parse tree will only be used for
inspection, ast2tuple() should be used instead to reduce memory
consumption and fragmentation. When the list representation is
required, this function is significantly faster than retrieving a
tuple representation and converting that to nested lists.
If line_info is true, line number information will be
included for all terminal tokens as a third element of the list
representing the token. Note that the line number provided specifies
the line on which the token ends. This information is
omitted if the flag is false or omitted.
ast2tuple(
ast[, line_info])
This function accepts an AST object from the caller in
ast and returns a Python tuple representing the
equivalent parse tree. Other than returning a tuple instead of a
list, this function is identical to ast2list().
If line_info is true, line number information will be
included for all terminal tokens as a third element of the list
representing the token. This information is omitted if the flag is
false or omitted.
compileast(
ast[, filename = '<ast>'])
The Python byte compiler can be invoked on an AST object to produce
code objects which can be used as part of an exec statement or
a call to the built-in eval() function.
This function provides the interface to the compiler, passing the
internal parse tree from ast to the parser, using the
source file name specified by the filename parameter.
The default value supplied for filename indicates that
the source was an AST object.
Compiling an AST object may result in exceptions related to
compilation; an example would be a SyntaxError caused by the
parse tree for del f(0): this statement is considered legal
within the formal grammar for Python but is not a legal language
construct. The SyntaxError raised for this condition is
actually generated by the Python byte-compiler normally, which is why
it can be raised at this point by the parser module. Most
causes of compilation failure can be diagnosed programmatically by
inspection of the parse tree.