Search code examples
javapythonobject-identity

Is there a Python equivalent of Java's IdentityHashMap?


I'm walking a data structure and would like to build a dict mapping X->Y, where X is a field in the data structure I'm walking and Y is a field in the data structure I'm building on the fly. X is an unhashable type.


Solution

  • Trivially:

    idmap = {}
    idmap[id(x)] = y
    

    Use the id of x as the dictionary key