subclassing from OrderedDict and defaultdict

后端 未结 2 1571
心在旅途
心在旅途 2021-02-14 03:28

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:15

    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)
    

提交回复
热议问题