is id(self) constant over an object lifetime ?

Adam Tauno Williams awilliam at whitemice.org
Fri Dec 3 08:52:20 EST 2010


On Fri, 2010-12-03 at 14:44 +0100, Jean-Michel Pichavant wrote: 
> Hello fellows,
> I would need a unique internal identifier to an object. Can I use the 
> object python id ?
> class Foo:
>     def getUniqueIdentifier():
>        return id(self)
> This id needs to be unique and constant over the python process lifetime.

<http://docs.python.org/library/functions.html>

<quote>
Return the “identity” of an object. This is an integer (or long integer)
which is guaranteed to be unique and constant for this object during its
lifetime. Two objects with non-overlapping lifetimes may have the same
id() value.
</quote>

Of course, "lifetime" is an interesting concept.  If an object is
serializable then this id cannot be used as an idempotent value.

Personally I would never use this; if an object needs an idempotent
value - create one.  "import uuid"




More information about the Python-list mailing list