[Pythonmac-SIG] memory corruption Python 2.3/Mac OS 10.3?

eichin at metacarta.com eichin at metacarta.com
Thu Aug 26 23:14:59 CEST 2004


The following example should make it clearer to you what is happening
(on linux python 2.1, as it happens, the behaviour is portable, and
not a bug.) A clearer (as well as correct) version might use:

    list_of_lists.append([0] * len(node))

instead...

#!/usr/bin/python

x = 0
list_of_lists = [ ]
		
node = [0,0,0,0,0,0,0]

listsize = 3
# initialize the list with empty list elements				
while x < listsize:
    list_of_lists.append(node)
    x += 1

import pprint
pprint.pprint(list_of_lists)
#  [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
list_of_lists[1][5] = 3
pprint.pprint(list_of_lists)
# [[0, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 3, 0]]
pprint.pprint(node)
# [0, 0, 0, 0, 0, 3, 0]


More information about the Pythonmac-SIG mailing list