问题描述
使用 super()
和直接使用父类名称之间有区别吗?例如:
Is there a difference between using super()
and using the parent class name directly? For example:
class Parent:
def __init__(self):
print("In parent")
self.__a=10
class Child(Parent):
def __init__(self):
super().__init__() # using super()
Parent.__init__(self) # using Parent class name
c=Child()
super().__ init __()
和之间内部有区别。父.__ init __(自我)
?
推荐答案
不在这种情况下。但一般,尤其是当您使用多重继承时, super()
委托给下一个对象在:
Not in this case. But in general, and especially when you use multiple inheritance, super()
delegates to the next object in the Method Resolution Order (MRO) as is specified in the documentation:
返回一个代理对象,将方法调用委托给a parent或
兄弟类的类型。这对于访问已在类中重写的继承方法
非常有用。搜索顺序与 getattr()
使用的
相同,只是跳过类型本身。
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.
该类型的 __ mro __
属性列出了 getattr()$ c使用的方法解析搜索顺序
$ c>和 super()
。属性
是动态的,只要继承层次结构是
更新,就可以更改。
The __mro__
attribute of the type lists the method resolution search order used by both getattr()
and super()
. The attribute is dynamic and can change whenever the inheritance hierarchy is updated.
(...)
(复制,粗体添加)
比如说你定义了类(借用:
Say for instance you define classes like (borrowed from 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__()
然后 __ mro __
A
是:
A.__mro__ == (A,D,B,C,E,G,H,F,object)
现在,如果我们调用 A()
,它会打印:
Now if we call A()
, it prints:
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 $的上下文中c $ c>
并在尝试获取 __ init __
时:
so it means that in the context of A
and when trying to obtain __init__
that:
-
super().__ init __
ofA
是D. __init __
; -
super().__ init __
ofD
是B .__ init __
; -
super().__ init__
B
是C .__ init __
; -
super().__ init __
C
是E .__ init __
; -
super().__ init __
ofE
isG .__ init __
; -
super().__ init __
ofG
是H .__ init __
; -
super().__ init __
的H
是F .__ init __
;和 -
super().__ init __
F
object .__ init __
。
super().__init__
ofA
isD.__init__
;super().__init__
ofD
isB.__init__
;super().__init__
ofB
isC.__init__
;super().__init__
ofC
isE.__init__
;super().__init__
ofE
isG.__init__
;super().__init__
ofG
isH.__init__
;super().__init__
ofH
isF.__init__
; andsuper().__init__
ofF
isobject.__init__
.
请注意 super()
本身不代表父母。例如 super()
D
是 B
和 B
不是 D
的超类,所以它真的取决于对象的类型(不在课堂上)。
Note thus that super()
does not per se delegates to a parent. For instance the super()
of D
is B
and B
is not a superclass of D
, so it really depends on the type of the object (not on the class).
现在,如果 D
, __ mro__
是:
D.__mro__ = (D,E,G,H,F,object)
如果我们构建 D
但我们得到:
If we construct a D
however we get:
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
它认为:
So in the context of D
it holds that:
-
super().__ init __
D
是E .__ init __
; -
super().__ init __
E
是G .__ init __
; -
super().__ init __
G
H .__ init __
; -
super().__ init __
ofH
是F .__ init __
;和 -
super().__ init __
F
object .__ init __
。
super().__init__
ofD
isE.__init__
;super().__init__
ofE
isG.__init__
;super().__init__
ofG
isH.__init__
;super().__init__
ofH
isF.__init__
; andsuper().__init__
ofF
isobject.__init__
.
所以这里 super( )
D
导致 E
(对于 __ init __
) 在 A
的上下文中不一样。
So here the super()
of D
leads to E
(for __init__
) which is not the same in the context of A
.
这篇关于super()和Parent类名之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!