The ast
module in Python provides a way to interact with Python’s abstract syntax tree (AST). The AST is a representation of the syntactic structure of Python source code, which allows you to analyze, modify, and transform Python code programmatically. This module is useful for tasks like code analysis, code transformation, and creating custom Python linters or code refactors.
1. Introduction to the AST
The abstract syntax tree is a tree representation of the source code where each node represents a syntactic construct. The ast
module allows you to parse Python code into its AST and then manipulate or analyze that tree.
2. Parsing Python Code
You can use the ast.parse()
function to parse Python source code into an AST:
import ast
# Python source code
source_code = """
def greet(name):
return f"Hello, {name}!"
"""
# Parse source code into AST
parsed_ast = ast.parse(source_code)
print(ast.dump(parsed_ast, indent=4))
The ast.dump()
function provides a readable string representation of the AST, which can be useful for debugging or understanding the structure of the parsed code.
3. Navigating the AST
You can navigate the AST using the various node types provided by the ast
module. For example, you can visit specific node types to analyze or modify the code:
import ast
class FunctionVisitor(ast.NodeVisitor):
def visit_FunctionDef(self, node):
print(f"Function name: {node.name}")
self.generic_visit(node)
# Create an instance of FunctionVisitor
visitor = FunctionVisitor()
# Visit nodes in the AST
visitor.visit(parsed_ast)
In this example, FunctionVisitor
is a custom visitor class that inherits from ast.NodeVisitor
. It overrides the visit_FunctionDef
method to print the names of functions defined in the source code.
4. Modifying the AST
You can also modify the AST by creating a custom transformer class that inherits from ast.NodeTransformer
:
import ast
class FunctionNameChanger(ast.NodeTransformer):
def visit_FunctionDef(self, node):
node.name = f"new_{node.name}"
return node
# Create an instance of FunctionNameChanger
transformer = FunctionNameChanger()
# Transform the AST
transformed_ast = transformer.visit(parsed_ast)
# Convert back to source code
import astor
new_source_code = astor.to_source(transformed_ast)
print(new_source_code)
In this example, FunctionNameChanger
is a custom transformer class that prefixes function names with new_
. The astor
module is used to convert the modified AST back into source code.
5. Use Cases
- Code Analysis: Analyze code structure and metrics, such as function definitions, variable usage, and more.
- Code Transformation: Automatically refactor code by changing function names, modifying code structure, etc.
- Custom Linters: Build custom linters to enforce coding standards or detect issues.
6. Conclusion
The ast
module is a powerful tool for working with Python code at a syntactic level. By parsing code into an abstract syntax tree, navigating and modifying the tree, and converting it back to code, you can perform a wide range of tasks from code analysis to transformation. Understanding and using the ast
module can greatly enhance your ability to interact with Python code programmatically.