同じように見える多重継承の 2 つの例がありますが、結果の順序が異なります。
class A(object):
def t(self):
print 'from A'
class B(object):
def t(self):
print 'from B'
class C(A): pass
class D(C, B): pass
その結果、次のようになりました。
>>> d = D()
>>> d.t() # Will print "from A"
>>> D.__mro__
(<class '__main__.D'>, <class '__main__.C'>, <class '__main__.A'>,
<class '__main__.B'>, <type 'object'>)
class First(object):
def __init__(self):
print "first"
class Second(First):
def __init__(self):
print "second"
class Third(First):
def __init__(self):
print "third"
class Fourth(Second, Third):
def __init__(self):
super(Fourth, self).__init__()
print "that's it"
その結果、次のようになりました。
>>> f = Fourth()
second
that's it
>>> Fourth.__mro__
(<class '__main__.Fourth'>, <class '__main__.Second'>, <class '__main__.Third'>
<class '__main__.First'>, <type 'object'>)
ご覧のとおり、 のフロー順序が異なり、2 番目の例では前にMRO
到達しませんが、1 番目の例ではに行く前に通過します。First
Third
A
B