Contrary to Python 3.1 Docs, hash(obj) != id(obj). So which is correct?
- by Don O'Donnell
The following is from the Python v3.1.2 documentation:
From The Python Language Reference Section 3.3.1 Basic Customization:
object.__hash__(self)
... User-defined classes have __eq__() and __hash__() methods
by default; with them, all objects compare unequal (except
with themselves) and x.__hash__() returns id(x).
From The Glossary:
hashable
... Objects which are instances of user-defined classes are
hashable by default; they all compare unequal, and their hash
value is their id().
This is true up through version 2.6.5:
Python 2.6.5 (r265:79096, Mar 19 2010 21:48:26) ...
...
>>> class C(object): pass
...
>>> c = C()
>>> id(c)
11335856
>>> hash(c)
11335856
But in version 3.1.2:
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) ...
...
>>> class C: pass
...
>>> c = C()
>>> id(c)
11893680
>>> hash(c)
743355
So which is it? Should I report a documentation bug or a program bug?
And if it's a documentation bug, and the default hash() value for a user
class instance is no longer the same as the id() value, then it would be
interesting to know what it is or how it is calculated, and why it was
changed in version 3.