Simple example of how to use ast.NodeVisitor?

前端 未结 3 1892
死守一世寂寞
死守一世寂寞 2020-12-02 08:03

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

3条回答
  •  死守一世寂寞
    2020-12-02 08:37

    generic_visit is called when a custom visitor (ie visit_Name) can't be found. Here's a piece of code I wrote recently with ast.NodeVisitor: https://foss.heptapod.net/pypy/pypy/-/blob/80ead76ab428100ffeb01109c7fc0d94f1048af2/py/_code/_assertionnew.py It interprets the AST nodes to gain debugging information about some of them and falls back in with generic_visit when a special implementation isn't provided.

提交回复
热议问题