Skip to content

Commit fd704a0

Browse files
authored
bpo-16011: clarify that 'in' always returns a boolean value (GH-152) (GH-883)
(cherry picked from commit 0ae7c8b)
1 parent 6a04ef7 commit fd704a0

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

Doc/reference/expressions.rst

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,37 +1249,30 @@ Python does not enforce these consistency rules.
12491249
Membership test operations
12501250
--------------------------
12511251

1252-
The operators :keyword:`in` and :keyword:`not in` test for collection
1253-
membership. ``x in s`` evaluates to true if *x* is a member of the collection
1254-
*s*, and false otherwise. ``x not in s`` returns the negation of ``x in s``.
1255-
The collection membership test has traditionally been bound to sequences; an
1256-
object is a member of a collection if the collection is a sequence and contains
1257-
an element equal to that object. However, it make sense for many other object
1258-
types to support membership tests without being a sequence. In particular,
1259-
dictionaries (for keys) and sets support membership testing.
1260-
1261-
For the list and tuple types, ``x in y`` is true if and only if there exists an
1262-
index *i* such that either ``x is y[i]`` or ``x == y[i]`` is true.
1263-
1264-
For the Unicode and string types, ``x in y`` is true if and only if *x* is a
1265-
substring of *y*. An equivalent test is ``y.find(x) != -1``. Note, *x* and *y*
1266-
need not be the same type; consequently, ``u'ab' in 'abc'`` will return
1267-
``True``. Empty strings are always considered to be a substring of any other
1268-
string, so ``"" in "abc"`` will return ``True``.
1269-
1270-
.. versionchanged:: 2.3
1271-
Previously, *x* was required to be a string of length ``1``.
1252+
The operators :keyword:`in` and :keyword:`not in` test for membership. ``x in
1253+
s`` evaluates to ``True`` if *x* is a member of *s*, and ``False`` otherwise.
1254+
``x not in s`` returns the negation of ``x in s``. All built-in sequences and
1255+
set types support this as well as dictionary, for which :keyword:`in` tests
1256+
whether the dictionary has a given key. For container types such as list, tuple,
1257+
set, frozenset, dict, or collections.deque, the expression ``x in y`` is equivalent
1258+
to ``any(x is e or x == e for e in y)``.
1259+
1260+
For the string and bytes types, ``x in y`` is ``True`` if and only if *x* is a
1261+
substring of *y*. An equivalent test is ``y.find(x) != -1``. Empty strings are
1262+
always considered to be a substring of any other string, so ``"" in "abc"`` will
1263+
return ``True``.
12721264

12731265
For user-defined classes which define the :meth:`__contains__` method, ``x in
1274-
y`` is true if and only if ``y.__contains__(x)`` is true.
1266+
y`` returns ``True`` if ``y.__contains__(x)`` returns a true value, and
1267+
``False`` otherwise.
12751268

12761269
For user-defined classes which do not define :meth:`__contains__` but do define
1277-
:meth:`__iter__`, ``x in y`` is true if some value ``z`` with ``x == z`` is
1270+
:meth:`__iter__`, ``x in y`` is ``True`` if some value ``z`` with ``x == z`` is
12781271
produced while iterating over ``y``. If an exception is raised during the
12791272
iteration, it is as if :keyword:`in` raised that exception.
12801273

12811274
Lastly, the old-style iteration protocol is tried: if a class defines
1282-
:meth:`__getitem__`, ``x in y`` is true if and only if there is a non-negative
1275+
:meth:`__getitem__`, ``x in y`` is ``True`` if and only if there is a non-negative
12831276
integer index *i* such that ``x == y[i]``, and all lower integer indices do not
12841277
raise :exc:`IndexError` exception. (If any other exception is raised, it is as
12851278
if :keyword:`in` raised that exception).

0 commit comments

Comments
 (0)