is id(self) constant over an object lifetime ?

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Dec 3 09:04:47 EST 2010


Adam Tauno Williams wrote:
> 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"
>
>   
Yep I saw that on the doc sorry for asking here before checking it.
Anyway your uuid is an awesome suggestion.

However since I'll need to send the id over xmlrpc, it needs to be 
marshallable, so I would need to send uuid1().hex, or something liek that.

JM




More information about the Python-list mailing list