在这种情况下不是。但通常,特别是当您使用多重继承时,super()委托给方法解析顺序 (MRO)中的下一个对象,如文档中所指定:
super([type[, object-or-type]])
返回一个代理对象,它将方法调用委托给类型的父类或同级类。这对于访问已在类中重写的继承方法很有用。搜索顺序与 by 使用的getattr()相同,只是跳过了类型本身。
type的__mro__属性列出了getattr()和使用的方法解析搜索顺序super()。该属性是动态的,并且可以在更新继承层次结构时更改。
(...)
(已复制,加粗体)
比如说你定义了像这样的类(借用这个问题,其中更详细地讨论了 MRO):
class F:
def __init__(self):
print('F%s'%super().__init__)
super().__init__()
class G:
def __init__(self):
print('G%s'%super().__init__)
super().__init__()
class H:
def __init__(self):
print('H%s'%super().__init__)
super().__init__()
class E(G,H):
def __init__(self):
print('E%s'%super().__init__)
super().__init__()
class D(E,F):
def __init__(self):
print('D%s'%super().__init__)
super().__init__()
class C(E,G):
def __init__(self):
print('C%s'%super().__init__)
super().__init__()
class B(C,H):
def __init__(self):
print('B%s'%super().__init__)
super().__init__()
class A(D,B,E):
def __init__(self):
print('A%s'%super().__init__)
super().__init__()
那么__mro__ofA是:
A.__mro__ == (A,D,B,C,E,G,H,F,object)
现在,如果我们调用A(),它会打印:
A<bound method D.__init__ of <__main__.A object at 0x7efefd8645c0>>
D<bound method B.__init__ of <__main__.A object at 0x7efefd8645c0>>
B<bound method C.__init__ of <__main__.A object at 0x7efefd8645c0>>
C<bound method E.__init__ of <__main__.A object at 0x7efefd8645c0>>
E<bound method G.__init__ of <__main__.A object at 0x7efefd8645c0>>
G<bound method H.__init__ of <__main__.A object at 0x7efefd8645c0>>
H<bound method F.__init__ of <__main__.A object at 0x7efefd8645c0>>
F<method-wrapper '__init__' of A object at 0x7efefd8645c0>
<__main__.A object at 0x7efefd8645c0>
所以这意味着在试图获得它的上下文中A和时:__init__
super().__init__A是D.__init__; _
super().__init__D是B.__init__; _
super().__init__B是C.__init__; _
super().__init__C是E.__init__; _
super().__init__E是G.__init__; _
super().__init__G是H.__init__; _
super().__init__H是F.__init__; _ 和
super().__init__F是object.__init__。_
因此请注意,它本身super()并不委托给 parent。例如super()ofD是B并且B不是 的超类D,所以它实际上取决于对象的类型(而不是类)。
现在在 的情况下D,__mro__是:
D.__mro__ = (D,E,G,H,F,object)
如果我们构造 aD但是我们得到:
D<bound method E.__init__ of <__main__.D object at 0x7efefd864630>>
E<bound method G.__init__ of <__main__.D object at 0x7efefd864630>>
G<bound method H.__init__ of <__main__.D object at 0x7efefd864630>>
H<bound method F.__init__ of <__main__.D object at 0x7efefd864630>>
F<method-wrapper '__init__' of D object at 0x7efefd864630>
所以在它的上下文中D认为:
super().__init__D是E.__init__; _
super().__init__E是G.__init__; _
super().__init__G是H.__init__; _
super().__init__H是F.__init__; _ 和
super().__init__F是object.__init__。_
所以这里的super()ofD导致E(for __init__)这在 的上下文中A是不一样的。