Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the mathematical operation used by the python hash() function?

print(hash('hello world'))

result :

6266945022561323786

What is the mathematical used by the python hash() function ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The Python documentation makes no guarantee about the particular algorithm that is used by hash() (or more precisely, object.__hash__(self)).

The documentation only says this:

object.__hash__(self)

Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, and dict. The __hash__() method should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple. Example:

def __hash__(self):
    return hash((self.name, self.nick, self.color))

The only thing that is guaranteed is this: "The only required property is that objects which compare equal have the same hash value".

There are a couple of desirable properties related to security, safety, and performance, but they are not required.

Every object can implement its own __hash__() however it wants, as long as it satisfies the property that two equal objects have the same hash value. And, in fact, many objects do provide their own implementations.

Even for built-in core objects such as strings, different implementations (and even different versions of different implementations) use different algorithms. CPython even uses a different seed value every time you run it (again, for security reasons).

So, the answer is: you can’t know what the algorithm is. All you know is that if a.__eq__(b) is True, then a.__hash__().__eq__(b.__hash__()) is also True.

Most importantly, there is no guarantee that a.__hash__().__eq__(b.__hash__()) being True implies a and b are equal, nor does a.__eq__(b) being False imply that a.__hash__().__eq__(b.__hash__()) is False.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading