subclassing from OrderedDict and defaultdict

后端 未结 2 675
广开言路
广开言路 2021-02-14 03:59

Raymond Hettinger showed a really cool way to combine collection classes:

from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict)         


        
2条回答
  •  一生所求
    2021-02-14 04:05

    I've found a way to subclass them both, but not sure if there are bugs:

    class OrderedDefaultDict(defaultdict, OrderedDict):
        def __init__(self, default, *args, **kwargs):
            defaultdict.__init__(self, default)
            OrderedDict.__init__(self, *args, **kwargs)
    

提交回复
热议问题