NaN Is Weird

Python’s mystery number multiplies in sets and haunts your keys — cue the comment war

TLDR: Python’s NaN can be stored in sets and dicts but doesn’t equal itself, causing duplicates and “ghost” keys. Comments split between “this is broken” and “this is by design,” with war stories from Rust and C++—a reminder that this quirky math value can quietly booby-trap your code.

Python’s “not-a-number” (NaN) just crashed the vibes in the Python Discord: it’s hashable, shows up ten times in a set, won’t even equal itself, and makes dictionary keys you can put in but can’t get back out. Devs called it a “ghost key” and a “Schrödinger number” that refuses commitment. The loudest gasp came from paulddraper, who branded it a “perversion of equality,” pointing out how some languages blow up on divide-by-zero yet calmly hand you NaN for other math fails. Meanwhile, adampunk rode in with the contrarian take: NaN is fine, it’s the hidden “payloads” that are wild—linking to The Secret Life of NaN—before admitting, yeah, NaN is weird but maybe necessarily so. The cross-language tea spilled fast: Rust folks chimed in that Rust splits “kinda-equal” from “truly equal” types to keep NaNs from breaking maps, and C++ veterans warned that trying to sort a list with NaNs can cause undefined behavior—yes, the dreaded crash. The bonus drama: hashing confusion. One commenter said the unstable NaN hash “feels like a bug,” while others shrugged: this is IEEE math land, not Disneyland. Verdict from the thread? NaN is that chaotic friend—fun at parties, terrible with your house keys, and guaranteed to start an argument.

Key Points

  • Python’s float('nan') is hashable, returning an integer from hash().
  • NaN is not equal to any value, including itself (float('nan') == float('nan') is False).
  • Python sets can contain multiple distinct NaN entries because equality checks fail to deduplicate them.
  • Dictionaries can store multiple NaN keys; retrieving a value requires referencing the exact NaN instance used as the key.
  • collections.Counter treats each generated NaN as a separate key, each counted once.

Hottest takes

“NaN == NaN is truly a perversion of equality” — paulddraper
“NaN is normal*, NaN PAYLOADS are weird” — adampunk
“It feels like a bug.” — kubb
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.