Commit 57d191fe authored by Georg Brandl's avatar Georg Brandl

Patch #1602128: clarify that richcmp methods can return NotImplemented

and should return True or False otherwise.
parent 6fc1697c
...@@ -1282,10 +1282,14 @@ follows: ...@@ -1282,10 +1282,14 @@ follows:
\code{\var{x}.__ne__(\var{y})}, \code{\var{x}.__ne__(\var{y})},
\code{\var{x}>\var{y}} calls \code{\var{x}.__gt__(\var{y})}, and \code{\var{x}>\var{y}} calls \code{\var{x}.__gt__(\var{y})}, and
\code{\var{x}>=\var{y}} calls \code{\var{x}.__ge__(\var{y})}. \code{\var{x}>=\var{y}} calls \code{\var{x}.__ge__(\var{y})}.
These methods can return any value, but if the comparison operator is
used in a Boolean context, the return value should be interpretable as A rich comparison method may return the singleton \code{NotImplemented} if it
a Boolean value, else a \exception{TypeError} will be raised. does not implement the operation for a given pair of arguments.
By convention, \code{False} is used for false and \code{True} for true. By convention, \code{False} and \code{True} are returned for a successful
comparison. However, these methods can return any value, so if the
comparison operator is used in a Boolean context (e.g., in the condition
of an \code{if} statement), Python will call \function{bool()} on the
value to determine if the result is true or false.
There are no implied relationships among the comparison operators. There are no implied relationships among the comparison operators.
The truth of \code{\var{x}==\var{y}} does not imply that \code{\var{x}!=\var{y}} The truth of \code{\var{x}==\var{y}} does not imply that \code{\var{x}!=\var{y}}
...@@ -1299,9 +1303,7 @@ the right argument does); rather, \method{__lt__()} and ...@@ -1299,9 +1303,7 @@ the right argument does); rather, \method{__lt__()} and
\method{__ge__()} are each other's reflection, and \method{__eq__()} \method{__ge__()} are each other's reflection, and \method{__eq__()}
and \method{__ne__()} are their own reflection. and \method{__ne__()} are their own reflection.
Arguments to rich comparison methods are never coerced. A rich Arguments to rich comparison methods are never coerced.
comparison method may return \code{NotImplemented} if it does not
implement the operation for a given pair of arguments.
\end{methoddesc} \end{methoddesc}
\begin{methoddesc}[object]{__cmp__}{self, other} \begin{methoddesc}[object]{__cmp__}{self, other}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment