19.3.1 AST Nodes

Python 2.4

19.3.1 AST Nodes

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 child nodes.

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.

All Node objects offer the following methods:

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 grammar production:

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 several tests.

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().

Node type Attribute Value
Add left left operand
right right operand
And nodes list of operands
AssAttr attribute as target of assignment
expr expression on the left-hand side of the dot
attrname the attribute name, a string
flags XXX
AssList nodes list of list elements being assigned to
AssName name name being assigned to
flags XXX
AssTuple nodes list of tuple elements being assigned to
Assert test the expression to be tested
fail the value of the AssertionError
Assign nodes a list of assignment targets, one per equal sign
expr the value being assigned
AugAssign node
op
expr
Backquote expr
Bitand nodes
Bitor nodes
Bitxor nodes
Break
CallFunc node expression for the callee
args a list of arguments
star_args the extended *-arg value
dstar_args the extended **-arg value
Class name the name of the class, a string
bases a list of base classes
doc doc string, a string or None
code the body of the class statement
Compare expr
ops
Const value
Continue
Decorators nodes List of function decorator expressions
Dict items
Discard expr
Div left
right
Ellipsis
Exec expr
locals
globals
For assign
list
body
else_
From modname
names
Function decorators Decorators or None
name name used in def, a string
argnames list of argument names, as strings
defaults list of default values
flags xxx
doc doc string, a string or None
code the body of the function
Getattr expr
attrname
Global names
If tests
else_
Import names
Invert expr
Keyword name
expr
Lambda argnames
defaults
flags
code
LeftShift left
right
List nodes
ListComp expr
quals
ListCompFor assign
list
ifs
ListCompIf test
Mod left
right
Module doc doc string, a string or None
node body of the module, a Stmt
Mul left
right
Name name
Not expr
Or nodes
Pass
Power left
right
Print nodes
dest
Printnl nodes
dest
Raise expr1
expr2
expr3
Return value
RightShift left
right
Slice expr
flags
lower
upper
Sliceobj nodes list of statements
Stmt nodes
Sub left
right
Subscript expr
flags
subs
TryExcept body
handlers
else_
TryFinally body
final
Tuple nodes
UnaryAdd expr
UnarySub expr
While test
body
else_
Yield value

See About this document... for information on suggesting changes.