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

Python

Python Problem Overview


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

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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

print(foo==None)
# True

print(foo is None)
# False

Solution 3 - Python

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:

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.

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

Solution 4 - Python

It depends on what you are comparing to None. Some classes have custom comparison methods that treat == None differently from is None.

In particular the output of a == None does not even have to be boolean !! - a frequent cause of bugs.

For a specific example take a numpy array where the == comparison is implemented elementwise:

import numpy as np
a = np.zeros(3) # now a is array([0., 0., 0.])
a == None #compares elementwise, outputs array([False, False, False]), i.e. not boolean!!!
a is None #compares object to object, outputs False

Solution 5 - Python

If you use numpy,

if np.zeros(3)==None: pass

will give you error when numpy does elementwise comparison

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmyusuf3View Question on Stackoverflow
Solution 1 - PythonBen HoffsteinView Answer on Stackoverflow
Solution 2 - PythonunutbuView Answer on Stackoverflow
Solution 3 - PythonDonald MinerView Answer on Stackoverflow
Solution 4 - PythonPGliviView Answer on Stackoverflow
Solution 5 - Pythonjf328View Answer on Stackoverflow