how to find a function definition using AST

stephene at sjsoft.com stephene at sjsoft.com
Mon Feb 23 16:33:54 CET 2004


I have a question about the compiler.ast module. Please bear with me if I
am missing something obvious, I am not too sure how to use this module.

I am using the compiler.visitor module to examine a piece of code and
extract certain useful information. However, I have run into trouble that I
dont know how to solve. When I override visitCallFunc I can get the name of
the function being called, but thats about it. What I would really like is
to know exactly where that function is defined (line of code, what class if
belongs to, everything).

I had a thought that when one prints the node argument from visitCallFunc
then you can see a node which looks something like Getattr(Name('catfish'),
'double'. Perhaps if this statement could be evaluated then the function in
question could be found.

Absolutely any thoughts would be appreciated!
Thanks
Stephen Emslie

my current code looks like this:

class C1:
    def F1(self):
        """
        pretty much does bugger all
        """
        catfish = C2
        x = catfish.double(50)
        return x

class C2:
    def double(x):
        "Return twice the argument"
        return x * 2

class MyVisitor(visitor.ASTVisitor):
    def visitCallFunc(self,node):
        print node


--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .






More information about the Python-list mailing list