【发布时间】:2017-09-20 20:03:12
【问题描述】:
在How does Python's super() work with multiple inheritance? 一个答案解释了这个例子的原因:
class First(object):
def __init__(self):
super(First, self).__init__()
print "first"
class Second(object):
def __init__(self):
super(Second, self).__init__()
print "second"
class Third(First, Second):
def __init__(self):
super(Third, self).__init__()
print "that's it"
答案是:
>>> x = Third()
second
first
that's it
根据解释,是因为:
在
First的__init__内部super(First, self).__init__()调用Second的__init__,因为这是MRO 规定的!
他是什么意思?为什么调用 First super __init__ 会调用 Second 的 __init__?我认为First与Second无关?
据说:“因为那是MRO规定的”,我看了https://www.python.org/download/releases/2.3/mro/ 但还是没有头绪。
谁能解释一下?
【问题讨论】:
-
与名称相反,
super不是指超类。 First 或 Second 是否有任何关系并不重要。 -
好的。我懂了。 super 表示遵循 MRO 指令。
标签: python multiple-inheritance method-resolution-order