[Python-Dev] Proposal: add odict to collections (original) (raw)

David Wolever wolever at cs.toronto.edu
Sun Jun 15 06:59:51 CEST 2008


On 14-Jun-08, at 8:39 PM, Armin Ronacher wrote:

... I noticed lately that quite a few projects are implementing their own subclasses of dict that retain the order of the key/value pairs. ... I'm +1 on this one too, as there are at least a couple of times in
recent memory when I would have found this useful.

And, as far as questions about the definition of an ordered
dictionary, is there any good reason not to simply treat the dict as
a list? Something like (with the obvious bits left out):

class odict(dict): def init(self, *args): self._order = [] def setitem(self, key, val): if key not in self:
self._order.append(key) def iter(self): return self._order def items(self): return ([item, self[item] for item in self._order]) def sort(self): self._order.sort() ... and so on ...

That way all the order-related functions are well defined, so it
would be hard claim that it doesn't do the "right thing" without
claiming that lists don't do the "right thing".



More information about the Python-Dev mailing list