function to return a list of all methods for an object

Discussion in 'Python' started by Kevin Altis, Mar 5, 2004.

  1. Kevin Altis

    Kevin Altis Guest

    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
     
    Kevin Altis, Mar 5, 2004
    #1
    1. Advertising

  2. Kevin Altis

    Peter Otten Guest

    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
     
    Peter Otten, Mar 5, 2004
    #2
    1. Advertising

  3. Kevin Altis

    F. Petitjean Guest

    On Fri, 5 Mar 2004 10:39:23 -0800, 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.


    > ... 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.
     
    F. Petitjean, Mar 5, 2004
    #3
  4. Kevin Altis

    Kevin Altis Guest

    "F. Petitjean" <> wrote in message
    news:4048d717$0$277$...
    > On Fri, 5 Mar 2004 10:39:23 -0800, 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.

    >
    > > ... 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
     
    Kevin Altis, Mar 5, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    1
    Views:
    548
    Scott Allen
    Jan 7, 2005
  2. dackz
    Replies:
    0
    Views:
    504
    dackz
    Feb 6, 2007
  3. Greenhorn
    Replies:
    15
    Views:
    881
    Keith Thompson
    Mar 6, 2005
  4. DG
    Replies:
    3
    Views:
    348
    Terry Reedy
    Jul 22, 2009
  5. Kenneth McDonald
    Replies:
    5
    Views:
    373
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page