The previous article describes the concept of multiple inheritance, syntax, code drills, and precautions for use. If you are interested, you can read it. This article is based on the extended popular science of the previous article. Just know it.
1. MRO in Python - method search order [multiple inheritance order]
The so-called method search order refers to the order in which the python interpreter searches for methods between the class that created the object and the parent class when an object calls a method. There is a built-in attribute mro for classes in python to view the method search order. (two underscores before and after mro)
MRO is the abbreviation of method resolution order, which is mainly used to judge the calling path of methods and properties in multiple inheritance.
Determine the order in which the methods of the C class object are called: the code in the previous article is unchanged, and the output is later: print(C.__mro__)
Specific code:
class A:
def test(self):
print("A --- test方法")
def demo(self):
print("A --- demo方法")
class B:
def demo(self):
print("B --- demo方法")
def test(self):
print("B --- test方法")
class C(B, A):
pass
# 创建子类对象
c = C()
c.test()
c.demo()
print(C.__mro__)
Results of the:
Result process description:
A tuple is output to the console. When the object method created by class C is used, the Python interpreter will first look for this method in class C, and if there is one, it will be executed directly without searching backwards. If this method is not found, it will look for this method in the second class from left to right in the order of the tuple. If it is found in class B, it will be executed directly without searching backwards. If it is not found, it will continue to look down in the order from left to right. In the third class, class A will continue to find whether this method is provided. If it is found, it will be executed. If it is not found, it will continue to look for the next class object. class, an error will be reported if the program has not been found.
(In python3, object is the base class of all classes, that is, as long as a class is defined, the final base class of this class is the object class. In short, object is the ancestor class of all classes in python)
Summarize:
When searching for a method, it is searched in the order from left to right according to the output result of __mro__. If a method is found in the current class, it will be executed directly, no more searching. If not found, it will be searched to see if there is a corresponding method in the next class , if found, execute directly, no longer search If the last class is found, the method has not been found, the program reports an error
2. New and old (classic) classes [Popular Science]
Object is the base class provided by python for all objects, providing some built-in properties and methods, which can be viewed using the dir function.
New-style class:
A class with object as the base class is recommended.
Classic class:
Classes not based on object are not recommended.
Differences between new-style classes and classic classes in python2.x and Python3.x:
When defining a class in python3.x, if no parent class is specified, object will be used as the base class of the class by default. The classes defined in python3.x are all new-style classes. When defining a class in python2.x, if no parent class is specified, object will not be the base class.
Code experience, as shown in the figure:
- Based on Python2.0
2. Based on Python3.x
New-style classes and classic classes will affect the search order of methods when multiple inheritance occurs. Note: In order to ensure that the written code can run in python2.x and python3.x at the same time, when defining a class in the future, if there is no parent class, it is recommended to inherit from object uniformly
class 类名(object):
pass
For more related Python tutorials, you can go to the Python Self-Learning Network (www.wakey.com.cn/), starting from the basic introductory free course, and gradually learn the python full-stack system course in depth, suitable for beginners to proficient in full-stack development.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。