NodeVisitor首先遍历AST深度优先,并在输入时仅访问每个节点一次。因此,做一些严肃的事情是有问题的。是否可以更改其默认行为?
答案 0 :(得分:8)
也许有人会对一些草拟的例子感兴趣,如何做DSblizzard提出的建议,所以听到我的例子:
import ast
class RecursiveVisitor(ast.NodeVisitor):
""" example recursive visitor """
def recursive(func):
""" decorator to make visitor work recursive """
def wrapper(self,node):
func(self,node)
for child in ast.iter_child_nodes(node):
self.visit(child)
return wrapper
@recursive
def visit_Assign(self,node):
""" visit a Assign node and visits it recursively"""
print(type(node).__name__)
@recursive
def visit_BinOp(self, node):
""" visit a BinOp node and visits it recursively"""
print(type(node).__name__)
@recursive
def visit_Call(self,node):
""" visit a Call node and visits it recursively"""
print(type(node).__name__)
@recursive
def visit_Lambda(self,node):
""" visit a Function node """
print(type(node).__name__)
@recursive
def visit_FunctionDef(self,node):
""" visit a Function node and visits it recursively"""
print(type(node).__name__)
@recursive
def visit_Module(self,node):
""" visit a Module node and the visits recursively"""
pass
def generic_visit(self,node):
pass
class SimpleVisitor(ast.NodeVisitor):
""" simple visitor for comparison """
def recursive(func):
""" decorator to make visitor work recursive """
def wrapper(self,node):
func(self,node)
for child in ast.iter_child_nodes(node):
self.visit(child)
return wrapper
def visit_Assign(self,node):
""" visit a Assign node """
print(type(node).__name__)
def visit_BinOp(self, node):
""" visit a BinOp node """
print(type(node).__name__)
def visit_Call(self,node):
""" visit a Call node """
print(type(node).__name__)
def visit_Lambda(self,node):
""" visit a Function node """
print(type(node).__name__)
def visit_FunctionDef(self,node):
""" visit a Function node """
print(type(node).__name__)
@recursive
def visit_Module(self,node):
""" visit a Module node and the visits recursively, otherwise you
wouldn't see anything here"""
pass
def generic_visit(self,node):
pass
# usage example
a = """
b= lambda x: x*5 +5
def hhh(u):
b=19
return u*b
m=hhh(9*4+5)
"""
recursive_visitor = RecursiveVisitor()
simple_visitor = SimpleVisitor()
tree = ast.parse(a)
print('\nvisit recursive\n')
recursive_visitor.visit(tree)
print('\nvisit simple\n')
simple_visitor.visit(tree)
希望有人觉得它很有用。
答案 1 :(得分:2)
不。你应该写自己的访客。递归访问所有节点非常简单,所以如果ast模块的东西没有为你做(并且模块中没有任何东西可以做你想要的,在这种情况下),自己做它并不坏