相关代码1:
class A(object):
def show(self):
print ('init A...')
class B(A):
def show(self):
super(B, self).show()
print('init B...')
class C(A):
def show(self):
# super(C, self).show()
print('init C...')
class D(B, C):
def show(self):
super(D, self).show()
print('init D...')
d = D()
d.show()
输出的结果是:
init C...
init B...
init D...
这里想问的是为什么没有经过A,输出init A...
相关代码2:
class A(object):
def show(self):
print ('init A...')
class B(A):
def show(self):
super(B, self).show()
print('init B...')
class C(A):
def show(self):
# super(C, self).show()
print('init C...')
class D(C, B): #继承类和代码1中的顺序相反
def show(self):
super(D, self).show()
print('init D...')
d = D()
d.show()
输出的结果是:
init C...
init D...
这里想问的是为什么B中的方法没有被调用?
还有的就是新式类的MRO算法采用广度优先搜索。在这里是怎么调用的?
http://python.jobbole.com/85685/ 这个讲的挺清楚的。
第一种情况mro是dbca, 第二种是dcba。在第一种,b中调用了super,所以会沿着mro的顺序走到c的show中。而第二种的c中没有调用super所以就结束了。