【问题标题】:A consistent Method Resolution Order couldn't be created (MRO error) [duplicate]无法创建一致的方法解决顺序(MRO 错误)[重复]
【发布时间】:2019-01-14 21:37:41
【问题描述】:
class Base(object):
    def __init__(self):
        print ("Base")

class childA(Base):
    def __init__(self):
        print ('Child A')
        Base.__init__(self)

class childB(Base,childA):
    def __init__(self):
        print ('Child B')
        super(childB, self).__init__()


b=childB()

继承将成为 childB、Base、childA、Base,在应用 MRO 后,它应该成为 childB、childA、Base。但它会引发 MRO 错误。为什么?

【问题讨论】:

  • 为什么childB继承自BasechildA 已经这样做了,为什么还要这样做两次?

标签: python python-3.6 method-resolution-order


【解决方案1】:

childB 试图从Base 继承两次,一次通过childA,一次直接。通过删除childB 上的多重继承来修复。

class Base(object):
    def __init__(self):
        print ("Base")

class childA(Base):
    def __init__(self):
        print ('Child A')
        Base.__init__(self)

class childB(childA):
    def __init__(self):
        print ('Child B')
        super(childB, self).__init__()

b=childB()

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多