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

相关文章:

  • 2021-08-13
  • 2021-05-30
  • 2021-09-10
  • 2022-01-22
  • 2022-02-19
  • 2021-11-08
猜你喜欢
  • 2021-10-20
  • 2022-01-12
  • 2021-10-14
  • 2021-08-25
  • 2022-12-23
  • 2022-01-22
  • 2021-11-13
相关资源
相似解决方案