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

Rayme Jernigan rayme at pobox.com
Fri Aug 27 15:59:11 CEST 2004


Ack! Assignment creates references, not copies. A newbie mistake. 
Thanks for walking this out for me. Bob, you too, I appreciate your 
time.

-Rayme.

On Aug 26, 2004, at 5:14 PM, eichin at metacarta.com wrote:

> 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]
>
>
William R Jernigan Associates
2616 Huntsman Trail
Zebulon, NC  27597
Phone: 919.269.0692



More information about the Pythonmac-SIG mailing list