Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
99107ad5
Commit
99107ad5
authored
Jan 18, 2001
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document rich comparisons.
parent
8c3547fa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
5 deletions
+42
-5
Doc/ref/ref3.tex
Doc/ref/ref3.tex
+42
-5
No files found.
Doc/ref/ref3.tex
View file @
99107ad5
...
...
@@ -133,9 +133,10 @@ Its truth value is false.
\item
[NotImplemented]
This type has a single value. There is a single object with this value.
This object is accessed through the built-in name
\code
{
NotImplemented
}
.
Binary number methods may return this value if they do not implement the
operation for the types of operands provided. The interpreter will then
try the reverse operation. Its truth value is true.
Numeric methods and rich comparison methods may return this value if
they do not implement the operation for the operands provided. (The
interpreter will then try the reflected operation, or some other
fallback, depending on the operator.) Its truth value is true.
\ttindex
{
NotImplemented
}
\obindex
{
NotImplemented@
{
\texttt
{
NotImplemented
}}}
...
...
@@ -943,8 +944,44 @@ expression: a more convenient or concise representation may be used
instead. The return value must be a string object.
\end{methoddesc}
\begin{methoddesc}
[object]
{__
lt
__}{
self, other
}
\methodline
[object]
{__
le
__}{
self, other
}
\methodline
[object]
{__
eq
__}{
self, other
}
\methodline
[object]
{__
ne
__}{
self, other
}
\methodline
[object]
{__
gt
__}{
self, other
}
\methodline
[object]
{__
ge
__}{
self, other
}
\versionadded
{
2.1
}
These are the so-called ``rich comparison'' methods, and are called
for comparison operators in preference to
\method
{__
cmp
__
()
}
below.
The correspondence between operator symbols and method names is as
follows:
\code
{
\var
{
x
}
<
\var
{
y
}}
calls
\code
{
\var
{
x
}
.
__
lt
__
(
\var
{
y
}
)
}
,
\code
{
\var
{
x
}
<=
\var
{
y
}}
calls
\code
{
\var
{
x
}
.
__
le
__
(
\var
{
y
}
)
}
,
\code
{
\var
{
x
}
==
\var
{
y
}}
calls
\code
{
\var
{
x
}
.
__
eq
__
(
\var
{
y
}
)
}
,
\code
{
\var
{
x
}
!=
\var
{
y
}}
and
\code
{
\var
{
x
}
<>
\var
{
y
}}
call
\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
}
.
__
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 Boolean value, else a
\exception
{
TypeError
}
will be raised.
By convention,
\code
{
0
}
is used for false and
\code
{
1
}
for true.
There are no reflected (swapped-argument) versions of these methods
(to be used when the left argument does not support the operation but
the right argument does); rather,
\method
{__
lt
__
()
}
and
\method
{__
gt
__
()
}
are each other's reflection,
\method
{__
le
__
()
}
and
\method
{__
ge
__
()
}
are each other's reflection, and
\method
{__
eq
__
()
}
and
\method
{__
ne
__
()
}
are their own reflection.
Arguments to rich comparison methods are never coerced. A rich
comparison method may return
\code
{
NotImplemented
}
if it does not
implement the operation for a given pair of arguments.
\end{methoddesc}
\begin{methoddesc}
[object]
{__
cmp
__}{
self, other
}
Called by all comparison operations. Should return a negative integer if
Called by comparison operations if rich comparison (see above) is not
defined. Should return a negative integer if
\code
{
self < other
}
, zero if
\code
{
self == other
}
, a positive integer if
\code
{
self > other
}
. If no
\method
{__
cmp
__
()
}
operation is defined, class
instances are compared by object identity (``address'').
...
...
@@ -1288,7 +1325,7 @@ called to implement the binary arithmetic operations (\code{+},
\code
{
-
}
,
\code
{
*
}
,
\code
{
/
}
,
\code
{
\%
}
,
\function
{
divmod()
}
\bifuncindex
{
divmod
}
,
\function
{
pow()
}
\bifuncindex
{
pow
}
,
\code
{
**
}
,
\code
{
<<
}
,
\code
{
>>
}
,
\code
{
\&
}
,
\code
{
\^
}
,
\code
{
|
}
) with re
versed
operands. These
\code
{
\&
}
,
\code
{
\^
}
,
\code
{
|
}
) with re
flected (swapped)
operands. These
functions are only called if the left operand does not support the
corresponding operation. For instance, to evaluate the expression
\var
{
x
}
\code
{
-
}
\var
{
y
}
, where
\var
{
y
}
is an instance of a class that
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment