Is there way to determine which class a method is bound to?

Discussion in 'Python' started by Victor Ng, Feb 25, 2005.

  1. Victor Ng

    Victor Ng Guest

    I'm doing some evil things in Python and I would find it useful to
    determine which class a method is bound to when I'm given a method
    pointer.

    For example:

    class Foo(object):
    def somemeth(self):
    return 42

    class Bar(Foo):
    def othermethod(self):
    return 42


    Is there some way I can have something like :

    findClass(Bar.somemeth)

    that would return the 'Foo' class, and

    findClass(Bar.othermethod)

    would return the 'Bar' class?

    vic
    Victor Ng, Feb 25, 2005
    #1
    1. Advertising

  2. Victor Ng

    Peter Otten Guest

    Victor Ng wrote:

    > I'm doing some evil things in Python and I would find it useful to
    > determine which class a method is bound to when I'm given a method
    > pointer.
    >
    > For example:
    >
    > class Foo(object):
    > def somemeth(self):
    > return 42
    >
    > class Bar(Foo):
    > def othermethod(self):
    > return 42
    >
    >
    > Is there some way I can have something like :
    >
    > findClass(Bar.somemeth)
    >
    > that would return the 'Foo' class, and
    >
    > findClass(Bar.othermethod)
    >
    > would return the 'Bar' class?
    >
    > vic


    >>> import inspect
    >>> class Foo(object):

    .... def foo(self): pass
    ....
    >>> class Bar(Foo):

    .... def bar(self): pass
    ....
    >>> def get_imp_class(method):

    .... return [t for t in inspect.classify_class_attrs(method.im_class) if
    t[-1] is method.im_func][0][2]
    ....
    >>> [get_imp_class(m) for m in [Bar().foo, Bar().bar, Bar.foo, Bar.bar]]

    [<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Foo'>,
    <class '__main__.Bar'>]

    but with this approach you will get into trouble as soon as you are using
    the same function to define multiple methods. There may be something in the
    inspect module more apt to solve the problem -- getmro() perhaps?

    Peter
    Peter Otten, Feb 25, 2005
    #2
    1. Advertising

  3. Victor Ng

    Victor Ng Guest

    Awesome! I didn't see the getmro function in inspect - that'll do the
    trick for me. I should be able to just look up the methodname in each
    of the class's __dict__ attributes.

    vic


    On Fri, 25 Feb 2005 16:29:25 +0100, Peter Otten <> wrote:
    > Victor Ng wrote:
    >
    > > I'm doing some evil things in Python and I would find it useful to
    > > determine which class a method is bound to when I'm given a method
    > > pointer.
    > >
    > > For example:
    > >
    > > class Foo(object):
    > > def somemeth(self):
    > > return 42
    > >
    > > class Bar(Foo):
    > > def othermethod(self):
    > > return 42
    > >
    > >
    > > Is there some way I can have something like :
    > >
    > > findClass(Bar.somemeth)
    > >
    > > that would return the 'Foo' class, and
    > >
    > > findClass(Bar.othermethod)
    > >
    > > would return the 'Bar' class?
    > >
    > > vic

    >
    > >>> import inspect
    > >>> class Foo(object):

    > ... def foo(self): pass
    > ...
    > >>> class Bar(Foo):

    > ... def bar(self): pass
    > ...
    > >>> def get_imp_class(method):

    > ... return [t for t in inspect.classify_class_attrs(method.im_class) if
    > t[-1] is method.im_func][0][2]
    > ...
    > >>> [get_imp_class(m) for m in [Bar().foo, Bar().bar, Bar.foo, Bar.bar]]

    > [<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Foo'>,
    > <class '__main__.Bar'>]
    >
    > but with this approach you will get into trouble as soon as you are using
    > the same function to define multiple methods. There may be something in the
    > inspect module more apt to solve the problem -- getmro() perhaps?
    >
    > Peter
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Victor Ng, Feb 25, 2005
    #3
  4. Victor Ng

    Peter Otten Guest

    Peter Otten wrote:

    >>>> import inspect
    >>>> class Foo(object):

    > ...     def foo(self): pass
    > ...
    >>>> class Bar(Foo):

    > ...     def bar(self): pass
    > ...
    >>>> def get_imp_class(method):

    > ...     return [t for t in inspect.classify_class_attrs(method.im_class)
    > if t[-1] is method.im_func][0][2]
    > ...
    >>>> [get_imp_class(m) for m in [Bar().foo, Bar().bar, Bar.foo, Bar.bar]]

    > [<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Foo'>,
    > <class '__main__.Bar'>]
    >
    > but with this approach you will get into trouble as soon as you are using
    > the same function to define multiple methods. There may be something in


    I think it might be better to demonstrate the problem than just to describe
    it:

    >>> def another(self): pass

    ....
    >>> Foo.alpha = another
    >>> Bar.beta = another
    >>> get_imp_class(Bar.alpha)

    <class '__main__.Foo'>
    >>> get_imp_class(Bar.beta)

    <class '__main__.Foo'>

    A name check won't help either:

    >>> Foo.alpha.__name__

    'another'

    Peter
    Peter Otten, Feb 25, 2005
    #4
  5. Victor Ng

    Roy Smith Guest

    In article <>,
    Victor Ng <> wrote:
    >I'm doing some evil things in Python and I would find it useful to
    >determine which class a method is bound to when I'm given a method
    >pointer.


    I don't know where (or if) it's documented, but im_class seems to give
    you what you want.

    ------
    class Foo(object):
    def x(self):
    return 42

    f = Foo()
    print f.x.im_class
    ------

    king:play$ ./x.py
    <class '__main__.Foo'>

    I have no idea why it's not __imclass__ or some such, but poking
    around with dir() is a great way to explore little nooks and crannies
    like this. I just printed dir(Foo().x) and tried stuff that looked
    interesting until I found what I (you) wanted.
    Roy Smith, Feb 25, 2005
    #5
  6. Victor Ng

    Guest

    Another way is to make a simple metaclass, setting an attribute (like
    defining_class, or something) on each function object in the class
    dictionary.
    , Feb 25, 2005
    #6
  7. Victor Ng

    Victor Ng Guest

    So I went digging through the documentation more and found the following:

    http://docs.python.org/ref/types.html

    There's a section titled "User-defined methods" which covers all the
    im_self, im_class attributes and what they are responsible for.

    vic

    On 25 Feb 2005 10:42:06 -0800, <> wrote:
    > Another way is to make a simple metaclass, setting an attribute (like
    > defining_class, or something) on each function object in the class
    > dictionary.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    ---
    "Never attribute to malice that which can be adequately explained by
    stupidity." - Hanlon's Razor
    Victor Ng, Mar 4, 2005
    #7
    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. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,215
    thushara wijeratna
    Oct 7, 2008
  2. José Joye
    Replies:
    2
    Views:
    649
    José Joye
    Jun 24, 2006
  3. Farshid Lashkari
    Replies:
    2
    Views:
    371
    Paolino
    Jul 28, 2005
  4. Pawel_Iks
    Replies:
    3
    Views:
    524
    terminator
    Jul 31, 2007
  5. Peña, Botp
    Replies:
    1
    Views:
    222
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page