jiahuiding jiahuiding - 24 days ago 11
Python Question

Fix the inheritance network

class A : pass
class B : pass
class C(A) : pass
class D(A,B) : pass
class E(A) : pass
class F(C,D) : pass
class G(B) : pass
class H(E,F,G) : pass


o = H(), I need to draw the complete inheritance network for H. Also, i need to indicate the order in which all its classes are searched for attributes.

below is the picture I drew:

enter image description here

I am not sure if I drew the order correct. can someone help me to fix it?

Answer

type.mro() will give you the order:

>>> type.mro(H)
[__main__.H,
 __main__.E,
 __main__.F,
 __main__.C,
 __main__.D,
 __main__.A,
 __main__.G,
 __main__.B,
 object]

As you can see you have a number of errors in your numbering.
If you need the hierarchy then you can use inspect:

>>> import inspect
>>> inspect.getclasstree(inspect.getmro(H))
[(object, ()),
 [(__main__.A, (object,)),
  [(__main__.C, (__main__.A,)),
   [(__main__.F, (__main__.C, __main__.D)),
    [(__main__.H, (__main__.E, __main__.F, __main__.G))]],
   (__main__.D, (__main__.A, __main__.B)),
   [(__main__.F, (__main__.C, __main__.D)),
    [(__main__.H, (__main__.E, __main__.F, __main__.G))]],
   (__main__.E, (__main__.A,)),
   [(__main__.H, (__main__.E, __main__.F, __main__.G))]],
  (__main__.B, (object,)),
  [(__main__.D, (__main__.A, __main__.B)),
   [(__main__.F, (__main__.C, __main__.D)),
    [(__main__.H, (__main__.E, __main__.F, __main__.G))]],
   (__main__.G, (__main__.B,)),
   [(__main__.H, (__main__.E, __main__.F, __main__.G))]]]]