Getting a list of an object's methods?
jeremy at jdyallop.freeserve.co.uk
Sun Jun 22 20:35:09 CEST 2003
Alexander Schmolck wrote:
> 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?
> 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
Good points. Thanks.
>>>> import inspect, operator
>>>> inspect.getmembers(foo, operator.isCallable)
> for a start
Unfortunately, this isn't any better than the original idea of using
dir(), since inspect.getmembers is implemented using dir(). As the OP
noted, dir() isn't reliable. For example, 'mro' isn't in int's dir(),
although it is (the name of) a method of type and in type's __dict__.
More information about the Python-list