ast | Python Standard Library – Real Python
The Python ast module lets you work with Abstract Syntax Trees (ASTs), which represent the structure of Python source code. You can use it for introspection, static analysis, and programmatic code transformations.
Here’s a quick example:
Key Features
- Parses Python source code into an Abstract Syntax Tree
- Generates ASTs directly from compiled code with the
ast.PyCF_ONLY_ASTflag - Provides tools for traversing and modifying ASTs
- Includes base classes for visiting and transforming nodes
- Preserves and updates source-location metadata (line and column information)
- Can compile ASTs back into executable code
- Can convert ASTs back into Python source code (but not original formatting or comments)
Frequently Used Classes and Functions
Examples
Modify an AST to change variable names:
Compile and execute an AST:
Common Use Cases
The most common tasks for ast include:
- Analyzing code for static analysis tools (such as linters and type checkers)
- Extracting structure for documentation and dependency analysis (such as imports)
- Refactoring code programmatically (such as renaming symbols or rewriting APIs)
- Instrumenting code automatically (such as inserting logging or timing)
- Generating code from templates or other inputs
- Building code formatters, transpilers, and domain-specific languages
Real-World Example
Suppose you want to programmatically rename all occurrences of a variable in a script:
In this example, the ast module was used to rename a variable in the source code and execute the modified code. Note the use of ast.fix_missing_locations() to ensure the modified AST has proper line number information before compilation.
Tutorial
Your Guide to the CPython Source Code
In this detailed Python tutorial, you'll explore the CPython source code. By following this step-by-step walkthrough, you'll take a deep dive into how the CPython compiler works and how your Python code gets executed.
For additional information on related topics, take a look at the following resources: