python2 python3 hash() function

(2020-11-24 14:22:23) $ python2
Python 2.7.12 (default, Oct  5 2020, 13:56:01)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hash("hash")
7799588877615763652


$ python3
Python 3.5.2 (default, Oct  7 2020, 17:19:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hash("hash")
4054793604053049264

$ python3
Python 3.5.2 (default, Oct  7 2020, 17:19:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hash("hash")
6415535283641827469

python2 的hash function是固定值

python3的hash function每次重启都不一样的值

$ python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from py27hash.hash import hash27 as hash
>>> hash("hash")
7799588877615763652

用py27hash.hash 库来进行python2的hash。

pip3 install py27hash

LEAVE A COMMENT