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

T

TP

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

MRAB

TP said:
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.
 
C

CTO

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/
 
G

Gary Herron

TP said:
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
 
C

CTO

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

TP

MRAB said:
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.

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

CTO

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:
Traceback (most recent call last):
<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.
 
C

CTO

Correction: the UserString will be dead on the final line. When I
typed
it in I had a strong reference still hanging around.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top