Skip to content

Commit f8775e4

Browse files
authored
bpo-43325: Add FAQ entry for identity tests (GH-25168)
1 parent 35715d1 commit f8775e4

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
@@ -1701,6 +1701,93 @@ to the object:
17011701
13891296
17021702

17031703

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

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)