Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

abstractsyntaxtree
python
communitycreator

What is ast.withitem(context_expr, optional_vars) in Python?

Adnan Abbas

Python ast module

The astAbstract Syntax Tree - a tree representation of source code module allows us to interact with and modify Python code. Python comes with abstract syntax grammar that is subject to change with every Python release.

This module helps applications to process trees of syntax and find out what the current syntax grammar looks like programmatically.

Abstract syntax trees are great tools for program analysis and program transformation systems.

The ast.withitem method

ast.withitem(context_expr, optional_vars) is a class defined in the ast module that expresses a single context in the with block in Python in the form of an abstract syntax tree.

When the parse() method of ast is called on a Python source code that contains a with block, the ast.with and ast.withitem classes are invoked.

Each context expression in the with block is expressed as a withitem node in an ast tree data structure.


The ast.With class represents a With node which contains a list of withitem nodes.

Parameters

  • context_expr: contains a single context manager in a with block.

  • optional_vars: node representing the as foo part of with statement. Often a Name, Tuple, or a List node. It is a None node if there is no as foo part.

  • body: list of nodes in the body of the with block.

Code

The following Python code illustrates an example of the ast.withitem class.

import ast
from pprint import pprint

class WithItemVisitor(ast.NodeVisitor):
    def visit_withitem(self, node):
        print('\nNode type: withitem\nFields: ', node._fields)
        self.generic_visit(node)
    
    def visit_With(self, node):
        print('\nNode type: With\nFields: ', node._fields)
        self.generic_visit(node)

    def visit_Name(self,node):
        print('\nNode type: Name\nFields: ', node._fields)
        ast.NodeVisitor.generic_visit(self, node)


visitor = WithItemVisitor()
tree = ast.parse("""
with a as b, c as d:
    ...
""")
pprint(ast.dump(tree))
visitor.visit(tree)

Explanation

  • We define a WithItemVisitor class that extends from the ast.NodeVisitor parent class. We override the predefined visit_withitem, visit_With, and visit_Name methods in the parent class, which receive the withitem, With, and Name nodes, respectively.

  • In these methods, we print the type and the fields inside the node and call the generic_visit() method to visit the children nodes of the input node.

  • We initialize a visitor object of the class WithItemVisitor in line 18.

  • We write a Python with block and send it to the ast.parse() method, which returns the result of the expression after evaluation, and then stores this result in tree.

  • The ast.dump() method returns a formatted string of the tree structure in tree. You can observe the string returned by the dump function in the output of the code.

  • The inbuilt visit method available to the visitor object visits all the nodes in the tree structure.

RELATED TAGS

abstractsyntaxtree
python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring