Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what situation is an object not equal to itself?

Tags:

python

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?

like image 980
dzieciou Avatar asked Dec 09 '19 17:12

dzieciou


2 Answers

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.

like image 184
lukeg Avatar answered Dec 02 '22 20:12

lukeg


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
like image 26
wjandrea Avatar answered Dec 02 '22 18:12

wjandrea