Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hash( (-2,2) ) == hash( (2,-2) ) returns True (Python)

Tags:

python

hash

So, this is fun - Python's hash notoriously returns True on hash(-1) == hash(-2), as discussed elsewhere, but what about this?

>>> hash( (-2,2) ) == hash( (2,-2) )
True

Is this a feature?

Some other quick experiments:

>>>(-2,2) == (2,-2)
False
>>>hash( (-1,) ) == hash( (-2,) )
True
>>>hash( (-1,-2) ) == hash( (-2,-1) )
True
>>>hash( (-2.01,2.01) ) == hash( (2.01,-2.01) )
False
>>>hash( (-1,1) ) == hash( (1,-1) ) 
False
like image 739
Tom Stephens Avatar asked Sep 19 '25 03:09

Tom Stephens


1 Answers

It's not a feature; it's a coincidence. Hash collisions occur.

Python's int hashing is really dumb and its tuple hashing is usually okay.

Python's dict implementation is meant to kick serious butt with bad hashes, so it doesn't matter too much.

like image 187
Mike Graham Avatar answered Sep 20 '25 19:09

Mike Graham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!