The compiler.ast module is generated from a text file that
describes each node type and its elements. Each node type is
represented as a class that inherits from the abstract base class
compiler.ast.Node and defines a set of named attributes for
The Node instances are created automatically by the parser
generator. The recommended interface for specific Node
instances is to use the public attributes to access child nodes. A
public attribute may be bound to a single node or to a sequence of
nodes, depending on the Node type. For example, the
bases attribute of the Class node, is bound to a
list of base class nodes, and the doc attribute is bound to
a single node.
Each Node instance has a lineno attribute which may
be None. XXX Not sure what the rules are for which nodes
will have a useful lineno.
Returns a flattened list of the child nodes and objects in the
order they occur. Specifically, the order of the nodes is the
order in which they appear in the Python grammar. Not all of the
children are Node instances. The names of functions and
classes, for example, are plain strings.
Returns a flattened list of the child nodes in the order they
occur. This method is like getChildren(), except that it
only returns those children that are Node instances.
Two examples illustrate the general structure of Node
classes. The while statement is defined by the following
while_stmt: "while" expression ":" suite
["else" ":" suite]
The While node has three attributes: test,
body, and else_. (If the natural name for an
attribute is also a Python reserved word, it can't be used as an
attribute name. An underscore is appended to the word to make it a
legal identifier, hence else_ instead of else.)
The if statement is more complicated because it can include
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
The If node only defines two attributes: tests and
else_. The tests attribute is a sequence of test
expression, consequent body pairs. There is one pair for each
if/elif clause. The first element of the pair is
the test expression. The second elements is a Stmt node that
contains the code to execute if the test is true.
The getChildren() method of If returns a flat list of
child nodes. If there are three if/elif clauses
and no else clause, then getChildren() will return
a list of six elements: the first test expression, the first
Stmt, the second text expression, etc.
The following table lists each of the Node subclasses defined
in compiler.ast and each of the public attributes available
on their instances. The values of most of the attributes are
themselves Node instances or sequences of instances. When the
value is something other than an instance, the type is noted in the
comment. The attributes are listed in the order in which they are
returned by getChildren() and getChildNodes().