2.1. Method Resolution Order

2.1.1. Inheritance Method Resolution

Listing 2.151. Method Resolution Order
class A:
    def show(self):
        print('a')

class B:
    def show(self):
        print('b')

class C:
    def show(self):
        print('c')

class D(A, B, C):
    pass


obj = D()

obj.show()
# a

print(D.__mro__)
# (<class '__main__.D'>,
#  <class '__main__.A'>,
#  <class '__main__.B'>,
#  <class '__main__.C'>,
#  <class 'object'>)

2.1.2. Inheritance Diamond

../_images/inheritance-diamond.jpg

Figure 2.15. Inheritance Diamond

Listing 2.152. Inheritance Diamond
class A:
    def show(self):
        print('a')


class B(A):
    def show(self):
        print('b')


class C(A):
    def show(self):
        print('c')


class D(B, C):
    pass


obj = D()

obj.show()
# b

print(D.__mro__)
# (<class '__main__.D'>,
#  <class '__main__.B'>,
#  <class '__main__.C'>,
#  <class '__main__.A'>,
#  <class 'object'>)
Listing 2.153. Inheritance Diamond
class A:
    def show(self):
        print('a')


class B(A):
    def show(self):
        print('b')


class C(A):
    def show(self):
        print('c')


class E(B):
    def show(self):
        print('e')


class F(C):
    def show(self):
        print('f')


class G(E, F):
    pass


obj = G()

obj.show()
# e

print(G.__mro__)
# (<class '__main__.G'>,
#  <class '__main__.E'>,
#  <class '__main__.B'>,
#  <class '__main__.F'>,
#  <class '__main__.C'>,
#  <class '__main__.A'>,
#  <class 'object'>)

2.1.3. Assignments

Todo

Create assignments