What is the difference between " is None " and " ==None "


Question

I recently came across this syntax, I am unaware of the difference.

I would appreciate it if someone could tell me the difference.

1
267
5/16/2016 4:54:34 PM

Accepted Answer

The answer is explained here.

To quote:

A class is free to implement comparison any way it chooses, and it can choose to make comparison against None mean something (which actually makes sense; if someone told you to implement the None object from scratch, how else would you get it to compare True against itself?).

Practically-speaking, there is not much difference since custom comparison operators are rare. But you should use is None as a general rule.

245
7/15/2010 4:55:39 PM

class Foo:
    def __eq__(self,other):
        return True
foo=Foo()

print(foo==None)
# True

print(foo is None)
# False

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon