Python中具有多重继承的调用解析顺序
我正在观看一个Python演讲,发现了一个有趣的语言特性。然而,当我试图运行一个测试代码时,它不起作用,我想知道为什么 我希望这张照片能打印出来:Python中具有多重继承的调用解析顺序,python,multiple-inheritance,Python,Multiple Inheritance,我正在观看一个Python演讲,发现了一个有趣的语言特性。然而,当我试图运行一个测试代码时,它不起作用,我想知道为什么 我希望这张照片能打印出来: Parent OtherParent 但我得到的却是: Parent Parent 示例代码: class Parent: def get_message(self): return "Parent" class Child(Parent): def do_print(self): print(s
Parent
OtherParent
但我得到的却是:
Parent
Parent
示例代码:
class Parent:
def get_message(self):
return "Parent"
class Child(Parent):
def do_print(self):
print(super().get_message())
class OtherParent:
def get_message(self):
return "OtherParent"
class OtherChild(Child, OtherParent):
pass
Child().do_print()
OtherChild().do_print()
编辑:运行在Windows、Python 3.5.1、Anaconda 4.0.0(64位)啊,我的朋友指出了与Youtube上示例的一个小偏差:
class Parent:
def get_message(self):
return "Parent"
class Child(Parent):
def do_print(self):
print(super().get_message())
class OtherParent(Parent): # <----- Inheritance of Parent makes it work
def get_message(self):
return "OtherParent"
class OtherChild(Child, OtherParent):
pass
Child().do_print()
OtherChild().do_print()
类父级:
def get_消息(自我):
返回“父项”
班级儿童(家长):
def do_打印(自):
打印(super().get_message())
类OtherParent(Parent):#问题的注释中提到了正确的解释,即来自OtherChild
类的MRO(注释中的链接:)
根据不同的继承,查看OtherChild
类的MRO的不同输出:
OtherParent
没有父类:
class OtherParent():
def get_message(self):
return "OtherParent"
print(OtherChild.__mro__)
Child().do_print()
OtherChild().do_print()
输出显示Parent
位于OtherParent
之前:
(<class '__main__.OtherChild'>, <class '__main__.Child'>, <class '__main__.Parent'>, <class '__main__.OtherParent'>, <class 'object'>)
Parent
Parent
class OtherParent(Parent):
def get_message(self):
return "OtherParent"
(<class '__main__.OtherChild'>, <class '__main__.Child'>, <class '__main__.OtherParent'>, <class '__main__.Parent'>, <class 'object'>)
Parent
OtherParent
输出显示OtherParent
现在位于Parent
之前:
(<class '__main__.OtherChild'>, <class '__main__.Child'>, <class '__main__.Parent'>, <class '__main__.OtherParent'>, <class 'object'>)
Parent
Parent
class OtherParent(Parent):
def get_message(self):
return "OtherParent"
(<class '__main__.OtherChild'>, <class '__main__.Child'>, <class '__main__.OtherParent'>, <class '__main__.Parent'>, <class 'object'>)
Parent
OtherParent
输出显示OtherParent
再次出现在Parent
之前,但也出现在Child
之前:
(<class '__main__.OtherChild'>, <class '__main__.OtherParent'>, <class '__main__.Child'>, <class '__main__.Parent'>, <class 'object'>)
Parent
Parent
(,)
父母亲
父母亲
也许有人可以解释最后一种情况,这种情况乍一看是不自然的。你的python版本是什么?print(OtherChild.\uuuuMRO\uuuuuuuu)
应该回答你关于解释行为的问题+1。有关影响其他孩子的混合顺序的答案,请参阅(简短回答:“MRO基本上是深度优先,从左到右”)。另外,还有一篇文章讨论了同时使用super()
和混合的可能混淆。这并不能真正解释为什么第一个代码不能按预期工作,或者这一变化是如何解决的。不幸的是,我不知道Python的内部工作原理,无法详细了解答案。哦,你是OP-那么你可以将这一点添加到问题中,而不是将其作为答案发布:“如果我使其他父项
继承自父项
,它会按预期工作,但我不明白为什么”为True,虽然我的问题更多的是关于“为什么这段代码不能像广告中所说的那样工作”,答案是我没有正确地实现它。事后看来,我可能已经删除了这个问题。但是如果你不理解其中的区别,为什么添加继承会改变行为,那么你可能仍然有一个问题。父母都没有do\u print
,它只在Child
类中定义,你的意思是get\u message
?但即使这样,我也不太明白解释。@NikoNyrh那你为什么接受它?!也许最好完全删除这个问题(您需要拒绝并取消注释这个答案),阅读MRO订单上的链接问题,然后再次询问您是否仍然不确定。就目前而言,这整件事都是一场车祸。抱歉,在详细阅读之前,我太急于接受看似合理的答案了:/I我无法删除该问题,因为它已经有了答案。下次我会在问问题之前做更多的研究。@NikoNyrh我更新了我的编辑,添加了一些解释很好的例子(我希望:)