Python Identity vs Equality: Using is and == Correctly
Learn the difference between Python's is and == operators: identity vs. value comparison, when to use each, and common object-comparison pitfalls.

In Python, is and == answer different questions. The == operator compares values, while the is operator compares object identity. Knowing which comparison you need prevents subtle bugs, especially when working with mutable objects, None, and custom classes.
The Difference Between is and ==
When you create two lists with identical contents, they are equal but not identical:
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True
print(a is b) # FalseThe lists have the same elements, so a == b is True. They are separate objects in memory, so a is b is False. This distinction is the core of identity versus equality in Python.
How Python Determines Identity
Every object has an identity, which you can inspect with the built-in id() function. The returned integer is unique and constant for the object during its lifetime. In CPython, id() returns the object's memory address, but that is an implementation detail; Python only guarantees uniqueness during the object's lifetime.
x = [1, 2]
y = x
print(id(x) == id(y)) # True, same objectThe is operator checks the same identity relationship: a is b is true exactly when id(a) == id(b). It compares references directly and does not call any __eq__ method, so it can be faster than ==.
When to Use Identity Checks
Use is when you care about object identity, not value. The most common case is checking for None:
if x is None:
...None is a singleton, so x is None is idiomatic and clearer than x == None.
Identity checks are also useful for sentinel values. A sentinel should be a unique object that no legitimate value can accidentally equal:
_MISSING = object()
def get_value(data, key):
value = data.get(key, _MISSING)
if value is _MISSING:
raise KeyError(key)
return valueBecause _MISSING is a unique object, no ordinary value stored in the dictionary can accidentally match it.
Equality and Custom Objects
By default, == behaves like is for instances of a user-defined class: it compares identity. To compare by value, override __eq__. If you do, also override __hash__, because objects that compare equal must have the same hash value.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
if not isinstance(other, Point):
return NotImplemented
return self.x == other.x and self.y == other.y
def __hash__(self):
return hash((self.x, self.y))After this, two Point instances with the same coordinates compare equal while remaining distinct objects.
Common Pitfalls: Integer Caching and String Interning
CPython caches small integers from -5 to 256 and can reuse those objects. This can make is return True for numbers that were assigned separately. Similarly, Python may intern some strings, especially string literals. These behaviors are implementation details.
a = 256
b = 256
print(a == b) # True
print(a is b) # True in CPython
c = 257
d = 257
print(c == d) # True
print(c is d) # Not guaranteed; depends on interpreter, version, or contextFor values that fall outside the cache or are constructed dynamically, is may return False even when the values are equal. Do not use is for value comparison of numbers or strings; use == instead.
Performance and Maintainability Considerations
is is slightly faster than == because it does not run comparison methods. In most programs, the difference is negligible. The greater benefit is clarity: x is None expresses exactly what you mean, while x == None can make a reader wonder whether a custom __eq__ is involved.
Code that relies on integer caching or string interning is fragile. It can behave differently across Python implementations, versions, or code contexts. Follow the semantic rule: use is for identity and == for equality.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.