ast
¶
ast¶
The ast module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like and allows modifications of it.
An abstract syntax tree can be generated by passing ast.PyCF_ONLY_AST as a flag to the compile() builtin function or by using the parse() function from this module. The result will be a tree of objects whose classes all inherit from ast.AST.
A modified abstract syntax tree can be compiled into a Python code object using the built-in compile() function.
Additionally various helper functions are provided that make working with the trees simpler. The main intention of the helper functions and this module in general is to provide an easy to use interface for libraries that work tightly with the python syntax (template engines for example).
copyright: | Copyright 2008 by Armin Ronacher. |
---|---|
license: | Python License. |
Functions¶
copy_location (new_node, old_node) |
Copy source location (lineno and col_offset attributes) from old_node to new_node if possible, and return new_node. |
dump (node[, annotate_fields, include_attributes]) |
Return a formatted dump of the tree in node. |
fix_missing_locations (node) |
When you compile a node tree with compile(), the compiler expects lineno and col_offset attributes for every node that supports them. |
get_docstring (node[, clean]) |
Return the docstring for the given node or None if no docstring can be found. |
increment_lineno (node[, n]) |
Increment the line number of each node in the tree starting at node by n. |
iter_child_nodes (node) |
Yield all direct child nodes of node, that is, all fields that are nodes and all items of fields that are lists of nodes. |
iter_fields (node) |
Yield a tuple of (fieldname, value) for each field in node._fields that is present on node. |
literal_eval (node_or_string) |
Safely evaluate an expression node or a string containing a Python expression. |
parse (source[, filename, mode]) |
Parse the source into an AST node. |
walk (node) |
Recursively yield all descendant nodes in the tree starting at node (including node itself), in no specified order. |
Classes¶
AST |
|
Add |
|
And |
|
Assert |
|
Assign |
|
Attribute |
|
AugAssign |
|
AugLoad |
|
AugStore |
|
BinOp |
|
BitAnd |
|
BitOr |
|
BitXor |
|
BoolOp |
|
Break |
|
Call |
|
ClassDef |
|
Compare |
|
Continue |
|
Del |
|
Delete |
|
Dict |
|
DictComp |
|
Div |
|
Ellipsis |
|
Eq |
|
ExceptHandler |
|
Exec |
|
Expr |
|
Expression |
|
ExtSlice |
|
FloorDiv |
|
For |
|
FunctionDef |
|
GeneratorExp |
|
Global |
|
Gt |
|
GtE |
|
If |
|
IfExp |
|
Import |
|
ImportFrom |
|
In |
|
Index |
|
Interactive |
|
Invert |
|
Is |
|
IsNot |
|
LShift |
|
Lambda |
|
List |
|
ListComp |
|
Load |
|
Lt |
|
LtE |
|
Mod |
|
Module |
|
Mult |
|
Name |
|
NodeTransformer |
A NodeVisitor subclass that walks the abstract syntax tree and allows modification of nodes. |
NodeVisitor |
A node visitor base class that walks the abstract syntax tree and calls a visitor function for every node found. |
Not |
|
NotEq |
|
NotIn |
|
Num |
|
Or |
|
Param |
|
Pass |
|
Pow |
|
Print |
|
RShift |
|
Raise |
|
Repr |
|
Return |
|
Set |
|
SetComp |
|
Slice |
|
Store |
|
Str |
|
Sub |
|
Subscript |
|
Suite |
|
TryExcept |
|
TryFinally |
|
Tuple |
|
UAdd |
|
USub |
|
UnaryOp |
|
While |
|
With |
|
Yield |
|
alias |
|
arguments |
|
boolop |
|
cmpop |
|
comprehension |
|
excepthandler |
|
expr |
|
expr_context |
|
keyword |
|
mod |
|
operator |
|
slice |
|
stmt |
|
unaryop |