Object Reference?

Michael Hudson mwh at python.net
Fri Aug 6 07:28:05 EDT 2004


"Chris S." <chrisks at NOSPAMudel.edu> writes:

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

I would recommend getting initmately familiar with the format of
pickles (something I can't claim to be).  The information must be in
there somewhere.

Actually, maybe you could write a custom unpickler that keeps track of
this kind of information.  Something else I haven't tried :-) But,
seriously, I think the knowledge that these objects came out of a
particular pickle is going to be a powerful advantage for this sort of
thing, and you should use it for everything it's worth.

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

Well, there's gc.get_referrers() and gc.get_referrents(), but I think
they are probably overkill (see above).

Cheers,
mwh

-- 
  (ps: don't feed the lawyers: they just lose their fear of humans)
                                         -- Peter Wood, comp.lang.lisp



More information about the Python-list mailing list