Martin McBride
Sep 14, 2021

I don't think what you are saying in the dictionary example is quite correct. There is nothing wrong with using integers as keys, that is how lists work. NumPy even uses tuples of integers as keys, and it works very well.

There is a problem with using floats as keys, but it has nothing to do with hashes at all. Python only uses hashes to efficiently detect that a key doesn't match. If the hashes match, it then still checks for equality before deciding that the keys match.

1 and 1.0 are considered equal in Python, so they are interchangeable as the same key. 0.8 and (10.0-9.2) are not equal in Python (due to rounding errors) so they behave as different keys. Again, nothing to do with hashes. It is the age old issue that you shouldn't compare two floats for equality, due to rounding errors.

Martin McBride
Martin McBride

No responses yet