当父类有一个 super() 是什么意思? [英] What does it mean when a parent class has a super()?
问题描述
据我所知,super()
用于调用子类中覆盖的方法(就像子类中覆盖的__init__
).
As far as i know, super()
is used to call the methods overwritten in the subclass (like an overwritten __init__
in the subclass).
另外,我知道所有 python 类都继承自一个名为 object
的特殊类.
Also, i know that all python classes inherit from a special class called object
.
在学习 OOP 时,我遇到了一个奇怪的例子:
While studying OOP i ran into a weird example:
class A(object):
def __init__(self):
print('A.__init__')
super().__init__()
class B(object):
def __init__(self):
print('B.__init__')
super().__init__()
class C(A, B):
def __init__(self):
print('C.__init__')
super().__init__()
我理解大部分内容,除了...为什么A类有一个super().__init__()
?它是父类,它不需要从任何人那里继承.特殊的 object
类不做任何事情,所以我不知道为什么需要它.
I understand most of this except...why does class A has a super().__init__()
? It's the parent class, it shouldn't need to inherit from anyone. And the special object
class doesn't do anything so i don't know why it would need it.
这不是我在父类中看到 super
的唯一案例,在我之前提出的一个问题中,一个问题的解决方案涉及它,但我不明白.我也找不到对 super()
继承的这种用法的简单解释.
This is not the only case i have seen a super
in the parent class, in a previous question i made, a solution to aproblem involved it but i didn't understood it. I also can't find an easy explanation of this usage of super()
inheritance.
简单来说...为什么我需要在父类中使用 super()
?
In simple terms...why would i need to use super()
in a parent class?
推荐答案
考虑以下示例:
class A:
def __init__(self, a, **kwargs):
print('A.__init__ called')
self.a = a
super().__init__(**kwargs)
print('A.__init__ finished')
class B:
def __init__(self, b, **kwargs):
print('B.__init__ called')
self.b = b
super().__init__(**kwargs)
print('B.__init__ finished')
class C(A, B):
def __init__(self, c, **kwargs):
print('C.__init__ called')
self.c = c
super().__init__(**kwargs)
print('C.__init__ finished')
当你创建一个C
对象时,输出如下:
When you create a C
object, the output is as follows:
>>> C(a=1, b=2, c=3)
C.__init__ called
A.__init__ called
B.__init__ called
B.__init__ finished
A.__init__ finished
C.__init__ finished
<__main__.C object at 0x7fd15abcab70>
从__init__
方法的调用顺序,以及它们以相反的顺序结束的事实,我们可以看到C.__init__
调用 A.__init__
,后者调用 B.__init__
.也就是说,虽然 A
没有显式的父类(所以它的直接父类是 object
),super().__init__
在 中调用>A
实际上调用了 B.__init__
.
From the order that the __init__
methods are called, and the fact that they finish in reverse order, we can see that C.__init__
calls A.__init__
, which calls B.__init__
. That is, although A
has no explicit parent class (so its direct parent is object
), the super().__init__
call in A
actually calls B.__init__
.
这是因为B
是C
的方法解析顺序 (MRO).A
类需要调用super()
,因为虽然它的超类是object
,但是Python允许多重继承,所以A
> 不能保证它是 MRO 中 object
之前的最后一个类.
This is because B
is the next class after A
in C
's method resolution order (MRO). The A
class needs to call super()
because although its superclass is object
, Python allows multiple inheritance, so A
cannot guarantee that it is the last class before object
in the MRO.
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
参见 Raymond Hettinger 的文章 Python 的 super() 考虑super! 有关 MRO 和协作多继承技术"的更详细说明.
See Raymond Hettinger's article Python's super() considered super! for a more detailed explanation about the MRO and "cooperative multiple inheritance techniques".
这篇关于当父类有一个 super() 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!