parsing ast nodes help to get callfunc values.

Peter Otten __peter__ at web.de
Tue Oct 30 22:15:51 CET 2007


Glich wrote:

> If some one could show me how to use something called "visitor"? 

import compiler

module = """
def fun1():
    print "Hi"
    fun2()
    fun4()

def fun2():
    pass

def fun4():
    pass

fun1()
fun3(fun2())
"""

class Visitor:
    def visitCallFunc(self, node):
        print node.lineno, node.node.name
        for child in node.getChildNodes():
            compiler.walk(child, self)
                    
if __name__ == "__main__":
    ast = compiler.parse(module)
    visitor = Visitor()
    compiler.walk(ast, visitor)

> I did not understand the documentation and I am in need of more SIMPLE
> example code.

If my example is not sufficient I encourage you to take a look into the
source code of the compiler package.

Peter



More information about the Python-list mailing list