Is there an object unique identifier in Python

Python

Python Problem Overview


This would be similar to the java.lang.Object.hashcode() method.

I need to store objects I have no control over in a set, and make sure that only if two objects are actually the same object (not contain the same values) will the values be overwritten.

Python Solutions


Solution 1 - Python

id(x)

will do the trick for you. But I'm curious, what's wrong about the set of objects (which does combine objects by value)?

For your particular problem I would probably keep the set of ids or of wrapper objects. A wrapper object will contain one reference and compare by x==y <==> x.ref is y.ref.

It's also worth noting that Python objects have a hash function as well. This function is necessary to put an object into a set or dictionary. It is supposed to sometimes collide for different objects, though good implementations of hash try to make it less likely.

Solution 2 - Python

That's what "is" is for.

Instead of testing "if a == b", which tests for the same value,

test "if a is b", which will test for the same identifier.

Solution 3 - Python

As ilya n mentions, id(x) produces a unique identifier for an object.

But your question is confusing, since Java's hashCode method doesn't give a unique identifier. Java's hashCode works like most hash functions: it always returns the same value for the same object, two objects that are equal always get equal codes, and unequal hash values imply unequal hash codes. In particular, two different and unequal objects can get the same value.

This is confusing because cryptographic hash functions are quite different from this, and more like (though not exactly) the "unique id" that you asked for.

The Python equivalent of Java's hashCode method is hash(x).

Solution 4 - Python

You don't have to compare objects before placing them in a set. set() semantics already takes care of this.

   class A(object): 
     a = 10 
     b = 20 
     def __hash__(self): 
        return hash((self.a, self.b)) 
 
   a1 = A()
   a2 = A()
   a3 = A()
   a4 = a1
   s = set([a1,a2,a3,a4])
   s
=> set([<__main__.A object at 0x222a8c>, <__main__.A object at 0x220684>, <__main__.A object at 0x22045c>])

Note: You really don't have to override hash to prove this behaviour :-)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiononeselfView Question on Stackoverflow
Solution 1 - Pythonilya n.View Answer on Stackoverflow
Solution 2 - PythonVicki LaidlerView Answer on Stackoverflow
Solution 3 - PythonNed BatchelderView Answer on Stackoverflow
Solution 4 - PythonVamsi NerellaView Answer on Stackoverflow