ZhangZhihui's Blog  

The order in which Python searches for attributes in base classes is called method resolution order(MRO). It gives a linearized path for an inheritance structure.

Python computes an MRO for every class in the hierarchy; this MRO is computed using the ‘C3 linearization algorithm’. This algorithm is quite complicated, you can check the documentation if you are interested in the details but roughly it works in a depth first, left to right manner, and it searches each class only once. For example, in our previous diamond inheritance example, the Person class can be reached in two ways but it will be looked up only once in MRO.

We can see the MRO for any class using the __mro__ attribute or the mro method or by using the help function. If we have an instance and want to see its MRO dynamically, we can use the __class__ attribute.

Here is the code for the diamond example that we have seen. The classes Student and Teacher inherit from class Person, and the class TeachingAssistant inherits from classes Student and Teacher. All the classes have defined a method named greet.

复制代码
class Person:
    def greet(self):
        print("I am a Person")


class Teacher(Person):
    def greet(self):
        print("I am a Teacher")


class Student(Person):
    def greet(self):
        print("I am a Student")


class TeachingAssistant(Student, Teacher):
    def greet(self):
        print("I am a Teaching Assistant")
复制代码

If we use help on the class TeachingAssistant, we will see MRO for it at the top.

>>> help(TeachingAssistant)

复制代码
Help on class TeachingAssistant in module aaa:

class TeachingAssistant(Student, Teacher)
 |  Method resolution order:
 |      TeachingAssistant
 |      Student
 |      Teacher
 |      Person
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  greet(self)
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from Person:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
(END)
复制代码

 

>>> TeachingAssistant.__mro__
(<class 'aaa.TeachingAssistant'>, <class 'aaa.Student'>, <class 'aaa.Teacher'>, <class 'aaa.Person'>, <class 'object'>)
>>> TeachingAssistant.mro()
[<class 'aaa.TeachingAssistant'>, <class 'aaa.Student'>, <class 'aaa.Teacher'>, <class 'aaa.Person'>, <class 'object'>]
>>> x = TeachingAssistant()
>>> x.__class__.__mro__
(<class 'aaa.TeachingAssistant'>, <class 'aaa.Student'>, <class 'aaa.Teacher'>, <class 'aaa.Person'>, <class 'object'>)

 

posted on   ZhangZhihuiAAA  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-07-30 Python - Modifying Lists/Dictionaries
2023-07-30 Python - Exception Shadowing
2023-07-30 Python - sorted() a tuple
2023-07-30 Python - Conditional Unpacking
2023-07-30 Python - List Anarchy
 
点击右上角即可分享
微信分享提示