How to compare values to None in Python

avatar

Borislav Hadzhiev

Wed Jun 15 20222 min read

banner

Photo by Katie McBroom

Compare values to None in Python #

Use the is operator to compare to None in Python, e.g. if my_var is None:. The is operator returns True if the two values point to the same object (it checks for identity), whereas the double equals == operator checks if the two values are equal.

main.py
my_var = None if my_var is None: # 👇️ this runs print('variable stores None') else: print('variable does NOT store None')

You should use the is operator when you need to check for an object's identity.

The pep 8 style guide mentions that comparison to singletons like None should always be done with is or is not, and never the equality operators.

Use the equality operators (equals == and not equals != ) when you need to check if a value is equal to another value, e.g. 'hi' == 'hi'.

When using is, we check if the values are the same object (same location in memory), not if they are equal.

Here is an example that better illustrates checking for identity (is) vs checking for equality (==).

main.py
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print(my_first_list is my_second_list) # 👉️ True print(my_first_list == my_second_list) # 👉️ True

We declared 2 variables that store the same list.

We set the second variable to the first, so both variables point to the same list object in memory.

Now, let's create a shallow copy of the list and assign it to the second variable.

main.py
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list.copy() # 👈️ copy created print(my_first_list is my_second_list) # 👉️ False print(my_first_list == my_second_list) # 👉️ True
Notice that the identity check failed. Even though the two lists store the same values, in the same order, they point to different locations in memory (they are not the same object).

When we use the double equals operator, Python calls the __eq__() method on the object.

That is x==y calls x.__eq__(y). In theory this method could be implemented in an unpredictable way, so comparing to None with the is operator is more direct.

You can use the id() function to get the identity of an object.

main.py
my_first_list = ['a', 'b', 'c'] print(id(my_first_list)) # 👉️ 139944523741504 my_second_list = my_first_list.copy() print(id(my_second_list)) # 👉️ 139944522293184 print(id(my_first_list) == id(my_second_list)) # 👉️ False

The function returns an integer, which is guaranteed to be unique and constant for the object's lifetime.

The id() function returns the address of the object in memory in CPython.

If the two variables refer to the same object, the id() function will produce the same result.

main.py
my_first_list = ['a', 'b', 'c'] print(id(my_first_list)) # 👉️ 140311440685376 my_second_list = my_first_list print(id(my_second_list)) # 👉️ 140311440685376 print(id(my_first_list) == id(my_second_list)) # 👉️ True

Passing a None value to the id() function is always going to return the same result because there is only one instance of None in a Python program.

main.py
print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984
Use the search field on my Home Page to filter through my more than 1,000 articles.