我有两个看起来相同的多重继承示例,但我得到的结果顺序不同。
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'>)
如您所见, 的流程顺序MRO
不同,在第二个示例中它没有到达First
之前,Third
但在第一个示例中它在到达之前A
经过B
。