Language and Compiler Modules

ast

Parses Python source into abstract syntax tree nodes for inspection and transformation. Nodes describe grammatical structure rather than preserving all formatting or comments. Parsing does not execute the source or guarantee that later compilation succeeds. Do not treat parsing or literal_eval as a resource-safe sandbox for hostile input.

Formal Syntax & Structure

python syntaxpython
import ast

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import asttree = ast.parse("total = 2 + 3")print(type(tree.body[0]).__name__)
Python Reference