ast
Stdlib — IntrospectionPython 2.6+Expert
Parse Python source into an Abstract Syntax Tree
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.6+
- Dependencies
- None — Python Standard Library
- Install
Included with Python
Learn by Difficulty
Quick Example
python
import ast code = "x = [i**2 for i in range(10) if i % 2 == 0]" tree = ast.parse(code) print(ast.dump(tree, indent=2))
The ast module is part of Python's standard library. Parse Python source into an Abstract Syntax Tree.
Try in PlaygroundTags
stdlibintrospectionmetaprogrammingparsing
Related Items
dis
Stdlib — Introspection
Disassemble Python bytecode to human-readable instructions
compile()
Built-in Function
Compiles source code into a code object for exec() or eval()
eval()
Built-in Function
Evaluates a string as a Python expression and returns the result
inspect
Stdlib — Introspection
Inspect live objects: source code, signatures, class hierarchies
tokenize
Stdlib — Introspection
Tokenize Python source code into individual tokens