Any built-in ishashable method ?
christian at python.org
Fri Jan 18 13:25:18 CET 2013
Am 18.01.2013 12:56, schrieb Jean-Michel Pichavant:
> You guessed right. But it took me a lot of time before jumping to that conclusion, mostly because the code worked in the first place (it can with a little bit of luck).
> Now I'm extra careful about what I use as dict key, I was just wondering if there was a reliable quick way to verify an object can be used as key.
> That brings me to another question, is there any valid test case where key1 != key2 and hash(key1) == hash(key2) ? Or is it some kind of design flaw ?
That's a valid case. Hashing is not a permutation, it's (usually)
surjective, not bijective. hash(key1) == hash(key2) is called a "hash
A good hashing algorithm should try to reduce the probability of
collisions for likely keys. A secure algorithm should also make it as
hard as possible to calculate deliberate collision. Collisions diminish
the efficiency of a hash map from O(1) to O(n) and can be used for
denial of service attacks.
More information about the Python-list