Changing Hash Values Across Versions

Christian Heimes lists at cheimes.de
Thu Jun 11 21:55:40 CEST 2009


Phil Thompson schrieb:
> How stable should the implementation of, for example, a string's hash
> across different Python versions?
> 
> Is it defined that hash("foo") will return the same value for Python 2.5.1,
> 2.6.1 and 2.6.2?

The hash of an object is an internal implementation detail. The hash()
value of an object isn't even stable for the one version of Python. It
depends on the architecture (32 vs. 64bit OS, maybe even big vs. little
endian). The hash sums of objects like classes may and most likely do
change when you restart the interpreter.

Python 2.5.4 (r254:67916, Feb  4 2009, 14:25:49)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct; struct.calcsize("P") * 8
32
>>> hash("a")
-468864544


Python 2.5.4 (r254:67916, Apr  4 2009, 17:56:17)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct; struct.calcsize("P") * 8
64
>>> hash("a")
12416037344


$ python2.5 -c "import os; print hash(os)"
140519221134232
$ python2.5 -c "import os; print hash(os)"
139731515618200
$ python2.5 -c "import os; print hash(os)"
140460433757080
$ python2.5 -c "import os; print hash(os)"
140707220554648




More information about the Python-list mailing list