转载地址:
http://blog.csdn.net/cxm19830125/article/details/20610533
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 C 6 leave A 7 leave B 8 leave D