What is the difference between is None and None
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the right way to test for None is usually is None, not == None. The difference is that is checks identity, while == checks equality and may call custom comparison logic.
None Is a Singleton
Python has exactly one None object. That is why identity comparison is the natural test.
This asks, "is this object the actual None singleton". That is precise and predictable.
== None Uses Equality Logic
When you write value == None, Python is not required to do a simple identity check. It may call value.__eq__(None), and user-defined classes can implement that however they want.
Here is a deliberately strange example:
Output:
That is the whole reason is None is preferred. Equality can lie for domain-specific reasons. Identity cannot.
Why is None Is the Idiomatic Choice
is None is recommended because:
- it matches Python's singleton model
- it avoids custom
__eq__surprises - it communicates intent clearly
In real code, this often appears in function arguments:
That is clearer than using a generic truthiness check such as if not user_id, which would also treat 0, False, and empty strings as missing.
is not None Is the Matching Form
When you want to assert presence, use is not None.
This matters when 0, False, or "" are legitimate values. A truthiness test would collapse those cases incorrectly.
Compare these two examples:
The first example says nothing about missingness. The second one does.
When == None Shows Up Anyway
You may still see == None in old code or in generic equality discussions. It is not illegal Python, but it is usually the wrong comparison for ordinary application logic.
Linters and style guides generally push developers toward identity checks for None. The reason is not micro-optimization. The reason is correctness and clarity.
A Common Real Bug
Suppose a function returns either a count or None when data is unavailable:
This prints count is 0, which is correct.
If you wrote if not count, you would incorrectly classify 0 as missing. That is why None checks should be explicit.
Common Pitfalls
- Using
== Noneand accidentally invoking custom equality behavior. - Using
if not valuewhen you specifically mean "missing" rather than "falsy". - Forgetting the matching readable form
is not None. - Comparing to
Nonein containers or frameworks that overload equality in surprising ways. - Assuming the issue is performance. The bigger concern is semantic correctness.
Summary
- '
is Nonechecks identity with the singletonNoneobject.' - '
== Nonechecks equality and can trigger custom__eq__behavior.' - For normal Python code, prefer
is Noneandis not None. - Use explicit
Nonechecks when0,False, or empty strings are valid values. - The main advantage of
is Noneis correctness, not speed.

