Can I find out (dynamically) where a method is defined?

allendowney at allendowney at
Mon Jun 9 17:28:45 CEST 2008

Hi All.

In a complex inheritance hierarchy, it is sometimes difficult to find
where a
method is defined.  I thought it might be possible to get this info
from the
method object itself, but it looks like maybe not.  Here is the test
case I tried:

class A(object):
    def method():

class B(A):

a = A()
b = B()

print a.method
print b.method

Since B inherits method from A, I thought that printing b.method might
me that the definition is in A, but no.  Here's the output:

<bound method A.method of <__main__.A object at 0xb7d55e0c>>
<bound method B.method of <__main__.B object at 0xb7d55e2c>>

This in indistinguishable from the case where B overrides method.

So, is there any way to inspect a method to see where (in what class)
is defined?


More information about the Python-list mailing list