Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contrary to Python 3.1 Docs, hash(obj) != id(obj). So which is correct?

Tags:

python

hash

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.

like image 478
Don O'Donnell Avatar asked Jun 12 '10 07:06

Don O'Donnell


1 Answers

I'm guessing this was a change made in Python 3.x to improve performance. Check out issue 5186, then look a little more closely at your mismatched numbers:

>>> bin(11893680)
'0b101101010111101110110000'
>>> bin(743355)
'0b10110101011110111011'
>>> 11893680 >> 4
743355

It's probably worth reporting as a documentation bug.

like image 155
ʇsәɹoɈ Avatar answered Sep 25 '22 15:09

ʇsәɹoɈ