Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to get back an object from its id() value

7 views
Skip to first unread message

TP

unread,
Apr 8, 2009, 11:46:11 AM4/8/09
to
Hi everybody,

I have a data structure (a tree) that has one constraint: I can only store
strings in this data structure.

To know if an object foo already exists in memory, I store "str(id(foo))" in
the data structure.
OK.

But how do I get a usable reference from the id value?
For example, if "foo" has a method "bar()", how can I call "foo.bar()"
from "str(id(foo))" (say, 149466208).

Thanks in advance,

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)

CTO

unread,
Apr 8, 2009, 12:09:03 PM4/8/09
to
> But how do I get a usable reference from the id value?
> For example, if "foo" has a method "bar()", how can I call "foo.bar()"
> from "str(id(foo))" (say, 149466208).

can you just use a weakref instead? It is certainly cleaner than
trying to store id's,
since an id is only guaranteed to be unique for the lifetime of the
object in question,
and the only way I know of correlating id's to objects involves some C-
level trickery.

for completeness- the trickery: http://www.friday.com/bbum/2007/08/24/python-di/

MRAB

unread,
Apr 8, 2009, 12:05:30 PM4/8/09
to pytho...@python.org
TP wrote:
> Hi everybody,
>
> I have a data structure (a tree) that has one constraint: I can only store
> strings in this data structure.
>
> To know if an object foo already exists in memory, I store "str(id(foo))" in
> the data structure.
> OK.
>
> But how do I get a usable reference from the id value?
> For example, if "foo" has a method "bar()", how can I call "foo.bar()"
> from "str(id(foo))" (say, 149466208).
>
You could create a dict with the string as the key and the object as the
value.

Gary Herron

unread,
Apr 8, 2009, 12:14:38 PM4/8/09
to TP, pytho...@python.org
TP wrote:
> Hi everybody,
>
> I have a data structure (a tree) that has one constraint: I can only store
> strings in this data structure.
>
> To know if an object foo already exists in memory, I store "str(id(foo))" in
> the data structure.
> OK.
>
> But how do I get a usable reference from the id value?
> For example, if "foo" has a method "bar()", how can I call "foo.bar()"
> from "str(id(foo))" (say, 149466208).
>


Short answer: You can't!

Longer answer: You still can't, but you may be able to work around it:

(1) Build up a dictionary in parallel to the structure:
idMapper[id(foo)] = foo
and later, index with
idMapper[int(idString)] to get foo back

(2) Get a different data structure. This one is clearly not satisfying
your needs.

(3) Pickle (or marshal or serialize as it's also called) your object
into a (perhaps) long string to store. When the string is retrieved,
unpickle to reconstruct an equivalent object. This new version of the
original foo may or may not be adequate for your use.

Gary Herron

> Thanks in advance,
>
> Julien
>
>

CTO

unread,
Apr 8, 2009, 12:17:07 PM4/8/09
to
<snip>

> You could create a dict with the string as the key and the object as the
> value.
</snip>

This will create a strong reference to the object, which is (I'm
assuming) undesired behavior.

TP

unread,
Apr 8, 2009, 12:20:56 PM4/8/09
to
MRAB wrote:

> You could create a dict with the string as the key and the object as the
> value.

Thanks. But it implies an additional data structure: a dictionnary.
I don't know what is the best:
* using an additional dict and maintaining it
* or using the "di" module proposed by CTO

If "di" is reliable, it seems a good solution for my initial constraint
which is the impossibility to store anything but strings in my data
structure.

CTO

unread,
Apr 8, 2009, 1:02:46 PM4/8/09
to
> I don't know what is the best:
> * using an additional dict and maintaining it

It works, but as you say, is somewhat inelegant.

> * or using the "di" module proposed by CTO

Let me be clear: I am not proposing that you use it. It *does* do what
you
ask- but what you are asking is, all by itself, not a good idea. The
dict
is a vastly superior- and standard- solution to the problem of mapping
one object onto another.

> If "di" is reliable, it seems a good solution for my initial constraint
> which is the impossibility to store anything but strings in my data
> structure.

di is not reliable. Its author says so, and took a lot of heat for not
saying so in Hollywood-sign letters, lit on fire and a thousand feet
tall.

If all you are worried about is storing a string, how about
UserString?

Notice the difference:

>>> from collections import UserString
>>> import weakref
>>> weakref.ref("ABC")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create weak reference to 'str' object
>>> weakref.ref(UserString("ABC"))
<weakref at 0xb7baef2c; to 'UserString' at 0xb7a330ec>


This way you don't have to maintain a dictionary, only store
your string once, and don't have to hack and kludge your way
around id mappings.

CTO

unread,
Apr 8, 2009, 1:04:51 PM4/8/09
to
Correction: the UserString will be dead on the final line. When I
typed
it in I had a strong reference still hanging around.

Christian Heimes

unread,
Apr 8, 2009, 2:03:31 PM4/8/09
to pytho...@python.org
TP wrote:
> If "di" is reliable, it seems a good solution for my initial constraint
> which is the impossibility to store anything but strings in my data
> structure.

'di' is dangerous and not reliable. When the original object is freed,
then the memory address may be used by another Python object, point to
an invalid Python object or some random bytes. You can easily mess up or
seg fault the Python interpreter. 'di' is an evil debugging hack that
may proof useful as last resort.

Do *not* use anything like 'di' in production code.

Christian


0 new messages