Execution order of multiple inheritance parent class constructors

 1 class a:
 2     def __init__(self):
 3         print('a')
 4 
 5 class b(a):
 6     def __init__(self):
 7         super().__init__()
 8         print('b')
 9 
10 class c(a):
11     def __init__(self):
12         super().__init__()
13         print('c' )
 14  
15  
16  class d(b,c):
 17      pass 
18  
19 mm= d()
 20  ''' 
21  a
 22  c
 23  b
 24  ''' 
25  
26  #Analyze this phenomenon 
27  #After inheritance, only one Copy the code of the constructor, the latter will overwrite the former. That is, c's constructor will override b's 
28  
29  print ( ' = ' * 30 )
 30  
31  class a:
 32      def  __init__ (self):
 33          print ( ' a')
34 
35 class b(a):
36     def __init__(self):                      #2
37         super().__init__()                   #2
38         print('b')
39 
40 class c(a):
41     def __init__(self):                        #3
42         super().__init__()                     #3
43         print('c' )                              # 3 
44  
45  class e(b):
 46      def  __init__ (self):
 47          super(). __init__ ()
 48          print ( ' e ' )
 49  
50  class d(e,c):
 51      pass 
52  
53 ddd = d()
 54  
55  ''' 
56  a
 57  c
 58  b
 59  e
 60  ''' 
61  
62  #In the first example, the base classes of b and c are both a
63 #第二个例子中 e和c的基类不同了,但e的基类是b,b和c的基类都是 a ,e类实例化时没有继续往上实例化到a,是因为一个类一次只能实例化一个实例出来?

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325015227&siteId=291194637