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

    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 :


    that would return the 'Foo' class, and


    would return the 'Bar' class?

    Victor Ng, Feb 25, 2005
    1. Advertisements

  2. Victor Ng

    Peter Otten Guest

    .... def foo(self): pass
    ........ def bar(self): pass
    ........ return [t for t in inspect.classify_class_attrs(method.im_class) if
    t[-1] is method.im_func][0][2]
    [<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 Otten, Feb 25, 2005
    1. Advertisements

  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.


    Victor Ng, Feb 25, 2005
  4. Victor Ng

    Peter Otten Guest

    I think it might be better to demonstrate the problem than just to describe
    <class '__main__.Foo'>

    A name check won't help either:

    Peter Otten, Feb 25, 2005
  5. Victor Ng

    Roy Smith Guest

    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$ ./
    <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
  6. Victor Ng

    percivall Guest

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

    Victor Ng Guest

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

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

    Victor Ng, Mar 4, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.