Why does changing 1 list affect the other?

Louis Pecora pecora at anvil.nrl.navy.mil
Fri Nov 7 14:32:55 EST 2003


In article <jQEqb.66010$ZC4.53825 at twister.nyroc.rr.com>,
 "Cy Edmunds" <cedmunds at spamless.rochester.rr.com> wrote:

> Try this:
> 
> >>> x = 5
> >>> y = x
> >>> print x is y
> True
> 
> y is second reference to the immutable value 5, not a copy.

So the action is on the binding end?  Only one 5 is created, but the x=4 
following statement UNbinds x and rebinds it to '4', rather than 
changing the object. y remains bound to '5'.  Did that come out right?

Then for lists, when we do   x[0]=4 we are NOT unbinding x, but rather 
just changing the object?

Sounds like if we try harder we will get to the _binding_ actions of the 
'=' operator.  Let me try harder right here (off the top of my head).

So, let's see, we view '=' as a binding operation.

x=list

binds x to the list, but each list element is bound to something else (I 
refer to this as a second level binding).  

For lists we are allowed to change that second level binding.  This 
second level action does NOT affect the first level y=x binding.

Now, does that sound right?  Anyone?

-- Lou Pecora




More information about the Python-list mailing list