function to return a list of all methods for an object

K

Kevin Altis

I need a way of getting a list of all the methods for an object. I want the
unbound methods for the objects class for some further manipulation. My old
way of doing this did a recursive iteration over __bases__ and the code
seems sort of fragile, especially with multiple inheritance. I came up with
a much simpler version that uses dir(), but would like a sanity check before
committing to it. Perhaps there is a better way? This only needs to work
with Python 2.2 and later, so I won't mind say "Doh!" if I've missed the
obvious simple solution and someone points out my stupidity.

# return a list of unbound methods for an object
def getMethods(object):
methods = []
names = dir(object.__class__)
for name in names:
m = getattr(object.__class__, name)
if isinstance(m, types.MethodType):
#print "method:", m
methods.append(m)
return methods


I went ahead and wrote a simple test program:

import types
import pprint

def getMethods(object):
methods = []
names = dir(object.__class__)
for name in names:
m = getattr(object.__class__, name)
if isinstance(m, types.MethodType):
#print "method:", m
methods.append(m)
return methods


class A:
def oneA(self):
pass

def dummy(self):
print "A dummy"

class B(A):
def twoB(self):
pass

def dummy(self):
print "B dummy"

class C:
def threeC(self):
pass

class D:
def fourD(self):
pass

class E(B, C, D):
pass

e = E()
print e, E
e.dummy()
methods = getMethods(e)
pprint.pprint(methods)


Here's the output:
<__main__.E instance at 0x007EABC0> __main__.E
B dummy
[<unbound method E.dummy>,
<unbound method E.fourD>,
<unbound method E.oneA>,
<unbound method E.threeC>,
<unbound method E.twoB>]

Thanks,

ka
 
P

Peter Otten

Kevin said:
I need a way of getting a list of all the methods for an object. I want
the unbound methods for the objects class for some further manipulation.
My old way of doing this did a recursive iteration over __bases__ and the
code seems sort of fragile, especially with multiple inheritance. I came
up with a much simpler version that uses dir(), but would like a sanity
check before committing to it. Perhaps there is a better way? This only
needs to work with Python 2.2 and later, so I won't mind say "Doh!" if
I've missed the obvious simple solution and someone points out my
stupidity.

# return a list of unbound methods for an object
def getMethods(object):
methods = []
names = dir(object.__class__)
for name in names:
m = getattr(object.__class__, name)
if isinstance(m, types.MethodType):
#print "method:", m
methods.append(m)
return methods

Seems sensible...
Here's the output:
<__main__.E instance at 0x007EABC0> __main__.E
B dummy
[<unbound method E.dummy>,
<unbound method E.fourD>,
<unbound method E.oneA>,
<unbound method E.threeC>,
<unbound method E.twoB>]


.... and works.

You might consider using inspect.getmembers() instead, which is implemented
in a similar fashion:

def getmembers(object, predicate=None):
results = []
for key in dir(object):
value = getattr(object, key)
if not predicate or predicate(value):
results.append((key, value))
results.sort()
return results

Providing the predicate is straightforward, so it all depends on wether you
like the (name, method) tuples instead of your method-only in the resulting
list.


Peter
 
F

F. Petitjean

I need a way of getting a list of all the methods for an object. I want the
unbound methods for the objects class for some further manipulation.
... I came up with
a much simpler version that uses dir(), but would like a sanity check before
committing to it. Perhaps there is a better way? This only needs to work
with Python 2.2 and later, so I won't mind say "Doh!" if I've missed the
obvious simple solution and someone points out my stupidity.


import types
import pprint
import inspect
help(inspect.getmembers)
def getMethods(object): # don't use object !
methods = []
names = dir(object.__class__)
for name in names:
m = getattr(object.__class__, name)
if isinstance(m, types.MethodType):
#print "method:", m
methods.append(m)
return methods
Try it :
b = inspect.BlockFinder()
inspect.getmembers(inspect.BlockFinder, inspect.ismethod)


Regards.
 
K

Kevin Altis

F. Petitjean said:
I need a way of getting a list of all the methods for an object. I want the
unbound methods for the objects class for some further manipulation.
... I came up with
a much simpler version that uses dir(), but would like a sanity check before
committing to it. Perhaps there is a better way? This only needs to work
with Python 2.2 and later, so I won't mind say "Doh!" if I've missed the
obvious simple solution and someone points out my stupidity.


import types
import pprint
import inspect
help(inspect.getmembers)
def getMethods(object): # don't use object !
methods = []
names = dir(object.__class__)
for name in names:
m = getattr(object.__class__, name)
if isinstance(m, types.MethodType):
#print "method:", m
methods.append(m)
return methods
Try it :
b = inspect.BlockFinder()
inspect.getmembers(inspect.BlockFinder, inspect.ismethod)

Awesome! Looks like I need to do a little refactoring if I'm going to deal
with the list of tuples result, but in the meantime I can do a list
comprehension like this in in the test so I still have a list of unbound
methods.

methods = [t[1] for t in inspect.getmembers(e.__class__, inspect.ismethod)]

Thanks for pointing out inspect,

ka
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top