Python MRO C3算法实现

 

算法实现

#-*- encoding:GBK -*-#
def mro_C3(*cls):
        if len(cls)==1:
            if not cls[0].__bases__:
                return  cls
            else:
                return cls+ mro_C3(*cls[0].__bases__)
        else:
            seqs = [list(mro_C3(C)) for C in cls ] +[list(cls)]
            res = []
            while True:
              non_empty = list(filter(None, seqs))
              if not non_empty:
                  return tuple(res)
              for seq in non_empty:
                  candidate = seq[0]
                  not_head = [s for s in non_empty if candidate in s[1:]]
                  if not_head:
                      candidate = None
                  else:
                      break
              if not candidate:
                  raise TypeError("inconsistent hierarchy, no C3 MRO is possible")
              res.append(candidate)
              for seq in non_empty:
                  if seq[0] == candidate:
                      del seq[0]
 

测试一

#####################测试1
 
ob=object
class A(ob):
    pass
class B(ob):
    pass
 
class X(A,B):
    pass
class Y(A,B):
    pass
try:
    class Z(X,Y):
        pass
    print Z.mro()
    print Z.__mro__
    print mro_C3(Z)
except :pass
 
try:
    print mro_C3(X,Y)#如果从X,Y继承,它的MRO值是什么
except Exception,e:
    print e
 
######打印结果
 
[<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.A'>, <class '__main__.B'>, <type 'object'>]
(<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.A'>, <class '__main__.B'>, <type 'object'>)
(<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.A'>, <class '__main__.B'>, <type 'object'>)
(<class '__main__.X'>, <class '__main__.Y'>, <class '__main__.A'>, <class '__main__.B'>, <type 'object'>)
 
 
 

测试二

#####################测试2
 
ob=object
class A(ob):
    pass
class B(ob):
    pass
 
class X(A):
    pass
class Y(B):
    pass
try:
    class Z(X,Y):
        pass
    print Z.mro()
    print Z.__mro__
    print mro_C3(Z)
except :pass
 
try:
    print mro_C3(X,Y)#如果从A,B继承,它的MRO值是什么
except Exception,e:
    print e
 
####打印结果
 
[<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.A'>, <class '__main__.Y'>, <class '__main__.B'>, <type 'object'>]
(<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.A'>, <class '__main__.Y'>, <class '__main__.B'>, <type 'object'>)
(<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.A'>, <class '__main__.Y'>, <class '__main__.B'>, <type 'object'>)
(<class '__main__.X'>, <class '__main__.A'>, <class '__main__.Y'>, <class '__main__.B'>, <type 'object'>)
 
 

测试三

#####################测试3
 
 
ob=object
class A(ob):
    pass
class B(ob):
    pass
 
class X(A,B):
    pass
class Y(B,A):
    pass
try:
    class Z(X,Y):
        pass
    print Z.mro()
    print Z.__mro__
    print mro_C3(Z)
except :pass
 
try:
    print mro_C3(X,Y)#如果从A,B继承,它的MRO值是什么
except Exception,e:
    print e

##打印结果  报错了,无法 定义 类 Z
inconsistent hierarchy, no C3 MRO is possible
 

你可能感兴趣的:(Python mro C3)