[Python-ideas] `OrderedDict.sort`

random832 at fastmail.us random832 at fastmail.us
Tue Sep 24 19:24:55 CEST 2013

On Tue, Sep 24, 2013, at 12:33, Ram Rachum wrote:
> For the record, I think that having a SortedDict in the stdlib would be
> awesome.

There are two issues with that. First of all, this demands that every
element be orderable with every other element. Since not every element
is going to be compared with every other element on insertion, it's easy
to imagine a case where this won't be caught until it's sorted again
later on. And this is ignoring the pathological behavior of
floating-point NaN values, which already silently break list sorting.
(Can someone explain to me how nan works as a dict key, by the way?)

Secondly, a SortedDict (or SortedSet) implies that the sorting is used
_instead of_ hashing, for lookup. This raises the question as to whether
keys/elements should be required to be hashable. On the one hand,
requiring them to be hashable gives you the implied guarantee of an
immutable equality relationship, which is _likely_ to also imply (on
orderable types) an immutable ordering, whereas there is nothing else
that can be used that directly implies an immutable ordering.

More information about the Python-ideas mailing list