mirror of
https://github.com/python/cpython.git
synced 2025-11-03 19:34:08 +00:00
Bring the notes on the relationship between __cmp__(), __eq__(), and
__hash__() up to date (re: use of objects which define these methods as dictionary keys). This closes SF bug #427698.
This commit is contained in:
parent
b9879e1020
commit
597bc1d46f
1 changed files with 15 additions and 10 deletions
|
|
@ -1010,10 +1010,14 @@ implement the operation for a given pair of arguments.
|
||||||
|
|
||||||
\begin{methoddesc}[object]{__cmp__}{self, other}
|
\begin{methoddesc}[object]{__cmp__}{self, other}
|
||||||
Called by comparison operations if rich comparison (see above) is not
|
Called by comparison operations if rich comparison (see above) is not
|
||||||
defined. Should return a negative integer if
|
defined. Should return a negative integer if \code{self < other},
|
||||||
\code{self < other}, zero if \code{self == other}, a positive integer if
|
zero if \code{self == other}, a positive integer if \code{self >
|
||||||
\code{self > other}. If no \method{__cmp__()} operation is defined, class
|
other}. If no \method{__cmp__()}, \method{__eq__()} or
|
||||||
instances are compared by object identity (``address'').
|
\method{__ne__()} operation is defined, class instances are compared
|
||||||
|
by object identity (``address''). See also the description of
|
||||||
|
\method{__hash__()} for some important notes on creating objects which
|
||||||
|
support custom comparison operations and are usable as dictionary
|
||||||
|
keys.
|
||||||
(Note: the restriction that exceptions are not propagated by
|
(Note: the restriction that exceptions are not propagated by
|
||||||
\method{__cmp__()} has been removed in Python 1.5.)
|
\method{__cmp__()} has been removed in Python 1.5.)
|
||||||
\bifuncindex{cmp}
|
\bifuncindex{cmp}
|
||||||
|
|
@ -1035,12 +1039,13 @@ mix together (e.g., using exclusive or) the hash values for the
|
||||||
components of the object that also play a part in comparison of
|
components of the object that also play a part in comparison of
|
||||||
objects. If a class does not define a \method{__cmp__()} method it should
|
objects. If a class does not define a \method{__cmp__()} method it should
|
||||||
not define a \method{__hash__()} operation either; if it defines
|
not define a \method{__hash__()} operation either; if it defines
|
||||||
\method{__cmp__()} but not \method{__hash__()} its instances will not be
|
\method{__cmp__()} or \method{__eq__()} but not \method{__hash__()},
|
||||||
usable as dictionary keys. If a class defines mutable objects and
|
its instances will not be usable as dictionary keys. If a class
|
||||||
implements a \method{__cmp__()} method it should not implement
|
defines mutable objects and implements a \method{__cmp__()} or
|
||||||
\method{__hash__()}, since the dictionary implementation requires that
|
\method{__eq__()} method, it should not implement \method{__hash__()},
|
||||||
a key's hash value is immutable (if the object's hash value changes, it
|
since the dictionary implementation requires that a key's hash value
|
||||||
will be in the wrong hash bucket).
|
is immutable (if the object's hash value changes, it will be in the
|
||||||
|
wrong hash bucket).
|
||||||
\withsubitem{(object method)}{\ttindex{__cmp__()}}
|
\withsubitem{(object method)}{\ttindex{__cmp__()}}
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue