Skip to content

Commit de0b2b1

Browse files
bpo-43325: Add FAQ entry for identity tests (GH-25168) (GH-25178)
1 parent cbfa09b commit de0b2b1

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

Doc/faq/programming.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,93 @@ to the object:
17021702
13891296
17031703

17041704

1705+
When can I rely on identity tests with the *is* operator?
1706+
---------------------------------------------------------
1707+
1708+
The ``is`` operator tests for object identity. The test ``a is b`` is
1709+
equivalent to ``id(a) == id(b)``.
1710+
1711+
The most important property of an identity test is that an object is always
1712+
identical to itself, ``a is a`` always returns ``True``. Identity tests are
1713+
usually faster than equality tests. And unlike equality tests, identity tests
1714+
are guaranteed to return a boolean ``True`` or ``False``.
1715+
1716+
However, identity tests can *only* be substituted for equality tests when
1717+
object identity is assured. Generally, there are three circumstances where
1718+
identity is guaranteed:
1719+
1720+
1) Assignments create new names but do not change object identity. After the
1721+
assignment ``new = old``, it is guaranteed that ``new is old``.
1722+
1723+
2) Putting an object in a container that stores object references does not
1724+
change object identity. After the list assignment ``s[0] = x``, it is
1725+
guaranteed that ``s[0] is x``.
1726+
1727+
3) If an object is a singleton, it means that only one instance of that object
1728+
can exist. After the assignments ``a = None`` and ``b = None``, it is
1729+
guaranteed that ``a is b`` because ``None`` is a singleton.
1730+
1731+
In most other circumstances, identity tests are inadvisable and equality tests
1732+
are preferred. In particular, identity tests should not be used to check
1733+
constants such as :class:`int` and :class:`str` which aren't guaranteed to be
1734+
singletons::
1735+
1736+
>>> a = 1000
1737+
>>> b = 500
1738+
>>> c = b + 500
1739+
>>> a is c
1740+
False
1741+
1742+
>>> a = 'Python'
1743+
>>> b = 'Py'
1744+
>>> c = b + 'thon'
1745+
>>> a is c
1746+
False
1747+
1748+
Likewise, new instances of mutable containers are never identical::
1749+
1750+
>>> a = []
1751+
>>> b = []
1752+
>>> a is b
1753+
False
1754+
1755+
In the standard library code, you will see several common patterns for
1756+
correctly using identity tests:
1757+
1758+
1) As recommended by :pep:`8`, an identity test is the preferred way to check
1759+
for ``None``. This reads like plain English in code and avoids confusion with
1760+
other objects that may have boolean values that evaluate to false.
1761+
1762+
2) Detecting optional arguments can be tricky when ``None`` is a valid input
1763+
value. In those situations, you can create an singleton sentinel object
1764+
guaranteed to be distinct from other objects. For example, here is how
1765+
to implement a method that behaves like :meth:`dict.pop`::
1766+
1767+
_sentinel = object()
1768+
1769+
def pop(self, key, default=_sentinel):
1770+
if key in self:
1771+
value = self[key]
1772+
del self[key]
1773+
return value
1774+
if default is _sentinel:
1775+
raise KeyError(key)
1776+
return default
1777+
1778+
3) Container implementations sometimes need to augment equality tests with
1779+
identity tests. This prevents the code from being confused by objects such as
1780+
``float('NaN')`` that are not equal to themselves.
1781+
1782+
For example, here is the implementation of
1783+
:meth:`collections.abc.Sequence.__contains__`::
1784+
1785+
def __contains__(self, value):
1786+
for v in self:
1787+
if v is value or v == value:
1788+
return True
1789+
return False
1790+
1791+
17051792
Modules
17061793
=======
17071794

Doc/tutorial/datastructures.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,8 @@ operators, not just comparisons.
661661

662662
The comparison operators ``in`` and ``not in`` check whether a value occurs
663663
(does not occur) in a sequence. The operators ``is`` and ``is not`` compare
664-
whether two objects are really the same object; this only matters for mutable
665-
objects like lists. All comparison operators have the same priority, which is
666-
lower than that of all numerical operators.
664+
whether two objects are really the same object. All comparison operators have
665+
the same priority, which is lower than that of all numerical operators.
667666

668667
Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is
669668
less than ``b`` and moreover ``b`` equals ``c``.

0 commit comments

Comments
 (0)