【发布时间】:2020-07-04 16:15:18
【问题描述】:
我是多级和多级继承的新手。我试图执行一个例子:
class First(object):
def __init__(self):
super().__init__()
print("first")
class Second(object):
def __init__(self):
super().__init__()
print("second")
class Third(First, Second):
def __init__(self):
super().__init__()
print("third")
Third()
我得到的输出是:
second
first
third
<__main__.Third at 0x21bbaf2fc88>
谁能解释一下这个输出的原因? 我期望输出是:
first
second
third
因为第三类的 __init__() 会调用第一类的 __init__()(因为它在父列表中是第一个),它会首先打印第二类的 __init__(),最后是第三类的 __init__()。
MRO 打印出我预期的结果。
print(Third.__mro__)
打印
(__main__.Third, __main__.First, __main__.Second, object)
【问题讨论】:
标签: python python-3.x oop inheritance multiple-inheritance