Python: how does a dict with mixed key type work


I understand that the following is valid in Python: foo = {'a': 0, 1: 2, some_obj: 'c'}

However, I wonder how the internal works. Does it treat everything (object, string, number, etc.) as object? Does it type check to determine how to compute the hash code given a key?

Best Solution

Types aren't used the same way in Python as statically types languages. A hashable object is simply one with a valid hash method. The interpreter simply calls that method, no type checking or anything. From there on out, standard hash map principles apply: for an object to fulfill the contract, it must implement both hash and equals methods.