How to determine from which class the method was inherited?

Is it possible to say from which class the method was inherited. take an example below

class A: def foo(): pass class B(A): def boo(A): pass class C(B): def coo() pass class D(C): def doo() pass >>> dir (D) ['__doc__', '__module__', 'boo', 'coo', 'doo', 'foo'] 

Is there any method to tell me which classes are boo, coo, foo where are inherited from?

+4
source share
1 answer

Use the inspect module:

 class A: def foo(self): pass class B(A): def boo(Aself): pass class C(B): def coo(self): pass class D(C): def doo(self): pass import inspect inspect.classify_class_attrs(D) [('__doc__', 'data', <class __main__.D at 0x85fb8fc>, None), ('__module__', 'data', <class __main__.D at 0x85fb8fc>, '__main__'), ('boo', 'method', <class __main__.B at 0x85fb44c>, <function boo at 0x8612bfc>), ('coo', 'method', <class __main__.C at 0x85fb8cc>, <function coo at 0x8612ca4>), ('doo', 'method', <class __main__.D at 0x85fb8fc>, <function doo at 0x8612f0c>), ('foo', 'method', <class __main__.A at 0x85fb71c>, <function foo at 0x8612f7c>)] 
+6
source

Source: https://habr.com/ru/post/1336106/


All Articles