博客
关于我
Python菱形继承的初始化问题和继承顺序
阅读量:567 次
发布时间:2019-03-09

本文共 2478 字,大约阅读时间需要 8 分钟。

Python中的菱形继承解决了多层多继承中的初始化和查找方法问题。以下是优化后的内容:


菱形继承的初始化问题

在Python中,类通过多层继承和多继承形成复杂的继承结构时,子类的初始化方法可能被多次调用,导致父类的初始化代码重复执行。例如:

class Electrical(object):    def __init__(self, name):        self.name = name        print('Electrical init')class Phone(Electrical):    def __init__(self, name, price):        Electrical.__init__(self, name)        self.price = price        print('Phone init')class Computer(Electrical):    def __init__(self, name, config):        Electrical.__init__(self, name)        self.config = config        print('Computer init')class HuaWei(Phone, Computer):    def __init__(self, name, price, config):        Phone.__init__(self, name, price)        Computer.__init__(self, name, config)        print('HuaWei init')h = HuaWei('huawei', 100, 'i7')

运行结果显示,Electrical的初始化执行了两次,导致重复打印。这是因为HuaWei同时继承了Phone和Computer,两者都继承了Electrical。在HuaWei的初始化方法中,分别调用了Phone和Computer的初始化方法,而这两种覆盖了Electrical的初始化,导致重复执行。


解决初始化问题的方法

使用super()方法来解决重复初始化的问题。修改后的代码如下:

class Electrical(object):    def __init__(self, name):        self.name = name        print('Electrical init')class Phone(Electrical):    def __init__(self, price, *args):        super(Phone, self).__init__(*args)        self.price = price        print('Phone init')class Computer(Electrical):    def __init__(self, config, *args):        super(Computer, self).__init__(*args)        self.config = config        print('Computer init')class HuaWei(Phone, Computer):    def __init__(self, name, price, config):        super(HuaWei, self).__init__(name, price, config)        print('HuaWei init')h = HuaWei('huawei', 100, 'i7')

运行结果调整为:

Electrical initComputer initPhone initHuaWei init

这样确保了每个父类的初始化只执行一次,只有HuaWei的初始化方法覆盖了两个父类的初始化。


菱形继承的查找顺序

在菱形继承结构中,属性和方法的查找顺序按照广度优先算法进行:

  • 首先检查HuaWei类本身。
  • 如果找不到,检查其最近的父类(Phone)。
  • 如果仍找不到,继续检查第二个父类(Computer)。
  • 最后,如果不在HuaWei和其父类中找到,查找更远的祖先类( Electrical)。
  • 如果依然找不到,查找object类。
  • 例如:

    class HuaWei(Phone, Computer):    passh = HuaWei()h.game()  # 调用Phone的 game 方法h.watch_movie()  # 调用Computer的 watch_movie 方法h.chat()  # 调用Electrical的 chat 方法

    运行结果示:

    Play game in phone!Watch movie in computer!Chat with friend in electrical!

    这表明查找顺序是按照HuaWei → Phone → Computer → Electrical 的顺序进行。


    使用__mro__方法

    在Python中,可以使用__mro__方法查看类的方法解析顺序(MRO),它展示了按照何种顺序查找方法和属性。例如:

    print(HuaWei.__mro__)# 输出结果显示继承顺序:huawei → phone → computer → electrical → object

    这使用户能够清晰地了解类的MRO结构,指导方法和属性的查找过程。


    总结

    通过使用super()方法,确保了菱形继承结构中每个类的初始化只执行一次。同时,理解__mro__方法的作用,明确了方法和属性查找的顺序,这对于编写和维护复杂的继承结构至关重要。正确配置MRO顺序,不仅解决了初始化问题,还确保了属性和方法的检索逻辑清晰易懂。

    转载地址:http://myppz.baihongyu.com/

    你可能感兴趣的文章
    python unicode-escape
    查看>>
    Python unittest mock:是否可以在测试时模拟方法默认参数的值?
    查看>>
    Python unittest单元测试框架 TestSuite测试套件
    查看>>
    PYTHON调离线语音合成并实时播放
    查看>>
    python unittest高级特性!
    查看>>
    Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
    查看>>
    Python urllib/Requests下载文件失败,但浏览器下载失败
    查看>>
    Python urllib2 文件上传问题
    查看>>
    Python urllib2.open 连接由对等错误重置
    查看>>
    python urllib2详解及实例
    查看>>
    Python url请求提示certificate verify failed unable to get local issuer certificate
    查看>>
    Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语或零偏移)
    查看>>
    python valueerror object2_python遇到错误记录
    查看>>
    python vars的作用
    查看>>
    Python vcrpy库:HTTP请求记录和重放
    查看>>
    Python virtualenv
    查看>>
    python vue3实现大文件分段续传(断点续传)--带暂停和继续功能
    查看>>
    Python WebDriver如何打印整个页面源(html)
    查看>>
    Python WebSocket自动化测试:构建高效接口测试框架
    查看>>
    Python Web开发
    查看>>