NaN is weird.
Published March 02, 2026
Last week in the Python Discord we had an unusual discussion about a Python oddity. Specifically concerning float('nan') . It turns out that (rather unsurprisingly when you think about it) float('nan') is hashable.
>>> hash(float('nan')) 274121555
If it is hashable you can put it in a set :
>>> set(float('nan') for _ in range(10)) {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}
But why are there 10 copies of nan in that set ??? A set shouldn't contain duplicates but that set obviously does. This comes down to the simple fact that no two instances of nan equal one another:
>>> float('nan') == float('nan') False
In fact, the same nan doesn't even equal itself!
>>> nan = float('nan') >>> nan == nan False >>> nan is nan True
... continue reading