@@ -133,9 +133,10 @@ \section{The standard type hierarchy\label{types}}
133133\item [NotImplemented]
134134This type has a single value. There is a single object with this value.
135135This object is accessed through the built-in name \code {NotImplemented}.
136- Binary number methods may return this value if they do not implement the
137- operation for the types of operands provided. The interpreter will then
138- try the reverse operation. Its truth value is true.
136+ Numeric methods and rich comparison methods may return this value if
137+ they do not implement the operation for the operands provided. (The
138+ interpreter will then try the reflected operation, or some other
139+ fallback, depending on the operator.) Its truth value is true.
139140\ttindex {NotImplemented}
140141\obindex {NotImplemented@{\texttt {NotImplemented }}}
141142
@@ -943,8 +944,44 @@ \subsection{Basic customization\label{customization}}
943944instead. The return value must be a string object.
944945\end {methoddesc }
945946
947+ \begin {methoddesc }[object]{__lt__}{self, other}
948+ \methodline [object]{__le__}{self, other}
949+ \methodline [object]{__eq__}{self, other}
950+ \methodline [object]{__ne__}{self, other}
951+ \methodline [object]{__gt__}{self, other}
952+ \methodline [object]{__ge__}{self, other}
953+ \versionadded {2.1}
954+ These are the so-called `` rich comparison'' methods, and are called
955+ for comparison operators in preference to \method {__cmp__()} below.
956+ The correspondence between operator symbols and method names is as
957+ follows:
958+ \code {\var {x}<\var {y}} calls \code {\var {x}.__lt__(\var {y})},
959+ \code {\var {x}<=\var {y}} calls \code {\var {x}.__le__(\var {y})},
960+ \code {\var {x}==\var {y}} calls \code {\var {x}.__eq__(\var {y})},
961+ \code {\var {x}!=\var {y}} and \code {\var {x}<>\var {y}} call
962+ \code {\var {x}.__ne__(\var {y})},
963+ \code {\var {x}>\var {y}} calls \code {\var {x}.__gt__(\var {y})}, and
964+ \code {\var {x}>=\var {y}} calls \code {\var {x}.__ge__(\var {y})}.
965+ These methods can return any value, but if the comparison operator is
966+ used in a Boolean context, the return value should be interpretable as
967+ a Boolean value, else a \exception {TypeError} will be raised.
968+ By convention, \code {0} is used for false and \code {1} for true.
969+
970+ There are no reflected (swapped-argument) versions of these methods
971+ (to be used when the left argument does not support the operation but
972+ the right argument does); rather, \method {__lt__()} and
973+ \method {__gt__()} are each other's reflection, \method {__le__()} and
974+ \method {__ge__()} are each other's reflection, and \method {__eq__()}
975+ and \method {__ne__()} are their own reflection.
976+
977+ Arguments to rich comparison methods are never coerced. A rich
978+ comparison method may return \code {NotImplemented} if it does not
979+ implement the operation for a given pair of arguments.
980+ \end {methoddesc }
981+
946982\begin {methoddesc }[object]{__cmp__}{self, other}
947- Called by all comparison operations. Should return a negative integer if
983+ Called by comparison operations if rich comparison (see above) is not
984+ defined. Should return a negative integer if
948985\code {self < other}, zero if \code {self == other}, a positive integer if
949986\code {self > other}. If no \method {__cmp__()} operation is defined, class
950987instances are compared by object identity (`` address'' ).
@@ -1288,7 +1325,7 @@ \subsection{Emulating numeric types\label{numeric-types}}
12881325\code {-}, \code {*}, \code {/}, \code {\% },
12891326\function {divmod()}\bifuncindex {divmod},
12901327\function {pow()}\bifuncindex {pow}, \code {**}, \code {<<}, \code {>>},
1291- \code {\& }, \code {\^ }, \code {|}) with reversed operands. These
1328+ \code {\& }, \code {\^ }, \code {|}) with reflected (swapped) operands. These
12921329functions are only called if the left operand does not support the
12931330corresponding operation. For instance, to evaluate the expression
12941331\var {x}\code {-}\var {y}, where \var {y} is an instance of a class that
0 commit comments