python MRO方法解决顺序 - 钻石关系

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python MRO方法解决顺序 - 钻石关系相关的知识,希望对你有一定的参考价值。

>>> class A(object):
...    def save(self):
...      print("a")
...
>>> class B(object):
...     def b(self):
...       print("b")
...
>>> class B(A):
...    def b(self):
...      print("b")
...
>>> class C(A):
...    def save(self):
...        print("c")

>>> class D(B,C):
...    def d(self):
...       print("d")
...
>>> d = D()
>>> d.save()
c

>>> class E(C,B):
...    def e(self):
...      print("e")
...
>>> class F(D,E):
...     def f(self):
...       print("f")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases C, B
class A:
  def save(self):
      print("a")
      
  def a(self):
    print("a")
      
      
class B(A):
  def b(self):
    print (b)
    
class C(A):
  def save(self):
    print("c")
    
  def c(self):
    print("c")
    
class D(B,C):
  def d(self):
    print("d")
      
class E(C,B):
  def e(self):
    print("e")
    
>>> b = B()
>>> b.save()
a
>>> d = D()
>>> d.save()
c
>>> class F(D, E):
...   def f(self):
...     print("f")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot create a consistent method resolution
order (MRO) for bases B, C
class A:
  def save(self):
      print("a")
      
  def a(self):
    print("a")
      
      
class B(A):
  def b(self):
    print (b)
    
class C(A):
  def save(self):
    print("c")
    
  def c(self):
    print("c")
    
class D(B,C):
  def d(self):
    print("d")
      
class E(C,B):
  def e(self):
    print("e")
    
class F(D, E):
  def f(self):
    print("f")
    
>>> b = B()
>>> b.save()
a
>>> d = D()
>>> d.save()
a
>>> f = F()
>>> f.save()
a    
    
    
    
    
    
    
  
      
      

以上是关于python MRO方法解决顺序 - 钻石关系的主要内容,如果未能解决你的问题,请参考以下文章

super()的用法

Python super() 函数

python 中面向对象编程的几个概念

Python3---常见函数---super()

Method Resolve Order (MRO) - 类对象属性的解析顺序

面向对象中super的作用