473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Object Reference?

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.
Jul 18 '05 #1
6 2237
On Fri, 06 Aug 2004 05:39:51 GMT, Chris S. <ch*****@nospamudel.edu> wrote:
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?
Jul 18 '05 #2
Anthony Baxter wrote:
On Fri, 06 Aug 2004 05:39:51 GMT, Chris S. <ch*****@nospamudel.edu> wrote:
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?
Jul 18 '05 #3
"Chris S." <ch*****@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
Jul 18 '05 #4
Michael Hudson wrote:
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).


Thanks, these are exactly what I need. Pickle is just the file format.
The data are Python constructs, so I shouldn't have to bother with the
format at all, since I'm simply converting the Pickled file into generic
Python data. Plus, writing my own Pickle parser would be hugely
complicated since Pickle is essentially its own small programming language.
Jul 18 '05 #5
> 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

Jul 18 '05 #6
Gandalf wrote:
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.


The gc module has methods that list an object's referrers and referents.
Although you're correct in that we're unable to access the total number
of references since duplicate references are not counted. For instance

import gc
a=[1,2,3]
b=[a,a,a,a]
print gc.get_referrers(a)

would only print out one instance of b, even though it references 'a'
four times. However, this only seems to apply for direct references, so
the total number of references can be determined by scanning all
referrers and counting the duplicate ids.
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
29
by: web1110 | last post by:
If I have 2 variables, A and B, referencing the same object and then do a A.Dispose(), what happens to B?
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.