what is the difference between “ is none ” and “ ==none ”

Answer 1

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.

is None is a bit (~50%) faster than == None 🙂 – Nas Banov

Answer 2

is always returns True if it compares the same object instance

Whereas == is ultimately determined by the __eq__() method

i.e.

1
2
3
4
5
6
7
8
9
10
class :
def __eq__(self,other):
return True
foo=Foo()

print(foo==None)


print(foo is None)
# False

Answer 3

In this case, they are the same. None is a singleton object (there only ever exists one None).

is checks to see if the object is the same object, while == just checks if they are equivalent.

For example:

1
2
3
4
p = [1]
q = [1]
p is q # False because they are not the same actual object
p == q # True because they are equivalent

But since there is only one None, they will always be the same, and is will return True.

1
2
3
p = None
q = None
p is q # True because they are both pointing to the same "None"

Answer 4

(ob1 is ob2) equal to (id(ob1) == id(ob2))

… but (ob is ob2) is a LOT faster. Timeit says “(a is b)” is 0.0365 usec per loop and “(id(a)==id(b))” is 0.153 usec per loop. 4.2x faster! – AKX

{} is {} is false and id({}) == id({}) can be (and is in CPython) true.

Reference

  1. What is the difference between “ is None ” and “ ==None ”
  2. Is there any difference between “foo is None” and “foo == None”?