CodeBlanks Logo Icon CodeBlanks

Summary - AST

Combine parse, NodeVisitor and compile to write a code analysis tool.

Recap: AST tools

In this group you learned the AST toolchain. You turned code into a tree with ast.parse, did quick scans with ast.walk, and walked orderly with NodeVisitor.

You changed nodes with NodeTransformer and recompiled and ran with compile. Now you will combine these into a code analysis tool: a FunctionLister visitor that prints the name of every function definition in the tree. This is the core idea of linters and documentation tools.

Practice Write a code analysis tool

Import the module with import ast. Write a FunctionLister(ast.NodeVisitor) class. In visit_FunctionDef(self, node) do print(node.name) and call self.generic_visit(node). Build the tree with ast.parse("def a():\n pass\ndef b():\n pass") and run it with FunctionLister().visit(tree).

Starter code

main.py
# write a function lister
# walk the tree and print names

Sample solution

main.py
import ast

class FunctionLister(ast.NodeVisitor):
    def visit_FunctionDef(self, node):
        print(node.name)
        self.generic_visit(node)

tree = ast.parse("def a():\n    pass\ndef b():\n    pass")
FunctionLister().visit(tree)

Expected output

a
b

What you learned

In this group you went deep into Python's abstract syntax tree. You learned to turn code into a tree with ast.parse and to do quick scans with ast.walk.

You saw orderly walking with NodeVisitor, node modification with NodeTransformer and recompilation with compile. In the summary you combined these into a code analysis tool. This knowledge is the foundation for writing linters, formatters and code generators.

Questions and answers

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

1 Which tool for which job?

  • ast.parse to build a tree
  • NodeTransformer to delete a node
  • ast.walk to modify a node
  • compile to run a tree Correct answer

Explanation: answer: compile to run a tree. ast.walk only walks, it does not modify.

2 Match all AST concepts

Term Meaning
ast.parse Produces a tree
NodeVisitor Walks orderly
NodeTransformer Changes nodes
compile Turns into code

3 Final blank

Question
class FunctionLister(ast.NodeVisitor):
    def visit_FunctionDef(self, node):
        print(node.____)
        self.generic_visit(node)
Answer
class FunctionLister(ast.NodeVisitor):
    def visit_FunctionDef(self, node):
        print(node.name)
        self.generic_visit(node)

Explanation: node.name carries the function's name.

All lessons in Expert