[Tutor] id()

Benoit Dupire bdupire@seatech.fau.edu
Thu, 26 Apr 2001 18:04:24 -0400


Stephen Aichele wrote:

> I have a question regarding the use of id(): basically, I'd like to find a way to reverse this process- in otherwords, is there a way to access a specific instance's attributes from the id # alone?
>
> I need to do this because in my app, I may have several instances of the same class up concurrently - all with the same variable name...  (I'm using wxPython) - I know that by reusing the variable name, I won't have access to any but the last instance created; however, I'm wondering if the previous instance IDs will still exist and if so, how to use them to access previous instances/windows...

if you don't keep track of the old objects, they are deleted by the garbage collector. You can't access them anymore.

I am not sure what you are referring to about wxPython....

To keep track of all your objects, just add them to a list...

my_list = []
for i in range(1,5):
    my_list.append( MyClass() )

Then you can access the last object:  my_list[-1]
and the previous ones...

If you want to refer an object by its id, you have to keep a table (dictionary) key= id, value = reference to the object...

I don't think there is another way to do it...

my_list = []
reverse_id_dict={}
for i in range(1,5):
    obj = MyClass()
    my_list.append( obj)
    reverse_id_dict[id(obj)] = obj

hope it helps

benoit



>
>
> sorry to be so confusing (and confused), but if anyone has an idea of my problem and how to deal with it, I'd love to hear it.
>
> thanks!
>
> -Stephen
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

--
Benoit Dupire
Graduate Student
----------------
I'd like to buy a new Boomerang. How can i get rid of the old one?