所以,这很有趣 - Python 的hash
臭名昭著的回报True
on hash(-1) == hash(-2)
, 正如其他地方所讨论的 https://stackoverflow.com/questions/10130454/why-do-1-and-2-both-hash-to-2-in-python,但是这个呢?
>>> hash( (-2,2) ) == hash( (2,-2) )
True
这是一个feature?
其他一些快速实验:
>>>(-2,2) == (2,-2)
False
>>>hash( (-1,) ) == hash( (-2,) )
True
>>>hash( (-1,-2) ) == hash( (-2,-1) )
True
>>>hash( (-2.01,2.01) ) == hash( (2.01,-2.01) )
False
>>>hash( (-1,1) ) == hash( (1,-1) )
False
这不是一个功能;而是一个功能。这是一个巧合。发生哈希冲突。
Python 的 int 哈希确实很愚蠢,而它的元组哈希通常还可以。
Python 的 dict 实现旨在用错误的哈希值来解决问题,所以这并不重要。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)