This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: hash() function gives the same result for -1 and for -2 argument (==-2)
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ardabro, eric.smith
Priority: normal Keywords:

Created on 2014-12-01 11:18 by ardabro, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg231932 - (view) Author: ardabro (ardabro) Date: 2014-12-01 11:18
built-in hash() function cannot be effectively used for integers due to 
weird behavior for -1 argument:

>>> hash(0)
0
>>> hash(-1)
-2                # !!!!!
>>> hash(-2)
-2
>>> hash(-3)
-3
>>>
msg231935 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-12-01 11:54
hash() is not defined to be unique, so you're always going to have collisions. The behavior of hash() for int that you're seeing is not a bug.

There's some background here: http://stackoverflow.com/questions/10130454/why-do-1-and-2-both-hash-to-2-in-python
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67162
2014-12-01 11:54:13eric.smithsetstatus: open -> closed

components: + Interpreter Core, - Library (Lib)

nosy: + eric.smith
messages: + msg231935
resolution: not a bug
stage: resolved
2014-12-01 11:18:28ardabrocreate