summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-11-03 08:44:43 (GMT)
committerGeorg Brandl <georg@python.org>2007-11-03 08:44:43 (GMT)
commitdb62967a46e3ced2c731a5f4dc7846ac696827f2 (patch)
tree8fd5bfcb719c455ab74ff3407bf410ae8aae1544 /Doc/reference
parenta6c04bed1ed51e87bf9a24bc4b9ab9364821aef5 (diff)
downloadcpython-db62967a46e3ced2c731a5f4dc7846ac696827f2.zip
cpython-db62967a46e3ced2c731a5f4dc7846ac696827f2.tar.gz
cpython-db62967a46e3ced2c731a5f4dc7846ac696827f2.tar.bz2
Re-add two paragraphs that seem to have been lost during the merge from trunk.
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/datamodel.rst17
1 files changed, 13 insertions, 4 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 69ac9dc..1e85f83 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1289,8 +1289,7 @@ Basic customization
instances are compared by object identity ("address"). See also the
description of :meth:`__hash__` for some important notes on creating
:term:`hashable` objects which support custom comparison operations and are
- usable as dictionary keys. (Note: the restriction that exceptions are not
- propagated by :meth:`__cmp__` has been removed since Python 1.5.)
+ usable as dictionary keys.
.. method:: object.__hash__(self)
@@ -1307,8 +1306,18 @@ Basic customization
(e.g., using exclusive or) the hash values for the components of the object that
also play a part in comparison of objects.
- :meth:`__hash__` may also return a long integer object; the 32-bit integer is
- then derived from the hash of that object.
+ If a class does not define a :meth:`__cmp__` or :meth:`__eq__` method it
+ should not define a :meth:`__hash__` operation either; if it defines
+ :meth:`__cmp__` or :meth:`__eq__` but not :meth:`__hash__`, its instances
+ will not be usable as dictionary keys. If a class defines mutable objects
+ and implements a :meth:`__cmp__` or :meth:`__eq__` method, it should not
+ implement :meth:`__hash__`, since the dictionary implementation requires that
+ a key's hash value is immutable (if the object's hash value changes, it will
+ be in the wrong hash bucket).
+
+ User-defined classes have :meth:`__cmp__` and :meth:`__hash__` methods
+ by default; with them, all objects compare unequal and ``x.__hash__()``
+ returns ``id(x)``.
.. method:: object.__bool__(self)