function to return a list of all methods for an object

F. Petitjean littlejohn.75 at news.noos.fr
Fri Mar 5 20:37:59 CET 2004


On Fri, 5 Mar 2004 10:39:23 -0800, Kevin Altis <altis at semi-retired.com> 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.




More information about the Python-list mailing list