Modifying Class Object

Dino Viehland dinov at microsoft.com
Wed Feb 10 23:58:33 EST 2010


Steve wrote:
> id() simply returns a unique value identifying a particular object. In
> CPython, where objects do not migrate in memory once created, the
> memory
> address of the object is used. In IronPython each object is assigned an
> id when it is created, and that value is stored as an attribute.

Just a point of clarification: In IronPython ids are lazily assigned upon
a call to the id().  They're actually fairly expensive to create because 
the ids need to be maintained by a dictionary which uses weak references.

> >> If you disagree, please write (in any implementation you like: it need
> >> not even be portable, though I can't imagine why ti wouldn't be) a
> >> Python function which takes an id() value as its argument and
> >> returns the value for which the id() value was provided.

Just for fun this works in IronPython 2.6:

>>> import clr
>>> clr.AddReference('Microsoft.Dynamic')
>>> from Microsoft.Scripting.Runtime import IdDispenser
>>> x = object()
>>> id(x)
43
>>> IdDispenser.GetObject(43)
<object object at 0x000000000000002B>
>>> IdDispenser.GetObject(43) is x
True

Please, please, no one ever use this code!

I do generally agree with the sentiment that id is object identity and in
way related to pointers though.




More information about the Python-list mailing list