Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
ast.NodeTransformer Class Reference
Inheritance diagram for ast.NodeTransformer:
Inheritance graph
Collaboration diagram for ast.NodeTransformer:
Collaboration graph

List of all members.

Public Member Functions

def generic_visit
def visit

Public Attributes

_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Detailed Description

A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
allows modification of nodes.

The `NodeTransformer` will walk the AST and use the return value of the
visitor methods to replace or remove the old node.  If the return value of
the visitor method is ``None``, the node will be removed from its location,
otherwise it is replaced with the return value.  The return value may be the
original node in which case no replacement takes place.

Here is an example transformer that rewrites all occurrences of name lookups
(``foo``) to ``data['foo']``::

   class RewriteName(NodeTransformer):

       def visit_Name(self, node):
           return copy_location(Subscript(
               value=Name(id='data', ctx=Load()),
           ), node)

Keep in mind that if the node you're operating on has child nodes you must
either transform the child nodes yourself or call the :meth:`generic_visit`
method for the node first.

For nodes that were part of a collection of statements (that applies to all
statement nodes), the visitor may also return a list of nodes rather than
just a single node.

Usually you use the transformer like this::

   node = YourTransformer().visit(node)

Definition at line 261 of file

Member Function Documentation

def ast.NodeTransformer.generic_visit (   self,
Called if no explicit visitor function exists for a node.

Reimplemented from ast.NodeVisitor.

Definition at line 297 of file

00298     def generic_visit(self, node):
00299         for field, old_value in iter_fields(node):
00300             old_value = getattr(node, field, None)
00301             if isinstance(old_value, list):
00302                 new_values = []
00303                 for value in old_value:
00304                     if isinstance(value, AST):
00305                         value = self.visit(value)
00306                         if value is None:
00307                             continue
00308                         elif not isinstance(value, AST):
00309                             new_values.extend(value)
00310                             continue
00311                     new_values.append(value)
00312                 old_value[:] = new_values
00313             elif isinstance(old_value, AST):
00314                 new_node = self.visit(old_value)
00315                 if new_node is None:
00316                     delattr(node, field)
00317                 else:
00318                     setattr(node, field, new_node)
00319         return node

Here is the call graph for this function:

def ast.NodeVisitor.visit (   self,
) [inherited]
Visit a node.

Definition at line 244 of file

00245     def visit(self, node):
00246         """Visit a node."""
00247         method = 'visit_' + node.__class__.__name__
00248         visitor = getattr(self, method, self.generic_visit)
00249         return visitor(node)

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

The documentation for this class was generated from the following file: