Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~2 min readPython
#python#identity#equality#is operator
Illustration showing two objects with same value but different identity, and the is vs == comparison in Python.

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:

python
a = [1, 2, 3] b = [1, 2, 3] print(a == b) # True print(a is b) # False

The 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.

python
x = [1, 2] y = x print(id(x) == id(y)) # True, same object

The 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:

python
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:

python
_MISSING = object() def get_value(data, key): value = data.get(key, _MISSING) if value is _MISSING: raise KeyError(key) return value

Because _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.

python
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.

python
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 context

For 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 tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.