Getting a list of an object's methods?

Alexander Schmolck a.schmolck at gmx.net
Sun Jun 22 11:24:12 EDT 2003


Jeremy Yallop <jeremy at jdyallop.freeserve.co.uk> writes:

> Freddie wrote:
> > I've been having some issues trying to get a list of the methods of a class. 
> > In 2.2.3, this code gives me all of the object's variables and methods:
> > 
> > for thing in dir(self):
> >     	print 'thing:', thing
> > 
> > In 2.1.3, on several different machines/OSes (Linux and FreeBSD), only the 
> > variables show up. The same thing happens with __dict__. Is there a way to do 
> > this that will work on 2.1.x, 2.2.x, and I guess 2.3.x as well? 
> 
>   self.__class__.__dict__

No, that won't work reliably because not all instances have __class__ and not
all types/classes have __dict__s (those with __slots__, e.g and also some C
extension).

Try

>>> import inspect, operator
>>> inspect.getmembers(foo, operator.isCallable)

for a start (should come pretty close, although it will also give you callables
that aren't methods). You'll have to check this for 2.1, but if I remember
rightly it ough to work.

'as




More information about the Python-list mailing list