anything like C++ references?

Mark Perrego perrego at mail.buffnet.net
Thu Jul 17 00:24:38 EDT 2003


I've been trying to follow this thread, mainly to make sure that how I 
think python works is how it actually does work.  I was confused by 
(paraphrasing) "in math, variables are bound to values, and values 
don't change, and python doesn't work that way."  I was saying to 
myself, "yes it does."  And now I think I know why:

>Stephen Horne wrote:
>If I type...
>
> >>> a = [1, 2, 3]
> >>> b = a
>
>Then in my mind, the point is that both variables get bound to the
>value '[1, 2, 3]'.

I don't believe this is the case.  At least not for your concepts of 
"variable", "bound" and "value."  To say it in (what I think are) your 
definitions of those terms...

>>> a = [1, 2, 3]

At this point, a is bound to a value which is a reference that refers 
to the list '[1, 2, 3]'.
The list referred to by the value bound to a contains 3 references, 
which refer to the integers 1, 2 and 3.

>>> b = a

At this point, b is bound to a value which is another reference to the 
same list.  

>If I now type...
>
>>>> b[2] = 4
>
>Then the variable b should now be bound to the value '[1, 2, 4]'. 

Why?  You didn't change the binding of b.  You changed the binding of 
the third spot in the list referred to by b.  Or to be more explicit 
"... referred to by the value bound by b".

>That is the whole point of what I've done. However, the fact that it 
was
>achieved by modifying a part of an object is an implementation detail.

But in reality, modifying an object is what you told python to do.  You 
asked to modify the list to throw away the previous reference it was 
holding in the third spot and to put another one in its place.

>It should not affect the *value* bound to the variable a. In other

It doesn't affect the value bound to the variable a.  The variable a is 
still bound to the same value.  That value is a reference to a 
particular list.  That list was modified.

>words, a new object should be created at that point - a copy of the
>original - which would then be modified. That way, the mathematical
>concept of variables being bound to values would be respected.

A copy of the original reference to the integer 3 is created, and 
modified to refer to the integer 4, then bound to the third spot in the 
list, replacing the original.  But this would violate the "values are 
immutable" concept so how about the new reference refers to the integer 
4 when it is created.  I don't know which is correct, and I don't know 
how you could tell.

Others, I believe, have used the term "bound" to mean what I am calling 
"referred to" which may be part of my confusion as I tried to pick 
which meaing of "bound" to take.  

Take all this with a grain of salt.  Its me trying to explain how I 
think python works in what I think are your terms.  And then there are 
the problems between my brain and my fingers.  Am I missing your point?  
I know you don't _like_ that python binds variables to values that are 
references, but do I have straight what you mean by variable, bound and 
value?

Thanks,
 - Mark





More information about the Python-list mailing list