[Python-ideas] incremental hashing in __hash__

Ethan Furman ethan at stoneleaf.us
Fri Dec 30 19:20:43 EST 2016


On 12/30/2016 03:36 PM, jab at math.brown.edu wrote:

> In the use cases I described, the objects' members are ordered. So in the unlikely event that two objects hash to the same value but are unequal, the __eq__ call should be cheap, because they probably differ in length or on their first member, and can skip further comparison. After a successful hash lookup of an object that's already in a set or dict, a successful identity check avoids an expensive equality check. Perhaps I misunderstood?

If you are relying on an identity check for equality then no two FrozenOrderedCollection instances can be equal.  Was that your intention?  It it was, then just hash the instance's id() and you're done.

If that wasn't your intention then, while there can certainly be a few quick checks to rule out equality (such as length) if those match, the expensive full equality check will have to happen.

--
~Ethan~


More information about the Python-ideas mailing list