diff options
author | Fred Drake <fdrake@acm.org> | 2000-07-11 19:43:47 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2000-07-11 19:43:47 (GMT) |
commit | 7399b9e6e49b802809b28bec11b718093dbf138c (patch) | |
tree | dc7e8ba4ff87de365b8e6c5903a2e885de902f19 /Doc | |
parent | 85f363990cbd6df21015eebdc171c533176e3407 (diff) | |
download | cpython-7399b9e6e49b802809b28bec11b718093dbf138c.zip cpython-7399b9e6e49b802809b28bec11b718093dbf138c.tar.gz cpython-7399b9e6e49b802809b28bec11b718093dbf138c.tar.bz2 |
Moshe Zadka <mzadka@geocities.com>:
Update the "in" / "not in" description to accomodate the current use
of the __contains__() discipline. This patch also incorporates
suggestions from Marc-Andre Lemburg <mal@lemburg.com>, minor markup
revisions from Fred Drake, and some rewording of the first affected
paragraph (also from Fred).
Closes SourceForge patch #100831.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/ref/ref5.tex | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/Doc/ref/ref5.tex b/Doc/ref/ref5.tex index 1ef032b..406d259 100644 --- a/Doc/ref/ref5.tex +++ b/Doc/ref/ref5.tex @@ -740,14 +740,35 @@ execution of a program. \end{itemize} -The operators \keyword{in} and \keyword{not in} test for sequence -membership: if \var{y} is a sequence, \code{\var{x} in \var{y}} is -true if and only if there exists an index \var{i} such that -\code{\var{x} = \var{y}[\var{i}]}. -\code{\var{x} not in \var{y}} yields the inverse truth value. The -exception \exception{TypeError} is raised when \var{y} is not a sequence, -or when \var{y} is a string and \var{x} is not a string of length -one.\footnote{The latter restriction is sometimes a nuisance.} +The operators \keyword{in} and \keyword{not in} test for set +membership: every type can define membership in whatever way is +appropriate. Traditionally, this interface has been tightly bound +the sequence interface, which is related in that presence in a sequence +can be usefully interpreted as membership in a set. + +For the list, tuple types, \code{\var{x} in \var{y}} is true if and only +if there exists such an index \var{i} such that +\code{var{x} == \var{y}[\var{i}]} is true. + +For the Unicode and string types, \code{\var{x} in \var{y}} is true if and only +if there exists such an index \var{i} such that +\code{var{x} == \var{y}[\var{i}]} is true. If \code{\var{x}} is not +a string of length \code{1} or a unicode object of length \code{1}, +a \exception{TypeError} exception is raised. + +For user-defined classes which define the \method{__contains__()} method, +\code{\var{x} in \var{y}} is true if and only if +\code{\var{y}.__contains__(\var{x})} is true. + +For user-defined classes which do not define \method{__contains__()} and +do define \var{__getitem__}, \code{\var{x} in \var{y}} is true if and only +if there is a non-negative integer index \var{i} such that +\code{\var{x} == \var{y}[\var{i}]}, and all lower integer indices +do not raise \exception{IndexError} exception. (If any other exception +is raised, it is as if \keyword{in} raised that exception). + +The operator \keyword{not in} is defined to have the inverse true value +of \keyword{in}. \opindex{in} \opindex{not in} \indexii{membership}{test} |