Most object-oriented programming languages (except C++) only support single inheritance, not multiple inheritance. Why? Because multiple inheritance not only increases programming complexity, but also easily leads to inexplicable errors.
Although Python supports multiple inheritance grammatically, it is not recommended to use multiple inheritance. Instead, it is recommended to use single inheritance, which can ensure clearer programming ideas and avoid unnecessary troubles.
When a subclass has multiple direct parent classes, the subclass will inherit the methods of all the parent classes, but what happens if there are multiple parent classes containing methods with the same name? At this time, the methods in the front parent class will "shadow" the methods in the back parent class.
The chestnuts are as follows:
# coding=utf-8classItem:
def info(self):print("Method in Item",'This is a commodity')classProduct:
def info(self):print('Method in Product','This is a profitable product')classComputer(Item,Product):
pass
c =Computer()
c.info()
The console prints as follows:
The method in Item, this is a commodity
Explanation: The console prints the methods in the Item class, so if there are methods in the parent class inherited by the subclass, if there is the same name, the former will "mask" the latter, that is, the direct parent class has a higher priority than the latter .
Python extension of knowledge points about multiple inheritance:
Most object-oriented programming languages (except C++) only support single inheritance, not multiple inheritance. Why? Because multiple inheritance not only increases programming complexity, but also easily leads to inexplicable errors.
Although Python supports multiple inheritance grammatically, it is not recommended to use multiple inheritance. Instead, it is recommended to use single inheritance, which can ensure clearer programming ideas and avoid unnecessary troubles.
When a subclass has multiple direct parent classes, the subclass will inherit the methods of all the parent classes, but what happens if there are multiple parent classes containing methods with the same name? At this time, the methods in the front parent class will "shadow" the methods in the back parent class.
The chestnuts are as follows:
# coding=utf-8classItem:
def info(self):print("Method in Item",'This is a commodity')classProduct:
def info(self):print('Method in Product','This is a profitable product')classComputer(Item,Product):
pass
c =Computer()
c.info()
Explanation: The console prints the methods in the Item class, so if there are methods in the parent class inherited by the subclass, if there is the same name, the former will "mask" the latter, that is, the direct parent class has a higher priority than the latter .
The above is the details of whether python supports multiple inheritance. For more information about whether python supports multiple inheritance, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts