Developpez.com

Python

Choisissez la catégorie, puis la rubrique :

31.4 Using Visitors to Walk ASTs

31.4 Using Visitors to Walk ASTs

The visitor pattern is ... The compiler package uses a variant on the visitor pattern that takes advantage of Python's introspection features to eliminate the need for much of the visitor's infrastructure.

The classes being visited do not need to be programmed to accept visitors. The visitor need only define visit methods for classes it is specifically interested in; a default visit method can handle the rest.

XXX The magic visit() method for visitors.

walk( tree, visitor[, verbose])

class ASTVisitor( )

The ASTVisitor is responsible for walking over the tree in the correct order. A walk begins with a call to preorder(). For each node, it checks the visitor argument to preorder() for a method named `visitNodeType,' where NodeType is the name of the node's class, e.g. for a While node a visitWhile() would be called. If the method exists, it is called with the node as its first argument.

The visitor method for a particular node type can control how child nodes are visited during the walk. The ASTVisitor modifies the visitor argument by adding a visit method to the visitor; this method can be used to visit a particular child node. If no visitor is found for a particular node type, the default() method is called.

ASTVisitor objects have the following methods:

XXX describe extra arguments

default( node[, ...])

dispatch( node[, ...])

preorder( tree, visitor)

See About this document... for information on suggesting changes.
Les développeurs logiciels actifs sont actuellement estimés à un peu moins de 19 millions dans le monde, 13 millions d'entre eux seraient des pros
Voici l'état de l'écosystème des développeurs en 2019 : l'évolution du monde du code, d'après une enquête menée par Jetbrains
Python 3.8.0 : un aperçu des fonctionnalités attendues pour la version stable du langage de programmation
Apprendre Python et s'initier à la programmation - Partie 2 : Programmation avancée, un cours de Sébastien Combéfis
Contacter le responsable de la rubrique Python

Partenaire : Hébergement Web