[Tutor] List of Methods in a class / library

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Sep 9 14:23:56 EDT 2003



On Tue, 9 Sep 2003, Anand Shankar wrote:

> How can I get a listing of all methods in a class or module, and better
> still the arguments required?


Hi Anand,


The other folks on Tutor have already mentioned using dir() and help().
Another possibility is to use the 'inspect' module:

    http://www.python.org/doc/lib/module-inspect.html


There are a few functions in the 'inspect' module to make it easier to
"introspect" things at runtime.


For example, there's a nice function called 'getargspec()' that takes a
function and returns a description of the arguments it uses:

###
>>> def hypotenuse(a, b):
...     return (a**2 + b**2) ** (0.5)
...
>>> import inspect
>>> inspect.getargspec(hypotenuse)
(['a', 'b'], None, None, None)
###


'getargspec()' is documented here:

    http://www.python.org/doc/lib/inspect-classes-functions.html



> No I don't mean the default: Look up documentation!!

But what's wrong with documentation?  *grin*


>From your question, it sounds like you might be trying to write some sort
of program that works on other programs, and for that kind of
metaprogramming, the 'inspect' module might just be the tool you're
looking for.  But is there anything in particular that you're trying to
do?



Best of wishes to you!




More information about the Tutor mailing list