首先,我必须为没有更好的标题道歉。如果您找到更合适的,请随时更改它。
基本上,我一直被 Python 的多重继承行为所困扰。在我之前的 SO 问题中,我被指示阅读Python 的 C3 MRO。这确实有助于我更好地理解 Python 中的多重继承。就在我以为我掌握了它的时候,我碰到了以下场景,我似乎无法理解。
class UltimateBase(object):
def check(self):
print 'base check'
class AMixin1(UltimateBase):
def check(self):
print 'check A'
class BMixin1(UltimateBase):
def check(self):
print 'check B'
class CMixin1(UltimateBase):
def check(self):
print 'check C'
class AMixin2(UltimateBase):
def check(self):
print 'check A'
return super(AMixin2, self).check()
class BMixin2(UltimateBase):
def check(self):
print 'check B'
return super(BMixin2, self).check()
class CMixin2(UltimateBase):
def check(self):
print 'check C'
return super(CMixin2, self).check()
class MyView1(AMixin1, BMixin1, CMixin1):
pass
class MyView2(AMixin2, BMixin2, CMixin2):
pass
class MyView3(AMixin1, BMixin2, CMixin2):
pass
class MyView4(AMixin2, BMixin1, CMixin2):
pass
class MyView5(AMixin2, BMixin2, CMixin1):
pass
class MyView6(AMixin1, BMixin1, CMixin2):
pass
class MyView7(AMixin1, BMixin2, CMixin1):
pass
class MyView8(AMixin2, BMixin1, CMixin1):
pass
myview1 = MyView1()
myview2 = MyView2()
myview3 = MyView3()
myview4 = MyView4()
myview5 = MyView5()
myview6 = MyView6()
myview7 = MyView7()
myview8 = MyView8()
myview1.check()
print '------------------------'
myview2.check()
print '------------------------'
myview3.check()
print '------------------------'
myview4.check()
print '------------------------'
myview5.check()
print '------------------------'
myview6.check()
print '------------------------'
myview7.check()
print '------------------------'
myview8.check()
print '------------------------'
输出:
check A
------------------------
check A
check B
check C
base check
------------------------
check A
------------------------
check A
check B
------------------------
check A
check B
check C
------------------------
check A
------------------------
check A
------------------------
check A
check B
------------------------
我可以根据观察输出找出一个模式,但它让我不理解这个结果背后的基本原理。
我有一些问题,例如,为什么myview2.check()
返回
check A
check B
check C
base check
不是
check A
base check
在我看来,我错过了关于多重继承的关键部分。请为我填补空白。