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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With