After evaluating the following code:
class A():
def method1():
pass
def method2():
pass
class B(A):
def method3():
pass
def method4():
pass
class C(B):
def method1(): # notice the overriding of A.method1
pass
def method5():
pass
myC = C()
import inspect
# Find all methods
print [attr for attr in inspect.getmembers(myC) if callable(attr[1])]
[('method1', bound method C.method1 of <builtin.C instance at 0x079EA0D0>), ('method2', bound method C.method2 of <builtin.C instance at 0x079EA0D0>), ('method3', bound method C.method3 of <builtin.C instance at 0x079EA0D0>), ('method4', bound method C.method4 of <builtin.C instance at 0x079EA0D0), ('method5', builtin.C instance at 0x079EA0D0>)]
How to retrieve the origin of the methods ?
- method1 and method5 come directly from class C definition
- method3 and method4 come from subclass B of class C
- method2 comes from subclass A of subclass B of class C.
For those who like to known the final goal, I want to display help for the methods directly defined in class C, not in subclasses.
You can check if the attribute is present in
myC
's class__dict__
: