Why does None
hash to -1042159082
(which I found to be equal to number of bytes in a Gigabyte negated)?
I realize this doesn't really effect my code, but I'm curious.
Hashes are used for dictionary key look-ups, so I decided to experiment:
>>> D = {-1042159082: 'Hello', None: 'Hi'}
>>> D[None]
'Hi'
>>> D[-1042159082]
'Hello'
>>>
I understand this as Python seeing two identical hashes and then checking type to see what's what. Is that right?
>>> {False: 'Hello', 0: 'Hi'}
{False: 'Hi'}
>>> {0: 'Hi', False: 'Hello'}
{0: 'Hello'}
This is just weird. What's more is that the first key is kept and the second value is kept.
Is this sorcery, or could somebody help me understand?
About 2 values which may produce the same output when passed to the built-in hash()
function (None
and -1042159082
in the question asked):
This is called a collision (see this page on Wikipedia for more information about collisions).
The hashing algorithm which Python uses has a special way to figure out which value the person really wants when there is a collision (see this page in the source code of CPython (the main Python interpreter), starting at line 51, for information on collisions for dicts, at the time of writing this answer; this file also has notes on how dicts are implemented).
About what happens with 0
and False
(and more useful info), see other answers to the current question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With