[Python-ideas] `OrderedDict.sort`

Andrew Barnert abarnert at yahoo.com
Wed Sep 25 18:35:14 CEST 2013

On Sep 25, 2013, at 6:47, random832 at fastmail.us wrote:

> On Tue, Sep 24, 2013, at 23:27, Andrew Barnert wrote:
>> Yes, every key must be comparable with every other key, and the
>> comparison must define a strict weak order, and the keys must be
>> comparison-immutable, and there's no way to test either of those
>> automatically. By comparison, a dict needs hashable keys, which can be
>> tested automatically, and equality-immutable and hash-immutable keys,
>> which can't really be tested but in practice hash is an acceptable test.
> I think of this as part of the hashable protocol, whereas we know that
> lists are orderable despite being mutable.

Please read the blog post rather than the one-line summary if you want to discuss the contents.

>> But it's no worse than many other requirements in the stdlib that can't
>> be tested automatically.
>> And yes, NaN is a problem, but it's exactly the same problem it is
>> everywhere else in Python.
> I was serious about wanting to know how dictionaries handle NaN as a
> key. Is it a special case? The obvious way of implementing it would
> conclude it is a hash collision but not a match.

I believe that, at least in CPython and PyPy, a hash collision is a match if they're identical or equal, which is why NaN values work, and why float("nan") and Decimal("nan") aren't matches, and so on.

But is there anything in the documentation that requires this, or is it just a side effect of implementation specifics? I don't know.

> I notice that
> Decimal('NaN') and float nan don't match each other (as do any other
> float/Decimal with the same value) but they do both work as dictionary
> keys.

More information about the Python-ideas mailing list