Skip to content
UPEEC

ast

Parse Python source into an Abstract Syntax Tree for analysis or transformation.

Advanced
Comments 0
import ast
tree = ast.parse("x = 1 + 2")
print(ast.dump(tree, indent=2))
ast.literal_eval("[1, 2, {'a': 3}]")    # safe evaluation of literals

Linters, formatters and code-checkers are built on ASTs.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.