Skip to content

Instantly share code, notes, and snippets.

@ayust
Created March 24, 2012 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayust/2177775 to your computer and use it in GitHub Desktop.
Save ayust/2177775 to your computer and use it in GitHub Desktop.
wat.
Python 2.6.7 (r267:88850, Dec 2 2011, 20:27:26)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = -1
>>> b = -2
>>> hash(a) == hash(b)
True
>>> hash(a)
-2
>>> hash(b)
-2
>>> c = -3
>>> hash(c)
-3
>>> hash(b) == hash(c)
False
>>>
@djmitche
Copy link

It's a hash function...

@ayust
Copy link
Author

ayust commented Mar 24, 2012

Yeah, I know why it happens, it's just amusing that they chose to have -1 hash to -2 (along with -2 hashing to -2).

@lunixbochs
Copy link

https://github.com/python-git/python/blob/master/Objects/stringobject.c#L1220

-1 is used as the "not yet computed" cached value, so they (unwisely?) coerce any -1 hash to -2 to allow caching ^_^

@ayust
Copy link
Author

ayust commented Mar 28, 2012

Cool, @lunixbochs ! Thanks for tracking that down. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment