Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > function to return a list of all methods for an object

Reply
Thread Tools

function to return a list of all methods for an object

 
 
Kevin Altis
Guest
Posts: n/a
 
      03-05-2004
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


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-05-2004
Kevin Altis wrote:

> 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

 
Reply With Quote
 
 
 
 
F. Petitjean
Guest
Posts: n/a
 
      03-05-2004
On Fri, 5 Mar 2004 10:39:23 -0800, Kevin Altis <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Kevin Altis
Guest
Posts: n/a
 
      03-05-2004

"F. Petitjean" <(E-Mail Removed)> wrote in message
news:4048d717$0$277$(E-Mail Removed)...
> On Fri, 5 Mar 2004 10:39:23 -0800, Kevin Altis <(E-Mail Removed)>

wrote:
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Using object methods of first module in methods of second module Nikita Petrov Ruby 2 04-06-2008 08:49 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Why Petshop Changed all static methods to instance methods when upgrading from version 3.0 to version 3.1? Neo ASP .Net 1 01-07-2005 01:46 AM



Advertisments