Object Reference?

Gandalf gandalf at geochemsource.com
Fri Aug 6 12:45:05 EDT 2004


> I'm trying to make a graphical editor and browser for Pickled files. 
> One aspect I'm not sure about is how to detect multiple references to 
> the same data. 

...

> For instance, say I had the Pickled data:
> a=[1,2,3]
> b=[a,4,5]
> c=[b,6,7]
> d=[a,b,c]
>
> The idea is to allow the user to browse this data and indicate 
> references. In this case, if 'a' was selected, the browser should show 
> that 'b', 'c', and 'd' contain a reference to 'a'. Inversely, if 'c' 
> were selected, it should indicate that it's first element just isn't a 
> list, but a reference to a list defined elsewhere, namely 'b'.


>>> Naturally, I could just recursively parse all the data comparing every
>>> element to every previously listed object, but is there a less 
>>> obtrusive
>>> method? Python figures out when to delete objects based on the 
>>> remaining
>>> references to an object. Is there a way to access this information to
>>> automatically lookup these references? Any help is greatly appreciated.
>>
>>
>> Use the "id()" of the objects?
>
>
> Well, I was thinking of a function that takes an object's id and 
> returns the ids of all the objects that reference it. Given that 
> Python keeps this information internally (I believe), my question is 
> how do I access his data and what would be the easiest way to code 
> this function? 

Yes, Python figures out when to delete an object, based on its reference 
count. But Python only counts the references. It keeps track of the 
number of references for the object. It does not know where the 
reference is located in memory. The short answer is that you cannot get 
this information effectively.

Long answer follows.

Question: There can be many objects in the structure without a name 
(e.g. not referenced from the value part of a dictionary).
How do you want to display them? As an example:

class A:
    pass

a = A() # THE object
b = [a,a,a,a]
del a

# At this point, THE object does not have a named reference at all but 
it is referenced four times. Do you really want to identify the objects 
with their memory address? The id function does this to you (Anthony 
Baxter said the same.) But this will be a useless pickle browser since 
nobody will notice memory address duplications.

Another big problem here. In Python, the only things you can have are an 
object references. You cannot name the object itself. There are no 
'object variables', only 'reference variables'. For example:

class A:
    pass

a = A() # THE object we are talking about
b = [a]
c = [b]

Here is the question: does the object b "references" a? The answer is 
that the question is wrong because 'a' is a reference itself - a 
reference can only reference one object at a time. Good questions are:

- does 'a' and 'b' reference to the same object? (No)
- does 'b[0]' and 'a' reference to the same object? (Yes)

You may think that a reference of 'a' is contained in 'b'. But this is 
not a general assumption. For example:

a = A()
b = A()
a.o = b
b.o =  a

Now which is contained in which?

Sorry for the long e-mail.

   G






More information about the Python-list mailing list