printing class methods

Alex Martelli aleax at aleax.it
Fri Jan 25 05:59:02 EST 2002


"Joe Connellan" <joe at al.com.au> wrote in message
news:3C50CC1C.7CFD8EA6 at al.com.au...
> Hi,
>     can some kind soul remind me how to find out (programatically) what
> methods/members a class has? also is there a way to find their number
> and expected type of arguments?

You can use module inspect for this, or operate at a lower level
of 'introspection' -- in Python 2.2, dir(X) where x is the class
object gives you a list of all class attribute names, then you
can use getattr(X, aname) to obtain an attribute object and check
it with callable (to see if it's a method or a data attribute)
and other helpful builtins.  With inspect, you could use
inspect.getmembers(X, inspect.ismethod) and get the methods
directly, as a sequence of (name, value) pairs.

inspect.getargspec is a nice way to learn all there is to learn
about a function's arguments (which of course does not include
any 'expected type' -- where would you get THAT info...?), but
as an argument you need to pass a function, not a method object.
Given a method object, the corresponding function object is
the method's im_func attribute, though.


Alex






More information about the Python-list mailing list