== vs is

Python
Author

Imad Dabbura

Published

November 29, 2022

Every thing in Python is an object. Variables are nothing but pointers to those objects. Therefore, multiple variables can point to the same object. Therefore:

a = [2]
b = a
a is b          #=> True
id(a) == id(b)  #=> True


a = [2]
b = [2]
a is b          #=> False
id(a) == id(b)  #=> True

It is recommended to use is operator when checking if a variable is: True, False, or None. If we use ==, it will invoke __eq__ method, which if not implemented, compare the ids of both variables and is slower than is operator because it first has to look up __eq__ method and then invoke id() on both variables.