Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db62967

Browse files
committed
Re-add two paragraphs that seem to have been lost during the merge from trunk.
1 parent a6c04be commit db62967

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

Doc/reference/datamodel.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,8 +1289,7 @@ Basic customization
12891289
instances are compared by object identity ("address"). See also the
12901290
description of :meth:`__hash__` for some important notes on creating
12911291
:term:`hashable` objects which support custom comparison operations and are
1292-
usable as dictionary keys. (Note: the restriction that exceptions are not
1293-
propagated by :meth:`__cmp__` has been removed since Python 1.5.)
1292+
usable as dictionary keys.
12941293

12951294

12961295
.. method:: object.__hash__(self)
@@ -1307,8 +1306,18 @@ Basic customization
13071306
(e.g., using exclusive or) the hash values for the components of the object that
13081307
also play a part in comparison of objects.
13091308

1310-
:meth:`__hash__` may also return a long integer object; the 32-bit integer is
1311-
then derived from the hash of that object.
1309+
If a class does not define a :meth:`__cmp__` or :meth:`__eq__` method it
1310+
should not define a :meth:`__hash__` operation either; if it defines
1311+
:meth:`__cmp__` or :meth:`__eq__` but not :meth:`__hash__`, its instances
1312+
will not be usable as dictionary keys. If a class defines mutable objects
1313+
and implements a :meth:`__cmp__` or :meth:`__eq__` method, it should not
1314+
implement :meth:`__hash__`, since the dictionary implementation requires that
1315+
a key's hash value is immutable (if the object's hash value changes, it will
1316+
be in the wrong hash bucket).
1317+
1318+
User-defined classes have :meth:`__cmp__` and :meth:`__hash__` methods
1319+
by default; with them, all objects compare unequal and ``x.__hash__()``
1320+
returns ``id(x)``.
13121321

13131322

13141323
.. method:: object.__bool__(self)

0 commit comments

Comments
 (0)