Hasty Briefsbeta

Bilingual

NaN Is Weird

5 days ago
  • #Python
  • #Oddities
  • #Programming
  • float('nan') is hashable and can be added to sets and dictionaries.
  • A set with multiple float('nan') entries will show duplicates because nan != nan.
  • float('nan') does not equal itself, but 'is' comparison returns True for the same instance.
  • Using float('nan') as a dictionary key is possible, but retrieving values requires the exact nan instance.
  • Counting occurrences of float('nan') in an iterable with Counter does not work as expected.
  • This behavior is a Python oddity, unlikely to be used deliberately in practice.