作者lv100 (Tsl)
看板Python
标题[问题] 多重继承以及super()
时间Thu Aug 9 23:59:41 2018
各位python版的前辈大家好
最近小弟在自学python
到了多重继承的这边有点小疑问
程式码如下:
class Base(object):
def __init__(self):
print ("enter Base")
print ("leave Base")
class A(Base):
def __init__(self):
print ("enter A")
super(A, self).__init__()
print ("leave A")
class B(Base):
def __init__(self):
print ("enter B")
super(B, self).__init__()
print ("leave B")
class C(A, B):
def __init__(self):
print ("enter C")
super(C, self).__init__()
print ("leave C")
c = C()
输出的是:
enter C
enter A
enter B
enter Base
leave Base
leave B
leave A
leave C
我知道多重继承中
super()调用的顺序是根据MRO列表的顺序
所以到leave Base都可以理解
疑问的点在於leave B->leave A->leave C的顺序
想请问这边程式是怎麽运行才会是如输出的顺序
感谢各位的解答
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.233.167.108
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1533830384.A.72D.html
1F:推 ching4562: 这是多重继承的菱形缺陷,跟呼叫方法顺序MRO有关,虽然 08/10 11:40
2F:→ ching4562: 我没有完全了解,但可以去翻翻 Expert Python Programm 08/10 11:40
3F:→ ching4562: ing 这本书 08/10 11:40
5F:→ ching4562: 这是该主题的原始文件 08/10 11:45
6F:→ izno: thanks a lot 08/11 00:35