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

Serdar Tumgoren zstumgoren at gmail.com
Thu Nov 11 15:02:44 CET 2010


Hi folks,
I'm trying to gain a deeper understanding of why it's possible to modify
list elements in-place *without* replacing them. For instance, why is the
below possible?

>>> class Dummy(object):
...     pass
...
>>> a = Dummy()
>>> b = Dummy()
>>> x = [a, b]
# modify list elements in-place
>>> x[1].name = 'The Dude'
>>> print x[1].name
The Dude
>>> x[0].name = 'The Stranger'
>>> for item in x:
...     print item.name
...
The Stranger
The Dude
# modify elements by iterating over the list
>>> for item in x:
...     item.start_date = 2010

>>> for item in x:
...     print item.name, item.start_date
...
The Stranger 2010
The Dude 2010

I figured the below quote offers the beginning of an explanation on this
page:
"The list object stores pointers to objects, not the actual objects
themselves."
http://effbot.org/zone/python-list.htm

But I was hoping you all could provide a more in-depth explanation, or even
better, direct me to some other readings and python code that implements
this behavior. Can anyone point me in the right direction for further study?

Thanks!
Serdar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101111/f2b1d5d3/attachment-0001.html>


More information about the Tutor mailing list