Python hash() function on strings

37,905

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).

See CPython's implementation of str.__hash__ in:

Share:
37,905
d-d
Author by

d-d

Updated on July 28, 2022

Comments

  • d-d
    d-d almost 2 years

    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?

  • d-d
    d-d over 7 years
    thanks. so it seems, that the hash(string) will be the same from run to run, right?
  • Selcuk
    Selcuk over 7 years
    @d-d No, it is not guaranteed to be the same every time, but it is guaranteed to return the same value within the same process. If you want a non-changing hash, use hashlib functions instead.
  • d-d
    d-d over 7 years
    yeah, I thought about it, but I need something faster than any hash function in this module. may be murmur hash or so..
  • Selcuk
    Selcuk over 7 years
    Check out various hash functions here