summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-10-22 15:00:06 (GMT)
committerGeorg Brandl <georg@python.org>2009-10-22 15:00:06 (GMT)
commit2eee1d4df1af9bf824a9fe61bba031bf78a9cabb (patch)
tree4ae88c4c95efcc626f77f82b9277aa32a2331742 /Doc/reference
parent6c14e587f59e72d69b99fdf766bbbcbf4af920fa (diff)
downloadcpython-2eee1d4df1af9bf824a9fe61bba031bf78a9cabb.zip
cpython-2eee1d4df1af9bf824a9fe61bba031bf78a9cabb.tar.gz
cpython-2eee1d4df1af9bf824a9fe61bba031bf78a9cabb.tar.bz2
#6324: membership test tries iteration via __iter__.
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/datamodel.rst12
-rw-r--r--Doc/reference/expressions.rst9
2 files changed, 16 insertions, 5 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 6a7acf9..3f9b181 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1888,12 +1888,16 @@ implemented as an iteration through a sequence. However, container objects can
supply the following special method with a more efficient implementation, which
also does not require the object be a sequence.
-
.. method:: object.__contains__(self, item)
- Called to implement membership test operators. Should return true if *item* is
- in *self*, false otherwise. For mapping objects, this should consider the keys
- of the mapping rather than the values or the key-item pairs.
+ Called to implement membership test operators. Should return true if *item*
+ is in *self*, false otherwise. For mapping objects, this should consider the
+ keys of the mapping rather than the values or the key-item pairs.
+
+ For objects that don't define :meth:`__contains__`, the membership test first
+ tries iteration via :meth:`__iter__`, then the old sequence iteration
+ protocol via :meth:`__getitem__`, see :ref:`this section in the language
+ reference <membership-test-details>`.
.. _sequence-methods:
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst
index 6481894..e060670 100644
--- a/Doc/reference/expressions.rst
+++ b/Doc/reference/expressions.rst
@@ -1068,6 +1068,8 @@ Comparison of objects of the same type depends on the type:
another one is made arbitrarily but consistently within one execution of a
program.
+.. _membership-test-details:
+
The operators :keyword:`in` and :keyword:`not in` test for collection
membership. ``x in s`` evaluates to true if *x* is a member of the collection
*s*, and false otherwise. ``x not in s`` returns the negation of ``x in s``.
@@ -1092,7 +1094,12 @@ string, so ``"" in "abc"`` will return ``True``.
For user-defined classes which define the :meth:`__contains__` method, ``x in
y`` is true if and only if ``y.__contains__(x)`` is true.
-For user-defined classes which do not define :meth:`__contains__` and do define
+For user-defined classes which do not define :meth:`__contains__` but do define
+:meth:`__iter__`, ``x in y`` is true if some value ``z`` with ``x == z`` is
+produced while iterating over ``y``. If an exception is raised during the
+iteration, it is as if :keyword:`in` raised that exception.
+
+Lastly, the old-style iteration protocol is tried: if a class defines
:meth:`__getitem__`, ``x in y`` is true if and only if there is a non-negative
integer index *i* such that ``x == y[i]``, and all lower integer indices do not
raise :exc:`IndexError` exception. (If any other exception is raised, it is as