Visiting nodes in a syntax tree with Python ast module
问题 I'm playing with python ast (abstract syntax tree). I wrote the following and it visited all nodes of the AST. import ast class Py2Neko(ast.NodeVisitor): def generic_visit(self, node): print type(node).__name__ ast.NodeVisitor.generic_visit(self, node) def visit_Name(self, node): print 'Name :', node.id def visit_Num(self, node): print 'Num :', node.__dict__['n'] def visit_Str(self, node): print "Str :", node.s if __name__ == '__main__': node = ast.parse("a = 1 + 2") print ast.dump(node) v =