The difference between python classic classes and new-style classes

The difference between a classic class and a new-style class
1. The classic class does not inherit the base class by default, while the new-style class inherits object by default
2. The classic class uses the left-to-right depth-first principle for multiple inheritance, while the new-style class It is matched by breadth-first CS algorithm.

3 Classic classes do not have __mro__ and instance.mro() methods, while new-style classes do

Why should you change from a classic class to a new-style class?
Because there are some problems with multiple inheritance in a classic class, it may cause the method query in the inheritance tree to bypass the parent class behind.
An mro list is the mro list that merges all parent classes, and follows The following three principles:
1. The subclass is always in front of the superclass
2. If there are multiple superclasses, they will be sorted according to their order in the list
3. If there are two legal choices for the next class, choose the first one Parent class
Use the best implementation of the mro algorithm:
avoid multiple inheritance,
use super consistently
Do not mix classic classes and new-style classes
Pay attention to check the class hierarchy when calling the parent class

Guess you like

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