How does Python's super() work with multiple inheritance?

      Python的super()在多继承下的方法推导顺序(MRO, Method Resolution Order)涉及到C3线性化算法(C3 linearization wiki)。其中关键是,

children precede their parents and the order of appearance in __bases__ is respected.

例子,

from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first encountered'

     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))

     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)

oc = OrderedCounter('abracadabra')
print oc

 

相关链接:

Python’s super() considered super!

MRO: The Python 2.3 Method Resolution Order

posted @ 2013-01-17 15:54  紫红的泪  阅读(467)  评论(0编辑  收藏  举报