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

Roy Smith roy at panix.com
Fri Feb 25 11:06:11 EST 2005


In article <mailman.3053.1109342775.22381.python-list at python.org>,
Victor Ng  <crankycoder at gmail.com> 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.




More information about the Python-list mailing list