首页 > 编程语言 >Python - Method Resolution Order (MRO)

Python - Method Resolution Order (MRO)

时间:2024-07-30 22:06:02浏览次数:9  
标签:__ Python Person MRO Student TeachingAssistant Resolution class

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'>)

 

标签:__,Python,Person,MRO,Student,TeachingAssistant,Resolution,class
From: https://www.cnblogs.com/zhangzhihui/p/18333443

相关文章

  • 计算机毕业设计选题推荐-零食批发商仓库管理系统-Java/Python项目实战
    ✨作者主页:IT研究室✨个人简介:曾从事计算机专业培训教学,擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。☑文末获取源码☑精彩专栏推荐⬇⬇⬇Java项目Python项目安卓项目微信小程序项目......
  • 【自动化测试必学语言】python:语言基础
    目录Python介绍语言的分类注释单行注释多行注释变量定义变量使用变量变量名的命名规范数据类型数字类型非数字类型type()函数input输入print输出格式化输出快捷键(小操作)运算符算术运算符 比较运算符Python介绍作者:吉多·范罗苏姆(Guidov......
  • Python基础知识笔记——常用函数
    一、range()函数range()函数用于生成一个整数序列。它通常用于循环结构中,例如for循环,以提供循环的迭代次数。range()函数可以有1到3个参数。#range(start,stop,step)range(2,6,2)#生成从2开始,到6结束(不包括6),步长为2的一串数字#参数指定不完全时,默认从0开始,步长......
  • [python] 启发式算法库scikit-opt使用指北
    scikit-opt是一个封装了多种启发式算法的Python代码库,可以用于解决优化问题。scikit-opt官方仓库见:scikit-opt,scikit-opt官网文档见:scikit-opt-doc。scikit-opt安装代码如下:pipinstallscikit-opt#调用scikit-opt并查看版本importskosko.__version__'0.6.6'0背景介......
  • 门控循环单元(GRU)预测模型及其Python和MATLAB实现
    ##一、背景循环神经网络(RNN)是处理序列数据的一类神经网络,尤其适用于时间序列预测、自然语言处理等领域。然而,传统的RNN在长序列数据的训练中面临梯度消失和爆炸的问题,导致模型对长期依赖的学习能力不足。为了解决这一问题,研究人员提出了多种改进的RNN结构,其中包括长短期记忆......
  • 长短期记忆网络(LSTM)预测模型及其Python和MATLAB实现
    ##LSTM预测模型背景长短期记忆网络(LongShort-TermMemory,LSTM)是一种特殊的递归神经网络(RNN),于1997年首次由SeppHochreiter和JürgenSchmidhuber提出。LSTM主要用于处理和预测序列数据中的时间依赖关系,能够有效地解决传统RNN在处理长序列时遇到的梯度消失和梯度爆炸问题。因......
  • 2024年华为OD机试真题-结队编程 -(C++/Java/python)-OD统一考试(C卷D卷)
     2024华为OD机试真题目录-(B卷C卷D卷)-【C++JavaPython】题目描述某部门计划通过结队编程来进行项目开发,已知该部门有N名员工,每个员工有独一无二的职级,每三个员工形成一个小组进行结队编程,结队分组规则如下:从部门中选出序号分别为i、j、k的3名员工,他们的职级分贝为......
  • Centos7 安装 Python3环境,兼容python2
     一、安装Python3查看是否已经安装Python。Centos7默认安装了python2.7.5.因为一些命令要用它比如yum它使用的是python2.7.5。使用python-V命令查看一下是否安装Python。 然后使用命令whichpython查看一下Python可执行文件 安装依赖yuminstallzlib-develbz......
  • Python 69个 常用 内置函数 之 总结+实例 篇
    1.abs()返回一个数的绝对值print(abs(-5))#输出5print(abs(5))#输出52.dict()创建一个字典my_dict=dict(name="Alice",age=25)print(my_dict)#输出{'name':'Alice','age':25}3.help()用于获取关于对象的帮助信息help(......
  • Python面向对象浅析
    目录面向对象基本概念一、类和对象类和对象是面向对象骗程的两个核心概念。在程序开发中,要设计一个类,通常需要满足一下三个要素:self详解:对象(Object)魔法方法:类里的一些特殊方法__init__和__del__方法:repr__和__str运算符的相关魔法方法:__eq__方法类属性和对象属......