Finding the instance reference of an object

Larry Bates larry.bates at vitalEsafe.com
Thu Oct 16 23:00:59 EDT 2008


Joe Strout wrote:
> On Oct 16, 2008, at 10:59 AM, Larry Bates wrote:
> 
>>> how do i find that the name is 'bob'
>>
>> Short answer is that you can't.  This because Python's names (bob) are 
>> bound to objects (modulename.objectname()).  They are NOT variables as 
>> they are in "other" programming languages.
> 
> Which other programming languages?  I've never seen an OOP language that 
> didn't work the same way as Python.
> 
> However, 'bob' here really is a variable.  It's a variable whose value 
> (at the moment) is a reference to some object.
> 
>> It is perfectly legal in Python to bind multiple names to a single 
>> object:
>>
>> a=b=c=modulename.objectname()
> 
> Right -- three variables (a, b, and c) that all have the same value, 
> i.e. all refer to the same object.  There's nothing more mysterious here 
> than
> 
> i=j=k=42
> 
> where i, j, and k all have the same value.  (The OP's question would be 
> like asking "what is the name of the variable referring to 42?  And 
> while you might, in theory, be able to produce a list of all such 
> variables by trolling through the Python's internals, it's a bit of a 
> silly thing to do.)
> 
>> a, b, and c all point to the same object.  An object can have an 
>> unlimited number of names bound to it.  This is one of the most 
>> difficult concepts for many beginning Python programmers to understand 
>> (I know I had a difficult time at first).  It is just not how we 
>> taught ourselves to think about "variables" and you can write quite a 
>> lot of Python treating the names you bind to objects like they were 
>> "variables".
> 
> Well, they are variables.  I'm not quite grasping the difficulty here... 
> unless perhaps you were (at first) thinking of the variables as holding 
> the object values, rather than the object references.  That is indeed 
> something important to grasp, since it explains why if you do
> 
>  a = b   # where b was some object with an attribute 'foo'...
>  a.foo = 42
> 
> ...you now find that b.foo is 42 too.  Nothing mysterious once you 
> realize that the value of a and b is a reference to some object that has 
> a "foo" attribute.
> 
> Not sure if all this was helpful to anyone, but I hope so!
> 
> Best,
> - Joe
> 

Sorry Joe, but I respectfully disagree about "not being mysterious".  I've been 
on this list for about 8 years and this question/issue comes up EVERY week with 
newbies.  Python names are just not variables in the traditional sense.  If they 
were then:

a=b=c=[42]

would create three independent variables that each contained a list with a 
single element of 42.  That is not what Python does and just about every newbie 
gets bitten by that fact at least once.

ow many posts have a read here where people do things like:

a = [1,2,3,4,5]
b = a
b.append(6)

and can't figure out why a now contains [1,2,3,4,5,6]?

Without fail they thought that b = a copied the contents of a into b.  That is, 
they thought of a and b as variables that "hold" values.  Now the term 
"variables" may mean something different to you than most of the other newbies 
that frequent this list, but they all (including me when I first started) miss 
the subtilety of the name/value bindings at first (but boy are they beautiful 
when you finally see the light).  Perhaps you were lucky and were able to grasp 
this quite powerful concept more easily than most of us.

I think this is one of the most difficult concepts about Python to grasp for 
people coming from VB, C, Fortran, Cobol (oops showed my age there), etc.  We 
trained our minds to treat "variables" like named buckets (or memory locations). 
  Even the OP here wants to interrogate the object in the bucket and coerce it 
into giving him the bucket (variable) name.

The problem, as Steven so eloquently shows in a separate post, is that is is 
hard to talk about names that are bound to objects.  So we fall back into 
familiar language and use "variable" which confuses a lot of newbies because 
they have preconceived notion about what a "variable" is and what it does.

Regards,
Larry



More information about the Python-list mailing list