[Python-ideas] incremental hashing in __hash__

jab at math.brown.edu jab at math.brown.edu
Fri Dec 30 22:24:08 EST 2016


On Fri, Dec 30, 2016 at 10:08 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> So maybe this will work?
>
>     def __hash__(self):
>         return hash(self.name) * hash(self.nick) * hash(self.color)
>
> In other words, don't create a new tuple, just use the ones you already
> have and toss in a couple maths operations.  (and have somebody vet that ;)



See the "Simply XORing such results together would not be order-sensitive,
and so wouldn't work" from my original post. (Like XOR, multiplication is
also commutative.)

e.g. Since FrozenOrderedCollection([1, 2]) != FrozenOrderedCollection([2,
1]), we should try to avoid making their hashes equal, or else we increase
collisions unnecessarily.

More generally, I think the trick is to get an even, chaotic distribution
into sys.hash_info.hash_bits out of this hash algorithm, and I think simply
multiplying hash values together like this wouldn't do that.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161230/7a379295/attachment.html>


More information about the Python-ideas mailing list