The indexing with a float was on a dict. If you can index with a string, why not a float? If it's hashable, it's good!
>>> k1 = "foo"
>>> k2 = hash(k1)
>>> hash(k1) == hash(k2)
True
>>> d = {k1: "v1", k2: "v2"}
>>> d[k1]
'v1'
>>> d[k2]
'v2'
Now that I've been pedantic, I hope I get this right: in Python, it is equality (==) that defines same-ness for dicts and sets. But if x == y, then you have to ensure that hash(x) == hash(y). Python uses this characteristic to make the initial check for dict and set membership integer comparisons on the hash, but when two items have the same hash, Python goes on to distinguish them on the basis of equality. class A:
def __hash__(self):
return -1
print(hash(A()))
Prints -2.For the same reason, if you ever need an easy hash collision, use the hashes of -1 and -2.
This was my first thought too, so I checked but
>>> hash(0)
0
>>> hash(0.0)
0
>>> hash('')
0
>>> {0: 4}['']
KeyError: ''
Which makes sense, because there is checking for hash collisions, and when they are resolved 0 == 0.0 while 0 != ''
def make_key(obj): return (obj.__class__.__module__, obj.__class__.__name__, hash(obj))
make_key(x) == make_key(y)