11

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

from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
  pass
# if pickle support is desired, see original post

I want to do something similar for OrderedDict and defaultdict. But of course, defaultdict has a different __init__ signature, so it requires extra work. What's the cleanest way to solve this problem? I use Python 3.3.

I found a good solution here: https://stackoverflow.com/a/4127426/336527, but I was thinking maybe deriving from defaultdict might make this even simpler?

Community
  • 1
  • 1
max
  • 49,282
  • 56
  • 208
  • 355
  • 3
    While we're on the topic, can someone explain to me how the OrderedDict example actually gets OrderedDict functionality without any explicit `super`-delegation to that class? Is it because of `super` calls somewhere within `Counter` that get re-routed through `OrderedDict` instead of going to `dict` directly as they ordinarily would? Or just what? – Karl Knechtel Mar 31 '12 at 01:02
  • @KarlKnechtel You mean `OrderedCounter`, but good question. – agf Mar 31 '12 at 01:18
  • @agf er, yes, exactly. Stupid edit timeout... :( – Karl Knechtel Mar 31 '12 at 01:19
  • @KarlKnechtel I think the answer is that `Counter` doesn't implement any of the `dict` methods that `OrderedDict` needs to implement to work, and `dict` is the base class of both `Counter` and `OrderedDict`, so normal calls on the instance to those methods in either `OrderedCounter` or `Counter` get delegated to `OrderedDict`. – agf Mar 31 '12 at 01:23
  • possible duplicate of [Can I do an ordered, default dict in Python?](http://stackoverflow.com/questions/6190331/can-i-do-an-ordered-default-dict-in-python) – drs Dec 31 '14 at 13:53

2 Answers2

7

Inheriting from OrderedDict as in the answer you linked to is the simplest way. It is more work to implement an ordered store than it is to get default values from a factory function.

All you need to implement for defaultdict is a bit of custom __init__ logic and the extremely simple __missing__.

If you instead inherit from defaultdict, you have to delegate to or re-implement at least __setitem__, __delitem__ and __iter__ to reproduce the in-order operation. You still have to do setup work in __init__, though you might be able to inherit or simply leave out some of the other methods depending on your needs.

Take a look at the original recipe or any of the others linked to from another Stack Overflow question for what that would entail.

Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238
3

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)
lilydjwg
  • 1,621
  • 20
  • 41