为什么 nan 的哈希为零?

Why is hash of nan zero?

我还以为

>>> hash(0)
0
>>> import math
>>> hash(math.nan)
0

会导致频繁的哈希冲突。为什么它们都散列为零?

This behaviour has changed in Python 3.10:

  • Hashes of NaN values of both float type and decimal.Decimal type now depend on object identity. Formerly, they always hashed to 0 even though NaN values are not equal to one another. This caused potentially quadratic runtime behavior due to excessive hash collisions when creating dictionaries and sets containing multiple NaNs. (Contributed by Raymond Hettinger in bpo-43475.)