indirectly addressing vars in Python
Jeremy Sanders
jeremy+complangpython at jeremysanders.net
Wed Oct 1 11:10:40 EDT 2008
Ross wrote:
> >>> myList[1]= myList[1]+1
The problem is this makes myList[1] point to a new integer, and not the one
that peas points to.
Python 2.5.1 (r251:54863, Jul 10 2008, 17:25:56)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> oats=[1]
>>> peas=[6]
>>> mylist = [oats, peas]
>>> mylist[1][0] = mylist[1][0]+1
>>> mylist
[[1], [7]]
>>> peas
[7]
This is because integers are immutable, but lists are mutable.
--
Jeremy Sanders
http://www.jeremysanders.net/
More information about the Python-list
mailing list