7

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
  • 48,794
  • 16
  • 117
  • 146
dzieciou
  • 4,049
  • 8
  • 41
  • 85

2 Answers2

8

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
  • 4,189
  • 3
  • 19
  • 40
7

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
  • 28,235
  • 9
  • 60
  • 81