mixing super and classic calls in Python
首先,让我引用一下"Python编程专家"一书中的一篇文章:
In the following example, a C class that calls its base classes using the __init__ method will
make B class be called twice!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class A(object): def __init__(self): print"A" super(A, self).__init__() class B(object): def __init__(self): print"B" super(B, self).__init__() class C(A,B): def __init__(self): print"C" A.__init__(self) B.__init__(self) print"MRO:", [x.__name__ for x in C.__mro__] #prints MRO: ['C', 'A', 'B', 'object'] C() #prints C A B B |
最后,这里是一个关于这里发生了什么的解释:
This happens due to the A.__init__(self) call, which is made with the C instance,
thus making super(A, self).__init__() call B's constructor. In other words,
super should be used into the whole class hierarchy. The problem is that sometimes
a part of this hierarchy is located in third-party code.
我不知道为什么"
要理解这种行为,您必须了解
如果你把
1 2 3 4 5 6 7 | class C(A,B): def __init__(self): print"C1" A.__init__(self) print"C2" B.__init__(self) print"C3" |
你得到
1 2 3 4 5 6 7 | MRO: ['C', 'A', 'B', 'object'] C1 A B C2 B C3 |
说明了
Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.
当你在
要验证这一点,请添加另一个类"z",并让"c"也从"z"继承。看看会发生什么。
1 2 3 4 5 6 7 8 9 10 11 | class Z(object): def __init__(self): print"Z" super(Z, self).__init__() class C(A, B, Z): def __init__(self): print"C" A.__init__(self) B.__init__(self) Z.__init__(self) |
在这种情况下,