Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - question about hashes and `None`

Tags:

python

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?

like image 326
orokusaki Avatar asked Dec 21 '22 16:12

orokusaki


1 Answers

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.

like image 200
Abbafei Avatar answered Jan 05 '23 10:01

Abbafei