Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an object unique identifier in Python

Tags:

python

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.

like image 862
oneself Avatar asked Aug 09 '09 21:08

oneself


People also ask

What is a Python object identity?

Identity in Python refers to the object you are referring to. In Python, the identity of an object is a unique, constant integer (or long integer) that exists for the length of the object's life.

Can the id of two objects be the same Python?

id() function in Python is an inbuilt function that returns a unique integer identity of an object. This identity has to be 100% unique and constant for this object during its lifetime, although two objects may have the same id() value if they have non-overlapping lifetimes.

How do you create a unique code in Python?

uuid1() is defined in UUID library and helps to generate the random id using MAC address and time component. bytes : Returns id in form of 16 byte string. int : Returns id in form of 128-bit integer. hex : Returns random id as 32 character hexadecimal string.

How do you find the value of an object in Python?

Python getattr() function is used to get the value of an object's attribute and if no attribute of that object is found, default value is returned. Basically, returning the default value is the main reason why you may need to use Python getattr() function.


3 Answers

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.

like image 55
ilya n. Avatar answered Oct 03 '22 18:10

ilya n.


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.

like image 20
Vicki Laidler Avatar answered Oct 03 '22 18:10

Vicki Laidler


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).

like image 38
Ned Batchelder Avatar answered Oct 03 '22 17:10

Ned Batchelder