# 6.MRO和C3算法

## 经典类的MRO

python3以不存在经典类

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


## 新式类的MRO

python中的新式类的MRO是采用的C3算法来完成的

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

3C算法

L(H) = H + L(G) + L(F) + GF

L(G) = G + L(E) + E

L(F) = F + L(D) + L(E) + DE

L(D) = D + L(B) + L(C) + BC

L(B) = B + L(A) + A

L(A) = A

L(E) = E + L(C) + L(A) + CA

L(C) = C + L(A) + A

L(C) = C + A + A #CA

L(E) = E + CA + A #ECA

L(A) = A #A

L(B) = B + A + A #BA

L(D) = D + BA + CA + BC #DBCA

L(F) = F + DBCA + ECA + DE #FDBECA

L(G) = G + ECA + E #GECA

L(H) = H + GECA + FDBECA + GF #HGFDBECA

H->G->F->D->B->E->C->A

(<class '__main__.H'>, <class '__main__.G'>, <class '__main__.F'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

## super

super()可以帮我们执行MRO中下一个父类的方法.

class Base1:
def chi(self):
print("我是Base1")
class Base2:
def chi(self):
print("我是Base2")
class Base3:
def chi(self):
print("我是Base3")
class Bar(Base1,Base2,Base3):
def chi(self):
print("我是Bar1")
super(Bar,self).chi()
print("我是Bar2")
b = Bar()
b.chi()
print(Bar.__mro__)


打印

我是Bar1

(<class '__main__.Bar'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class '__main__.Base3'>, <class 'object'>)

#### 面试题

class Init(object):
def __init__(self, v):
print("init")
self.val = v

def __init__(self, val):
print(self.val)
self.val += 2

class Mult(Init):
def __init__(self, val):
print("Mult")
super(Mult, self).__init__(val) # Haha
self.val *= 5

class HaHa(Init):
def __init__(self, val):
print("哈哈")
super(HaHa, self).__init__(val) # Init
self.val /= 5

pass

class Incr(Pro):
def __init__(self, val):
super(Incr, self).__init__(val)
self.val += 1

a = Incr(5)
print(a.val)
print(b.val)


Add2
Mult

init
5.0
8.0
4