References and copying

Donn Cave donn at u.washington.edu
Fri Jun 9 13:54:35 EDT 2006


In article <44899cb3$0$21184$626a54ce at news.free.fr>,
 bruno at modulix <onurb at xiludom.gro> wrote:
> danmcleran at yahoo.com wrote:
...
> >>And I don't understand it.  I thought, that b will be a reference to a,
> >>so changing b should change a as well.
> > 
> > 
> > No, you've set the name b to reference a slice of a. Slicing a list
> > always returns a new list.
> 
> Please verify before asserting:
> 
> >>> a = [[1, 2], [3, 4]]
> >>> b = a[1]
> >>> b is a[1]
> True
> >>> id(b)
> 46912496915448
> >>> id(a[1])
> 46912496915448
> >>>

You're right - he actually didn't set the name b to reference a
slice of a.  But if he had - slicing a list does return a new list.
Indexing, as in the example, returns the item object.  Or, binds a
reference to the left hand side identifier, whatever, but there is
no way to bind anything to the list location.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list