Staging: this is not the live site, pydocs.org.
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. © Python Software Foundation. PSF License Version 2 · Official docs ↗
Symbols in ast a alias alias(identifier name, identifier? AnnAssign AnnAssign(expr target, expr annotation, expr? arg arg(identifier arg, expr? arguments arguments(arg* posonlyargs, arg* args, arg? Assign Assign(expr* targets, expr value, string? AST This is the abstract base of all AST node classes. AsyncFor AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? AsyncFunctionDef AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? AsyncWith AsyncWith(withitem* items, stmt* body, string? Attribute Attribute(expr value, identifier attr, expr_context ctx) AugAssign AugAssign(expr target, operator op, expr value) b BinOp BinOp(expr left, operator op, expr right) BoolOp BoolOp(boolop op, expr* values) c Call Call(expr func, expr* args, keyword* keywords) ClassDef ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list, type_param* type_params) compare Recursively compares two ASTs. Compare Compare(expr left, cmpop* ops, expr* comparators) comprehension comprehension(expr target, expr iter, expr* ifs, int is_async) Constant Constant(constant value, string? copy_location Copy source location (`lineno`, `col_offset`, `end_lineno`, and `end_col_offset` attributes) from *old_node* to *new_node* if possible, and return *new_node*. d Dict Dict(expr?* keys, expr* values) dump Return a formatted dump of the tree in node. f fix_missing_locations When you compile a node tree with compile(), the compiler expects lineno and col_offset attributes for every node that supports them. For For(expr target, expr iter, stmt* body, stmt* orelse, string? FunctionDef FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? g GeneratorExp GeneratorExp(expr elt, comprehension* generators) get_docstring Return the docstring for the given node or None if no docstring can be found. Global Global(identifier* names) i If If(expr test, stmt* body, stmt* orelse) IfExp IfExp(expr test, expr body, expr orelse) Import Import(alias* names, int? increment_lineno Increment the line number and end line number of each node in the tree starting at *node* by *n*. Interpolation Interpolation(expr value, constant str, int conversion, expr? iter_child_nodes 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 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields`` that is present on *node*. l Lambda Lambda(arguments args, expr body) List List(expr* elts, expr_context ctx) ListComp ListComp(expr elt, comprehension* generators) literal_eval Evaluate an expression node or a string containing only a Python expression. m Match Match(expr subject, match_case* cases) MatchClass MatchClass(expr cls, pattern* patterns, identifier* kwd_attrs, pattern* kwd_patterns) MatchMapping MatchMapping(expr* keys, pattern* patterns, identifier? Module Module(stmt* body, type_ignore* type_ignores) n Name Name(identifier id, expr_context ctx) NodeTransformer A :class:`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. p parse Parse the source into an AST node. PyCF_ONLY_AST Generates and returns an abstract syntax tree instead of returning a compiled code object. PyCF_OPTIMIZED_AST The returned AST is optimized according to the optimize argument in compile() or ast.parse(). PyCF_TYPE_COMMENTS Enables support for PEP 484 and PEP 526 style type comments (# type: <type>, # type: ignore <stuff>). s SetComp SetComp(expr elt, comprehension* generators) Starred Starred(expr value, expr_context ctx) Subscript Subscript(expr value, expr slice, expr_context ctx) t Try Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody) TryStar TryStar(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody) Tuple Tuple(expr* elts, expr_context ctx) TypeAlias TypeAlias(expr name, type_param* type_params, expr value) TypeVar TypeVar(identifier name, expr? u UnaryOp UnaryOp(unaryop op, expr operand) unparse Unparse an ast.AST object and generate a string with code that would produce an equivalent ast.AST object if parsed back with ast.parse(). w walk Recursively yield all descendant nodes in the tree starting at *node* (including *node* itself), in no specified order. While While(expr test, stmt* body, stmt* orelse) With With(withitem* items, stmt* body, string? withitem withitem(expr context_expr, expr?
Python docs © Python Software Foundation · Examples CC0 ·
Source on GitHub · Not affiliated with the PSF