[issue9982] Pointer problem in initializing array of arrays

2010-09-29 Thread Marvin Mundry

New submission from Marvin Mundry marvin@gmx.de:

 m1=[[0,0,0,0]]*4
 m1
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
 m1[0][0]+=1
 m1
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]

after initializing an array of arrays as done in the first line of the code 
snippet all elements in the array point to the same object in memory.

--
components: None
messages: 117581
nosy: mastermarv
priority: normal
severity: normal
status: open
title: Pointer problem in initializing array of arrays
versions: Python 2.6

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9982
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9982] Pointer problem in initializing array of arrays

2010-09-29 Thread Antoine Pitrou

Antoine Pitrou pit...@free.fr added the comment:

That's expected behaviour, syntactically. Multiplying a sequence doesn't 
deep-copy its elements.
If you want an array of distinct arrays, just write:

 m1 = [[0,0,0,0] for i in range(4)]
 m1[1][0] = 6
 m1
[[0, 0, 0, 0], [6, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

--
nosy: +pitrou
resolution:  - invalid
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9982
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com