Sub topic 7
Identity versus equality: is and ==
This chapter assumes you know how to assign variables, build lists, and run short Python snippets in a script or the REPL. It covers the difference between two objects being equal and being the same object.
Two different questions
Python gives you two operators that both look like "are these the same?" but ask different questions.
==asks whether two objects have equal values. The type decides what "equal" means.isasks whether two names refer to the exact same object in memory.
is never calls any comparison logic. It compares object identity, which you can inspect with id().
a = [1, 2]
b = [1, 2]
c = a
print(a == b)
print(a is b)
print(c is a)
value = None
print(value is None)The output is True, False, True, True. The lists a and b hold equal contents but were built separately, so they are two objects. The name c is another label for the object a already points to, so c is a is true.
How == works
For built-in types, == compares contents. For your own classes, it calls the __eq__ method. If you do not define one, Python falls back to identity, so two separate instances are unequal even when their attributes match.
Because == can be customized, it can return surprising results. is cannot be customized, so its answer is always about identity.
When to use is
Use is for singletons, objects of which only one exists. The standard case is None.
result = None
if result is None:
print("no result yet")Prefer is None over == None. A class can define __eq__ so that obj == None returns True, but nothing can change what obj is None returns. The same advice applies to is not None.
True and False are also singletons, but you rarely need to test them with is. Write if flag: and let truthiness do the work.
The small-integer and string trap
Do not use is to compare numbers or strings. CPython, the standard Python implementation, reuses some objects as an optimization. Small integers (roughly -5 to 256) are cached, and some strings are interned. That makes is appear to work on them.
x = 1000
y = 1000
print(x == y)Here x == y is True. Whether x is y is True depends on the interpreter, the Python version, and whether both values came from the same compiled code block. Recent Python versions also emit a SyntaxWarning when you write is with a literal, such as x is 1000.
Choosing the right test
| Question | Operator |
|---|---|
| Do these hold equal values? | `==` |
| Is this the same object? | `is` |
| Is this value `None`? | `is None` |
| Is this item inside a container? | `in` |
Common errors
- Writing
if name is "admin":instead of==. It may work in testing and fail later. - Using
== Noneand getting a wrong answer from a class with a custom__eq__. - Assuming that
b = acopies a list. It only adds a second name, so changes through one name show through the other.
Exercise
Given p = [1, 2], q = p, and r = [1, 2], what do p is q, p is r, and p == r print?
Show answer
True, False, True. The name q refers to the same list as p, so the identity test succeeds. The list r is a separate object, so p is r is false, but its contents are equal to p, so p == r is true.
Exercise
Why is if x is None: preferred over if x == None:?
Show answer
None is a singleton, so an identity test is exact and fast. == calls __eq__, which a custom class can override to return True for None, so the result can be misleading.