More than once, Python The subclass inherits all the properties and methods of the parent class. Strictly speaking, the construction method of a class is actually an instance method, so there is no doubt that the construction method of the parent class and the child class will inherit the same.
But we know that Python is an object-oriented programming language that supports multiple inheritance. If a subclass inherits a class instance method with the same name in multiple parent classes, the subclass object will give priority to the instance method in the first parent class when calling the method. Obviously, the same is true of the construction method.
class People: def __init__(self,name): self.name = name def say(self): print("I am a person and my name is:",self.name) class Animal: def __init__(self,food): self.food = food def display(self): print("I am an animal.,I eat",self.food) #The name attribute and say() in People mask the class Person(People, Animal): pass per = Person("zhangsan") per.say() #per.display() # The operation result is //I'm a person. My name is zhangsan
In the above program, the Person class inherits both People and Animal, with People in front. This means that when the per object is created, it will call the constructor inherited from People. Therefore, we can see that the above program creates the per object and assigns a value to the name attribute.
However, if you remove the comment on the last line and run this line of code, the Python interpreter will report the following error:
Traceback (most recent call last): File "D:\python3.6\Demo.py", line 18, in <module> per.display() File "D:\python3.6\Demo.py", line 11, in display print("I am an animal.,I eat",self.food) AttributeError: 'Person' object has no attribute 'food'
This is because the value of the food field needs to be used in the display() method inherited from the Animal class, but because the construction method of the People class "masks" the construction method of the Animal class, when creating the per object, the construction method of the Animal class is not executed, so the program fails.
In turn, if you change line 13 to the following:
class Person(Animal, People)
When the per object is created, a value is passed to the food field. This means that per.display() can execute sequentially, but per.say() will report an error.
In this case, the correct approach is to define the constructor of the Person class itself (equivalent to overriding the constructor of the first direct parent class). But it is important to note that if we define the construction method in the subclass, we must invoke the method of constructing the parent class in this method.
Among the construction methods in the subclass, there are two ways to call the parent construction method, namely:
- A class can be regarded as an independent space. When an instance method is called outside the class, it can be called just like a normal function, except that the class name needs additional remarks (this method is also called unbound method);
- Use the super() function. However, if multiple inheritance is involved, the function can only call the constructor of the first direct parent class.
In other words, when multiple inheritance is involved, there are two ways to call the first parent class to construct a method in the subclass constructor, while the only way to call other parent classes to construct a method is to use unbound methods.
It is worth mentioning that in Python 2.x, the syntax format of super() function is as follows:
Among them, Class value is the Class name of subclass, obj usually refers to self.
However, in Python 3.x, the super() function has a simpler syntax format, which is recommended:
On the basis of mastering the use of super() function, we can try to modify the above program:
class People: def __init__(self,name): self.name = name def say(self): print("I am a person and my name is:",self.name) class Animal: def __init__(self,food): self.food = food def display(self): print("I am an animal.,I eat",self.food) class Person(People, Animal): #Custom construction method def __init__(self,name,food): #Call the construction method of the People class super().__init__(name) #super(Person,self).__init__(name) #Execution effect is the same as the previous line #People.__init__(self,name)#Use unbound method to call People class to construct method #Call the constructor of other parent classes, and manually pass values to self Animal.__init__(self,food) per = Person("zhangsan","Cooked Food") per.say() per.display() # The operation result is //I'm a person. My name is zhangsan //I'm an animal. I eat cooked food
As you can see, in the Person class custom constructor, you can call the People class constructor, use the super() function, or use the unbound method. However, to call the construction method of the Animal class, only unbound methods can be used.