Object Reference question

Dave Angel davea at ieee.org
Fri Aug 21 09:56:57 CEST 2009


josef wrote:
> To begin, I'm new with python. I've read a few discussions about
> object references and I think I understand them.
>
> To be clear, Python uses a "Pass By Object Reference" model.
> x = 1
> x becomes the object reference, while an object is created with the
> type 'int', value 1, and identifier (id(x)). Doing this with a class,
> x = myclass(), does the same thing, but with more or less object
> attributes. Every object has a type and an identifier (id()),
> according to the Python Language Reference for 2.6.2 section 3.1.
>
> x in both cases is the object reference. I would like to use the
> object to refer to the object reference. If I have a gross
> misunderstanding, please correct me.
>
> The following is what I would like to do:
> I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
> is an object reference. Entering dk gives me the object: [MyClass0
> instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
> 0x0010 ... ]
>
> I need the object reference name (a,b,c,d) from dk to use as input for
> a file. Where do I find the memory location of the object reference
> and the object reference name memory location? I am unconcerned with
> the fact that the memory location will change the next time I run a
> python session. I will be using the object reference name for
> processing right away.
>
> My main focus of this post is: "How do I find and use object reference
> memory locations?"
>
> Thoughts?
> Thanks,
>
> Josef
>
>   
There was a similar query here within the last couple of months, and 
lots of interesting discussion.  But I never saw a use case convincing 
enough for me to want to remember how the various suggestions worked.  
Just how are you planning to use this?  Are you planning to write a 
debugger?

Or are you trying to keep mnemonic names for all instances of a 
particular class?  Is this for a particular program's use, or are you 
trying to create a library to be used to reverse engineer some software 
you con't control?

Several of your phrasings imply you don't understand Python yet.

"memory location" - invisible to python use.  And although id() will 
give you a hash-code that's actually a memory address, there's no direct 
way to use it.  And names (attributes) don't necessarily have an address.
"the object reference name (a,b,c,d) from dk"  What is this?  There's 
nothing that even conceptually looks like that when you assign   dk = 
[a, b, c, d]


A given object may have one to many references, and some of these may 
have names.  If you constrain those names to be in a particular context, 
it may be possible to search for which name(s) currently happen(s) to 
point to the given object.  For example, if you have the following at 
top level in a module:

a = MyClass0()
b = MyClass1()
c = MyClass2()
dk = [a, b, c]

then, given the  id() of  dk[2], you could search the particular modules 
global name dictionary, and find c.  But for the following fragment, you 
could not:

a = MyClass0()
b = MyClass1()
c = MyClass2()
dk = [a, b, c]
c = 42

dk remains the same, but the dk[2] item no longer has any name 
referencing it.

At any given instant of time, most objects in a typical program have no 
name associated with them.  Many of them never did have a name.  What 
would you want if dk had been created as:

dk = [MyClass0(), MyClass1(), MyClass2()]

or if a, b, and/or c were local variables in a function that's long 
since quit running, or that has run many times, each time creating new 
objects?

DaveA




More information about the Python-list mailing list