我有兩個多重繼承的例子看起來像相同,但我得到differents結果順序。Python的超級()爲多繼承不一致
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"
而當我們」結果ve:
>>> f = Fourth()
second
that's it
>>> Fourth.__mro__
(<class '__main__.Fourth'>, <class '__main__.Second'>, <class '__main__.Third'>
<class '__main__.First'>, <type 'object'>)
正如你所看到的,MRO
流動順序是不同的,在第二個例子是Third
之前沒有達到First
,但在第一個例子是由A
纔去B
通過。
感謝
什麼是你的問題? – Eric 2013-04-23 18:16:00
@Eric:問題是:爲什麼在第二個例子中,它在第三個之前沒有達到第一個,但是在第一個例子中,在到達B之前,它經過了A? – 2013-04-23 18:18:40
@segfolt:是的,如果你關注MRO,你會發現行爲(順序)是不同的,但代碼是相同的,在我看來,MRO ordem應該是相同的。 – 2013-04-23 18:21:05