Why aren't OrderedDicts comparable with < etc?

Terry Reedy tjreedy at udel.edu
Thu Jul 16 14:50:41 EDT 2009

Mark wrote:
> On 16 July, 10:21, Piet van Oostrum <p... at cs.uu.nl> wrote:

>> But why should the order be as if the OrderedDict was a list of tuples.
>> A dict can be considered as a mapping and then you might want to treat
>> either the key or the value as contravariant (the key I guess). So there
>> is ambiguity. Why would the view as a list of tuples for the ordering be
>> the `natural' view?
>> Maybe you may expect some kind of monotonicity such that d1<d2 implies
>> d1[x]<d2[x], but that doesn't work for d1 = {1:10, 2:20} and d2 = {1:15,
>> 2:5}. So maybe there is only a partial ordering?
> OK, that seems to me to be a convincing argument against supporting
> ordering.

To put the above in a slightly different way. OrderedDicts are a 
recently added niche class that Raymond added to what is mostly his 
collections module because there are enough few use cases. There was 
pydev discussion which included the idea, I believe, that they should 
fundamentally be dicts, not lists. Regardless, the justifying use cases 
did not include a need to compare OrderedDicts. The small fraction of 
the few use cases for OrderedDicts that do require comparision can be 
met by extracting the needed sequences and comparing *them* in the 
appropriate manner. The 'appropriate manner' is not likely to always be 
the same. This point may have come up in the discussion, but I would let 
you check for sure if curious.

'Consistency' is a Python design principle, but it is balanced with 
others, so that it is not sufficient reason to add nearly useless 
features. There is a cost to every addition.

Terry Jan Reedy

More information about the Python-list mailing list