Skip to content

Instantly share code, notes, and snippets.

@runningzyp
Last active March 10, 2022 02:17
Show Gist options
  • Save runningzyp/6b19e3177e6305172444e42ecb90c8a3 to your computer and use it in GitHub Desktop.
Save runningzyp/6b19e3177e6305172444e42ecb90c8a3 to your computer and use it in GitHub Desktop.
order of python class inheritance
class A(object):
def test(self):
print('from A')
class B(A):
def test(self):
print('from B')
class C(A):
def test(self):
print('from C')
class J(A):
def test(self):
print('from J')
class D(B,J):
def test(self):
print('from D')
class E(C):
def test(self):
print('from E')
class F(D,E):
# def test(self):
# print('from F')
pass
f1=F()
f1.test()
print(F.__mro__) #只有新式才有这个属性可以查看线性列表,经典类没有这个属性
新式类继承顺序:F->D->B->J->E->C->A
经典类继承顺序:F->D->B->A->J->E->C
python3中统一都是新式类
pyhon2中才分新式类与经典类
所有的父类的MRO列表遵循如下原则:
子类会先于父类被检查;多个父类会根据它们在列表中的顺序被检查;如果对下一个类存在两个合法的选择,则选择第一个类。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment