Python call tree tool?

François Pinard pinard at iro.umontreal.ca
Tue Apr 23 14:52:23 EDT 2002


[Robin Becker]

> I guess I can use inspect to figure out the functions/classes that are
> declared in an arbitrary module, but it seems harder to determine if
> these things are actually referenced in other modules. For that I assume
> I will have to do some code analysis.

It may be difficult to do in general.  Suppose you have a `maker' module
containing the definition of class `Class' (having a rich set of methods), a
`processor' module with a `do_it' function, and a `driver' module containing:

    def main():
        import maker, processor
        processor.do_it(maker.Class())

All references to `Class' methods will occur in `processor' module, but
you have no clues in `processor' about the type of the argument of `do_it'.
Moreover, in `main' above, `make.Class()' may well have been stored into an
intermediate variable or more structured container, before being actually
used as an argument to `processor.do_it'.

Such usages do not appear uncommon at all to me, and it surely requires an
extremely clever and sophisticated code analyser to find out what really
happens at run-time.  This is only a small dark side to a great virtue of
Python, by which a variable, with no fixed type in advance, gives simple
access to all methods of the class of the object it currently refers to.

Other languages might give you good tools for tracking dependencies,
but there is a high price to pay in flexibility, for making these possible.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list