在这种情况中没有。但是一般而言,尤其是当您使用多重继承时,super() 会委托给 方法解析顺序 (MRO) 中的下一个对象 em> 在documentation 中指定:
super([type[, object-or-type]])
返回一个代理对象,将方法调用委托给父级或
类型的兄弟类。这对于访问继承的方法很有用
已在类中被覆盖。搜索顺序同上
getattr() 使用,除了类型本身被跳过。
类型的__mro__属性列出了getattr()和super()使用的方法解析搜索顺序。属性
是动态的并且可以在继承层次结构改变时改变
更新了。
(...)
(复制,加粗)
比如说你定义了类似的类(借用自this question, where the MRO is discussed in more detail):
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__()
那么A的__mro__就是:
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__;
-
D 中的super().__init__ 是B.__init__;
-
super().__init__ 的B 是C.__init__;
-
super().__init__ 的 C 是 E.__init__;
-
super().__init__ 的E 是G.__init__;
-
super().__init__ 的G 是H.__init__;
-
H 的super().__init__ 是F.__init__;和
-
super().__init__ 的 F 是 object.__init__。
因此请注意,super()本身并不代表父母。例如D 的super() 是B 和B 不是D 的超类,所以它真的取决于对象的类型(不取决于类) .
现在对于D,__mro__ 是:
D.__mro__ = (D,E,G,H,F,object)
如果我们构造一个D,我们会得到:
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的上下文中认为:
-
D 中的super().__init__ 是E.__init__;
-
super().__init__ 的E 是G.__init__;
-
super().__init__ 的G 是H.__init__;
-
super().__init__ 的H 是F.__init__;和
-
super().__init__ 的 F 是 object.__init__。
所以这里D 的super() 导致E(对于__init__)这在A 的上下文中不一样 .