Binding a variable?

Kent Johnson kent37 at tds.net
Fri Oct 21 13:28:46 EDT 2005


Paul Dale wrote:
> 
> Hi everyone,
> 
> Is it possible to bind a list member or variable to a variable such that

No, Python variables don't work that way.
> 
> temp = 5

The name 'temp' is now bound to the integer 5. Think of temp as a pointer to an integer object with value 5.
> 
> list = [ temp ]

the name 'list' is bound to a list whose only member is a reference to the integer 5
> 
> temp == 6

Ok now temp is bound to a new integer, but the list hasn't changed - it's member is still a reference to 5
> 
> list
> 
> would show
> 
> list = [ 6 ]

You have to put a mutable object into the list - something whose state you can change. For example this works, because temp and lst[0] are references to the same mutable (changeable) list:
 >>> temp = [6]
 >>> lst = [temp]
 >>> lst
[[6]]
 >>> temp[0] = 5
 >>> lst
[[5]]

Kent



More information about the Python-list mailing list