I’m wondering about how the presence of IEEE-754 special values affects behaviour of tuples containing them in sets and dicts.
The python3 REPL session below shows that nan in sequences doesn’t prevent them from working as keys.
I tried to track down the spec behaviour, and found this:
Sequences (instances of tuple, list, or range) can be compared only within each of their types, …
Sequences compare lexicographically using comparison of corresponding elements. The built-in containers typically assume identical objects are equal to themselves.
iiuc, that last sentence means that (math.nan,) == (math.nan,) because it’s essentially doing math.nan is math.nan || math.nan == math.nan when comparing corresponding elements.
But the “typically” there seems like it’s an optional optimization; a conforming Python runtime could elect not do that in which case (math.nan,) != (math.nan,) would be spec-compliant.
Am I misreading the documentation? What can one rely on when trying to write Python code that might use tuples containing nan in sets/dicts?
$ python3
Python 3.10.8 (main, Oct 13 2022, 10:17:43) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import nan
>>> nan == nan
False
>>> (nan,) is (nan,)
False
>>> (nan,) == (nan,)
True
>>> s = set()
>>> s.add((nan,))
>>> (nan,) in s
True
>>> s.add(0.0)
>>> 0.0 in s
True
>>> -0.0 in s
True
>>>