1

I know we shouldn't rely on the return value of hash() to be consistent between different versions. But I have this legacy code that needs to be migrated from python 2.7 to python 3.6. And one critical part of it is that I need keep hash() result to be same.

Is there any way to achieve that?

I've tried to set PYTHONHASHSEED to 0, but it doesn't help.

$ PYTHONHASHSEED=0 python3
Python 3.6.3 (default, Oct  8 2017, 23:14:45)
>>> hash('abc')
4596069200710135518

$ python2
Python 2.7.14 (default, Mar 30 2018, 12:43:21)
>>> hash('abc')
1453079729188098211
Ryan Ye
  • 3,159
  • 1
  • 22
  • 26
  • 2
    https://stackoverflow.com/questions/40137072/why-is-hash-slower-under-python3-4-vs-python2-7 python 3 now uses SipHash... – Jean-François Fabre May 22 '18 at 15:01
  • 4
    Why do you need the hash to be the same? It seems like the legacy code was planned poorly--no app should be dependent on the details of the hash function unless it does its own hashing. – Rory Daulton May 22 '18 at 15:04
  • There's python code for the old hash function on https://peps.python.org/pep-0456/ under "Current implementation with modified FNV" – Joachim Wagner Sep 21 '22 at 11:22

0 Answers0