What is None? How to Test None with the “==” or “is” operator?

Mayuresh Kedari
5 min readDec 31, 2021

--

Is it essential to use “== None” instead of “is None” or vice versa?

Article’s title gif
None Comparison: GIF by Mayuresh Kedari on Canva

In this article, you will learn the following points:

  1. What is None?
  2. What is the value of None?
  3. What is the type of None?
  4. How to compare or test None?

What is None & its type?

  1. None is similar to null in other programming languages such as C, C++, and Java, but in Python it’s different.
  2. None is commonly used to represent missing values or unavailability of items/elements.
  3. They are often used as default arguments in functions and methods.
  4. If a function or method has a missing return statement, then it will return None implicitly. It is also advised to be more explicit rather than implicit (The Zen of Python).
  5. None is a singleton object of the class ‘NoneType’.
  6. None is an immutable object with a false value.
Type of None

What is the value of None?

  • None, unlike null in other languages, doesn’t define 0 (zero), an empty string, or any other value.
Value of None
  • If any object is associated with the value ‘None’ and tested with an if statement, None will be evaluated as False implicitly.
None evaluation
  • The value “None” cannot be changed.
Assignment of the new value to None
  • Similar to the assignment of None to a value, the ID (memory location) never changes, even when None is assigned to any object.
  • Every object with the None value has the same ID as None. Both will point to the same object.
ID of None and its associated object

The difference between “==” and “is” operator:

  • “==” and “!=” are Equality operators that compare the values of two objects.
  • “is” and “is not” are Identity operators that compare the id (memory location) of them. In simple terms, both objects are pointing to the same or not with respect to the memory location.

How to compare or test None?

  • Whether I should use equality operators (== / !=) or identity operators (is/is not) to compare the None with any object.
  • Answer: The “identity operator”.
  • Whenever you have to compare any object or variable with None, never use an equality operator(== / !=). This may lead to a bug or an unexpected result, but hopefully, in some cases, Python provides the correct result.

Fun Fact: Identity operators provide faster performance than equality operators.

Note: Even though identity operators are faster than equality operators, it is still recommended that they not be used just for performance, because the accuracy of the result is more important than performance.

  • Python standards — PEP 8 says:

1. Comparisons to singletons like None should always be done with is or is not, never the equality operators.

2. Also, beware of writing if x when you really mean if x is not None — e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context

  • I personally found that if you are comparing the objects with None, especially if the objects are of a user-defined class (check the below example):
  • This will result into —
  1. Correct results: if you use the Identity operator (is / is not)
  2. Unexpected results: if you use the Equality operator (== / !=)

Highly advised: Look over the examples below, then implement and test your own user-defined classes (don’t forget to implement the __eq__ method), then compare your results to None using both equality and identity operators.

Examples:

For a better understanding, debug the following programs and read the comments provided in them.

  • Simplest example:
  • Detailed example: With a built-in class
With a built-in class: list
  • Detailed example: With a user-defined class
With a user-defined class: NoneComparison

Except for itself, comparing None to any other value with “==” or “is” always returns False.

Conclusion:

  • None is a vital, immutable, and singleton object.
  • Furthermore, comparing any object with None using identity operators is essential for getting the correct results.

Thank you so much for taking the time to read this!

Thanks to:

I’d like to thank Coen de Groot for pointing out my naming convention case mistakes (as of now, I’ve corrected with snake case), which should have followed PEP 8, and for correcting me with the Incorrect results to Unexpected results.

Why you should use ‘is’ over ‘==’?

There is only one None. ‘==’ compares values. So ‘a == None’ translates into ‘Does object a have the same value as object None’. However, there is only one ‘None’ object. So ‘==’ is a bit of a silly question to ask. If ‘a == None’ then always ‘a is None’.

— short and crisp answer by Coen de Groot

Shortly about Coen: He is a UK-based Independent Python developer and trainer with over 23 years of experience who, like me, loves to maintain the code as per Python standards and more.

--

--

Mayuresh Kedari
Mayuresh Kedari

Written by Mayuresh Kedari

Data Engineer & Software Developer | Passionate about learning new, innovative techniques and technologies. Experienced in robust DE and S/W development.