Does Python support 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()

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

Does Python support multiple inheritance?
How does Python output integers
How does python output backslashes
Talking about inheritance in Python
How does python update packages
How does Python list update value
How does python change the environment
Why doesn't Python support function overloading?
What does rc1 mean in python
What does def in python do
Using Python to implement multiple clipboards
How does python call java classes
How does python import dependency packages
How does python enter interactive mode
What does np do in python
How does Python generate xml files
How does python determine prime numbers