Python Glossary Русский

all Python 3astterm

ast

module

Abstract syntax trees: parse Python source into a tree of nodes and walk it.

Syntax

import ast

Linters, analysers and code generators use it: ast.parse turns source into a tree you can search for constructs or rewrite. Another common use is ast.literal_eval, a safe replacement for eval on strings of literals. The tree describes syntax, not execution: variable types and values are not visible in it.

Examples

import ast
tree = ast.parse('x = 1 + 2')
print(type(tree.body[0]).__name__)  # → Assign
print(ast.literal_eval('[1, 2, 3]'))  # → [1, 2, 3]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy