d-d d-d - 1 month ago 17
Python Question

Python hash() function on strings

How does a hash value of some particular string is calculated in CPython2.7?

For instance, this code:

print hash('abcde' * 1000)


returns the same value even after I restart the Python process and try again (I did it many times).

So, it seems that
id()
(memory address) of the string doesn't used in this computation, right? Then how?

Answer

Hash values are not dependent on the memory location but the contents of the object itself. From the documentation:

Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).

You can check out the source code for CPython implementation of the hash method of str class here:

http://svn.python.org/view/python/trunk/Objects/stringobject.c?view=markup#l1263

Comments