Answers:
跟着...:
>>> class A(object): pass
...
>>> A.__mro__
(<class '__main__.A'>, <type 'object'>)
>>> class B(A): pass
...
>>> B.__mro__
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
>>> class C(A): pass
...
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <type 'object'>)
>>>
只要我们具有单一继承,__mro__
它就只是该类的元组:类,其基础,其基础的基础,依此类推object
(当然,仅适用于新型类)。
现在,具有多重继承...:
>>> class D(B, C): pass
...
>>> D.__mro__
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <type 'object'>)
...您还可以确保,在中__mro__
,没有类被重复,并且没有类在其祖先之后出现,除了首先进入相同多重继承级别的类(如本例中的B和C)之外,__mro__
左到右。
从类的实例(而不是方法)获得的每个属性在概念上都会沿进行查找__mro__
,因此,如果祖先中有多个类定义了该名称,则可以告诉您在哪里可以找到该属性-在第一个类中在__mro__
定义该名称。
这也许会显示解决的顺序。
class A(object):
def dothis(self):
print('I am from A class')
class B(A):
pass
class C(object):
def dothis(self):
print('I am from C class')
class D(B, C):
pass
d_instance= D()
d_instance.dothis()
print(D.mro())
和响应将是
I am from A class
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
该规则是深度优先的,在这种情况下,其含义是D,B,A,C。
在搜索继承的类时,Python通常使用深度优先的顺序,但是当两个类从同一个类继承时,Python将从mro中删除对该类的首次提及。
钻石继承的解决顺序将有所不同。
class A(object):
def dothis(self):
print('I am from A class')
class B1(A):
def dothis(self):
print('I am from B1 class')
# pass
class B2(object):
def dothis(self):
print('I am from B2 class')
# pass
class B3(A):
def dothis(self):
print('I am from B3 class')
# Diamond inheritance
class D1(B1, B3):
pass
class D2(B1, B2):
pass
d1_instance = D1()
d1_instance.dothis()
# I am from B1 class
print(D1.__mro__)
# (<class '__main__.D1'>, <class '__main__.B1'>, <class '__main__.B3'>, <class '__main__.A'>, <class 'object'>)
d2_instance = D2()
d2_instance.dothis()
# I am from B1 class
print(D2.__mro__)
# (<class '__main__.D2'>, <class '__main__.B1'>, <class '__main__.A'>, <class '__main__.B2'>, <class 'object'>)
class B3
,但在第二种情况下的转到class A
后class B1