Python None comparison should I use is or ?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you want to test whether a value is None in Python, the idiomatic and correct answer is to use is or is not, not ==. The reason is that None is a singleton object, and identity checks express exactly what you mean.
Use is None
The standard pattern is:
And the negative form is:
This is the form recommended by Python style guides and widely expected by other Python developers.
Why is Is Better Than ==
is checks object identity. It asks whether two references point to the exact same object.
== checks equality. It asks whether two objects should be considered equal according to their value semantics.
For None, you do not want value equality. You want to know whether the object is literally the special singleton None.
Both happen to be true here, but they mean different things.
The Real Problem with == None
The danger is that == can call custom equality logic. User-defined classes may implement __eq__ in ways that produce surprising results.
That example is intentionally strange, but it shows why is None is more reliable. It cannot be faked by overloaded equality behavior.
This Also Helps Readability
if x is None: communicates intent clearly. It tells the reader you are checking for the special sentinel object, not comparing general values.
That is especially important in code where None means something specific, such as:
- optional function arguments
- missing database fields
- "not yet computed" cache entries
- sentinel return values
Clear intent matters more than tiny syntax differences.
Do Not Use Truthiness Instead
Another common mistake is writing:
That does not mean the same thing. It treats 0, False, [], "", and None the same way.
If you specifically mean None, write that explicitly:
This avoids bugs where valid but empty values get mistaken for missing values.
Typical Function Example
Here is a normal use case with an optional argument:
This makes the sentinel meaning of None explicit and easy to follow.
What About Singletons Like True and False
The guidance for None is especially strong because it is a singleton sentinel used for absence of value. For booleans, you often want truthiness or explicit boolean logic instead of identity checks.
So this article's rule is not "always use is instead of ==." It is specifically "use is when checking for None."
Common Pitfalls
The biggest mistake is using == None, which can invoke custom equality behavior and obscure your real intent.
Another mistake is using if not value when the code needs to distinguish between None and other falsy values such as 0 or an empty string.
Developers also sometimes compare return values to None indirectly through chained conditions, making the code harder to read than a direct is None check.
Finally, avoid is for ordinary value comparisons like numbers or strings. is is for identity, not general equality.
Summary
- Use
is Noneandis not NoneforNonechecks in Python. - '
ischecks identity, which matches the meaning ofNone.' - '
==can be overridden by custom classes and is less reliable here.' - Do not replace a
Nonecheck with a generic truthiness check unless that is truly what you mean. - The rule is specific to sentinel-style identity checks, not to all comparisons in Python.
Related reading
- Python not working in the command line of git bash
- Python not working in the command line of git bash
- Python NTL - Identifying text interest / topic
- Python Numerical Integration for Volume of Region
- python numpy ValueError operands could not be broadcast together with shapes
- Python OCR ignore signatures in documents
- Python operation vs is not
- Python os.path.join on a list
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.