CodeBlanks Logo Icon CodeBlanks

NodeTransformer

Learn transformation with ast.NodeTransformer.

What is NodeTransformer?

ast.NodeTransformer is the transforming sibling of NodeVisitor. Its visit_ methods take a node and return a modified or new node.

For example inside visit_Constant writing return ast.Constant(value=node.value * 2) doubles every constant. After transformation ast.fix_missing_locations(tree) repairs line numbers; this is required for compile and exec. This pattern is the basis of code optimization and transformation tools.

Changing nodes with visit_

In a NodeTransformer a visit_ method can do three things: return the same node (no change), return a new node (replace) or return None (delete).

When creating a new node you use constructors like ast.Constant(value=...). After transformation ast.fix_missing_locations(tree) repairs line information. Skipping this step makes compile fail. That is why this call always sits between transformation and compilation.

Practice Transform with NodeTransformer

Import the module with import ast. Write a NumberDoubler(ast.NodeTransformer) class. In visit_Constant(self, node) if isinstance(node.value, int) return ast.Constant(value=node.value * 2); otherwise return node. Build the tree with ast.parse("x = 5"), transform with NumberDoubler().visit(tree), call ast.fix_missing_locations(tree). Run exec(tree, ns) in an empty ns = {} dict and print ns["x"].

Starter code

main.py
# write a number doubler
# transform the tree and run it
# print the result

Sample solution

main.py
import ast

class NumberDoubler(ast.NodeTransformer):
    def visit_Constant(self, node):
        if isinstance(node.value, int):
            return ast.Constant(value=node.value * 2)
        return node

tree = ast.parse("x = 5")
NumberDoubler().visit(tree)
ast.fix_missing_locations(tree)
ns = {}
exec(tree, ns)
print(ns["x"])

Expected output

10

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a NodeTransformer return?

main.py
class D(ast.NodeTransformer):
    def visit_Constant(self, node):
        return ast.Constant(value=0)
  • Nothing (None)
  • A list
  • A new or modified node Correct answer
  • The original node unchanged

Explanation: answer: a new or modified node. A transformer returns a node from visit_ methods.

2 Complete the NodeTransformer definition

Question
class Doubler(____):
    def visit_Constant(self, node):
        return node
Answer
class Doubler(ast.NodeTransformer):
    def visit_Constant(self, node):
        return node

Explanation: A transformer derives from ast.NodeTransformer.

3 Match NodeTransformer concepts

Term Meaning
NodeTransformer Changes nodes
visit_Constant Constant node
fix_missing_locations Repairs lines
return node Stays the same

All lessons in Expert