[Tutor] A deeper explanation of ability to modify list elements in-place

Serdar Tumgoren zstumgoren at gmail.com
Thu Nov 11 17:30:14 CET 2010


>
>
> Would you have the same question about what takes place here?:
>
> >>> list_ = [3,4,5]
> >>> list_[1] = 10
> >>> list_
> [3, 10, 5]
> >>>
>
>
No surprise there. It's a familiar usage and it appears to be the canonical
example (in textbooks/tuts) for demonstrating "in-place" object
modification. In these examples the object being modified is the list
itself, rather than the objects stored in the list. The examples typically
show how the sort function sorts the actual list as opposed to returning a
sorted copy of the list.

But I think I see your point. The list object behaves the same as the
objects stored inside the list. In other words, the list object is a
reference to an ordered sequence of object references, and you're operating
on those (referenced) objects rather than copies of the objects when you
iterate over the list. Is that correct? If so, no doubt there's a simpler
way to say all that?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101111/153338d1/attachment.html>


More information about the Tutor mailing list