首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Python MRO C3算法兑现

2012-10-08 
Python MROC3算法实现?算法实现#-*- encoding:GBK -*-#def mro_C3(*cls):if len(cls)1:if not cls[0].__

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=objectclass A(ob):    passclass B(ob):    pass class X(A,B):    passclass Y(A,B):    passtry:    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=objectclass A(ob):    passclass B(ob):    pass class X(A):    passclass Y(B):    passtry:    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=objectclass A(ob):    passclass B(ob):    pass class X(A,B):    passclass Y(B,A):    passtry:    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##打印结果  报错了,无法 定义 类 Zinconsistent hierarchy, no C3 MRO is possible
?

热点排行