6

I found the following lines in the json/encoder.py module:

if o != o:
   text = 'NaN'

In what situation is an object not equal to itself?

Boann
  • 47,128
  • 13
  • 114
  • 141
dzieciou
  • 3,379
  • 6
  • 35
  • 67

2 Answers2

7

This can happen in the case of floating point numbers that adhere to IEEE 754 standard. See Why is NaN not equal to NaN?

By definition, a value of NaN ("not a number") is unequal to itself.

lukeg
  • 3,901
  • 3
  • 18
  • 39
5

The question seems to be about NaN, but it's worth mentioning that you can define the comparison method __eq__ in a custom class.

For example you could make it always false:

class NotEqual:
    def __eq__(self, other):
        return False

n = NotEqual()
print(n == n)  # -> False
wjandrea
  • 23,210
  • 7
  • 49
  • 68