Simple example of how to use ast.NodeVisitor?
问题 Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6? The difference between visit and generic_visit is unclear to me, and I cannot find any example using google codesearch or plain google. 回答1: ast.visit -- unless you override it in a subclass, of course -- when called to visit an ast.Node of class foo , calls self.visit_foo if that method exists, otherwise self.generic_visit . The latter, again in its implementation in class ast itself, just