转载地址:
super的用法是调用继承类的初始化方法,如下面的代码:
1 class A(object): 2 def __init__(self): 3 print 'A __init__' 4 super(A, self).__init__() 5 print 'leave A' 6 7 class C(object): 8 def __init__(self): 9 print 'C __init__'10 super(C, self).__init__()11 print 'leave C'12 13 class B(A,C):14 def __init__(self):15 print 'B __init__'16 super(B, self).__init__()17 print 'leave B'18 19 class D(B):20 def __init__(self):21 print 'D __init__'22 super(D, self).__init__()23 print 'leave D'24 25 if __name__ == '__main__':26 D()
结果:
1 D __init__2 B __init__3 A __init__4 C __init__5 leave C6 leave A7 leave B8 leave D