I. Overview
Python supports multiple inheritance, that is, a subclass can inherit multiple parent/base classes. When an attribute is called that is not defined by itself, in what order does it find in the parent class? Especially when many parent classes contain attributes with the same name, it involves the difference between the new class and the classical class.
II. Multiple Inheritance
1 class Food(object): 2 3 def __init__(self, name, color): 4 self.name = name 5 self.color = color 6 7 def eatable(self): 8 print("%s can be eaten." % self.name) 9 10 def appearance(self): 11 print('The color of the %s is %s.' % (self.name, self.color)) 12 13 14 class Fruits(object): 15 16 def __init__(self, name, nutrition): 17 self.name = name 18 self.nutrition = nutrition 19 20 def info(self): 21 print("%s can supply much %s." % (self.name, self.nutrition)) 22 23 24 class Salad(Fruits, Food): # Inheriting multiple parent classes 25 26 def __init__(self, name, nutrition, color, tasty): 27 super(Salad, self).__init__(name, nutrition) 28 Food.__init__(self, name, color) 29 self.tasty = tasty 30 31 def taste(self): 32 print("%s is a little %s." % (self.name, self.tasty)) 33 34 35 obj = Salad('orange', 'VC', 'orange', 'sour') 36 37 obj.eatable() 38 obj.appearance() 39 obj.info() 40 obj.taste()
Salad(Fruits,Food) in the example above inherits two parent classes, Fruits and Food.
supper() function is a method of new class, which requires the top-level parent class to inherit from the object, so that super() function can be used to call init() and other functions of the parent class. Each parent class executes and executes once, and there is no duplication of calls. When using the super() method, the first parent class in the first multiple inheritance is automatically found.
But if you want to continue calling other parent init() functions or the same-name functions of two parent classes, you need to use the method of calling classical classes, that is, the parent class name. _init_ (self, parameter), as in the example above.
3. Inheritance Order of Classical Class VS New Class
3.1 New Class
1. The object class must be inherited when defining a new type of class. If the object class is inherited, it is called a new type of class.
class Fruits(object): 'new-style class' pass
2. Use super() function class to call init() and other functions of parent class
Sup (subclass name, self). _init_ (parameter 1, parameter 2,...)
3. Order of calling the same property or method in the parent class
The order of invocation of the new class is breadth-first query.
Subclasses first query from left to right in all their parent classes, and then query in the parent class of their own parent class if there are no required methods or attributes.
The code is as follows:
1. Call its own properties
1 class A(object): 2 def __init__(self): 3 self.n = "A" 4 5 6 class B(A): 7 8 def __init__(self): 9 super(B, self).__init__() 10 self.n = "B" 11 12 13 class C(A): 14 15 def __init__(self): 16 super(C, self).__init__() 17 self.n = "C" 18 19 20 class D(B, C): 21 22 def __init__(self): 23 super(D, self).__init__() 24 self.n = "D" 25 26 27 d = D() 28 print(d.n) 29 30 #output 31 D
2. Code in Note D, get B
1 class A(object): 2 def __init__(self): 3 self.n = "A" 4 5 6 class B(A): 7 8 def __init__(self): 9 super(B, self).__init__() 10 self.n = "B" 11 12 13 class C(A): 14 15 def __init__(self): 16 super(C, self).__init__() 17 self.n = "C" 18 19 20 class D(B, C): 21 22 # def __init__(self): 23 # super(D, self).__init__() 24 # self.n = "D" 25 pass 26 27 d = D() 28 print(d.n) 29 30 #output 31 B
3. Code in Note B, get C
1 class A(object): 2 def __init__(self): 3 self.n = "A" 4 5 6 class B(A): 7 8 # def __init__(self): 9 # super(B, self).__init__() 10 # self.n = "B" 11 pass 12 13 14 class C(A): 15 16 def __init__(self): 17 super(C, self).__init__() 18 self.n = "C" 19 20 21 class D(B, C): 22 23 # def __init__(self): 24 # super(D, self).__init__() 25 # self.n = "D" 26 pass 27 28 d = D() 29 print(d.n) 30 31 #output 32 C
4. Note C code, get A
1 class A(object): 2 def __init__(self): 3 self.n = "A" 4 5 6 class B(A): 7 8 # def __init__(self): 9 # super(B, self).__init__() 10 # self.n = "B" 11 pass 12 13 14 class C(A): 15 # 16 # def __init__(self): 17 # super(C, self).__init__() 18 # self.n = "C" 19 pass 20 21 22 class D(B, C): 23 24 # def __init__(self): 25 # super(D, self).__init__() 26 # self.n = "D" 27 pass 28 29 d = D() 30 print(d.n) 31 32 #output 33 A
3.2 Classical Classes
1. Classic Class Definition, Nothing Inheritance
class Fruit: 'Classical Classes' pass
2. Inheritance of functions or attributes such as init() of the parent class
Parent class name. _init_ (self, parameter 1, parameter 2,...)
3. Order of calling the same property or method in the parent class
In Python 3, the order of multiple inherited queries is breadth-first.
Classic class call order is: depth first query
Subclasses are queried in such order as the parent of the parent class, and if not, they return to find another parent class.
The code is as follows:
1. Call its own properties
1 class A:#Classical Classes 2 def __init__(self): 3 self.n = "A" 4 5 class B(A): 6 pass 7 def __init__(self): 8 self.n = "B" 9 10 class C(A): 11 def __init__(self): 12 self.n = "C" 13 14 class D(B,C): 15 def __init__(self): 16 self.n = "D" 17 18 d = D() 19 print(d.n) 20 21 #output 22 D 23 24 All code
2. Code in Note D, get B
1 class A: 2 def __init__(self): 3 self.n = "A" 4 5 class B(A): 6 def __init__(self): 7 self.n = "B" 8 9 class C(A): 10 def __init__(self): 11 self.n = "C" 12 13 class D(B,C): 14 pass 15 16 d = D() 17 print(d.n) 18 19 #output 20 B
3. Code in Note B, get A
1 class A: 2 def __init__(self): 3 self.n = "A" 4 5 class B(A): 6 pass 7 8 class C(A): 9 def __init__(self): 10 self.n = "C" 11 12 class D(B,C): 13 pass 14 15 d = D() 16 print(d.n) 17 18 #output 19 A
4. Note A code, get C
1 class A: 2 pass 3 4 class B(A): 5 pass 6 7 class C(A): 8 def __init__(self): 9 self.n = "C" 10 11 class D(B,C): 12 pass 13 14 d = D() 15 print(d.n) 16 17 #output 18 C
IV. SUMMARY
1. New classes inherit object classes, while classical classes do not inherit any classes.
2. New classes are constructed by super keyword inheritance, while classical classes are inherited by parent class. _init(self)
3. New-style class: breadth-first query, classic class: depth-first query (because new-style class is new, so look for the latest, the latest; then classic is old, so far and deeper)
4. It is worth noting that we did the above in Python 2. In Python 3, both classical and new types of queries are breadth-first queries, and the deep queries in abandoned 2 have been used.