Copying from a list instead of referencing an index

This one is a bit hard to explain. To see it: download the attached file, Senso.zip, and extract it somewhere, then run senso_editor.py. Now click on the Ships tab, create a new ship, and then copy it with the Copy button. Then change one of the ships, and check the other one; both the ships have changed.

I have isolated the problem to be the copying method. For ships, it is on line 462:

self.ships.append(self.ships[self.selectedship])

self.ships is a list of dictionaries containing all necessary information for each ship (position, size, speed, etc). That line is supposed to copy one of these dictionaries and append it to the end of self.ships. Instead, what it seems to be doing is appending a reference to the ship I’m trying to make a copy of. I was surprised that changing it to:

self.ships.append(self.ships[self.selectedship:self.selectedship+1][0])

seemed to have no effect.

I am completely stumped. I have no idea at all how to solve this problem. How can I accomplish what I’m trying to do?

I don’t think it’s relevant, but I only tested this on Fedora 13; I haven’t tested it on Windows yet.

Senso.zip (26.1 KB)

From:
wxpython-users@googlegroups.com [mailto:wxpython-users@googlegroups.com] On
Behalf Of
onpon4

···

Sent: Friday, September 03, 2010 10:55 AM
To: wxpython-users@googlegroups.com
Subject: [wxPython-users] Copying from a list instead of referencing an
index

This one is a bit hard to explain. To see it: download the
attached file, Senso.zip, and extract it somewhere, then run senso_editor.py.
Now click on the Ships tab, create a new ship, and then copy it with the Copy
button. Then change one of the ships, and check the other one; both the ships
have changed.

I have isolated the problem to be the copying method. For ships, it is on line
462:

self.ships.append(self.ships[self.selectedship])

self.ships is a list of dictionaries containing all necessary information for
each ship (position, size, speed, etc). That line is supposed to copy one of
these dictionaries and append it to the end of self.ships. Instead, what it
seems to be doing is appending a reference to the ship I’m trying to make a
copy of. I was surprised that changing it to:

self.ships.append(self.ships[self.selectedship:self.selectedship+1][0])

seemed to have no effect.

I am completely stumped. I have no idea at all how to solve this problem. How
can I accomplish what I’m trying to do?

I don’t think it’s relevant, but I only tested this on Fedora 13; I haven’t
tested it on Windows yet.


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

[alm>]

I
think you were appending a reference. This fixes it.

self.ships.append(self.ships[self.selectedship].copy())

I’m
on XP, by the way, and only tried the issue. Seems OK.

Al

Thanks, that was just what I needed.

···

On Fri, Sep 3, 2010 at 2:13 PM, Al Mansur almansur@rusnam.org wrote:

From:
wxpython-users@googlegroups.com [mailto:wxpython-users@googlegroups.com] On
Behalf Of
onpon4
Sent: Friday, September 03, 2010 10:55 AM
To: wxpython-users@googlegroups.com
Subject: [wxPython-users] Copying from a list instead of referencing an
index

This one is a bit hard to explain. To see it: download the
attached file, Senso.zip, and extract it somewhere, then run senso_editor.py.
Now click on the Ships tab, create a new ship, and then copy it with the Copy
button. Then change one of the ships, and check the other one; both the ships
have changed.

I have isolated the problem to be the copying method. For ships, it is on line
462:

self.ships.append(self.ships[self.selectedship])

self.ships is a list of dictionaries containing all necessary information for
each ship (position, size, speed, etc). That line is supposed to copy one of
these dictionaries and append it to the end of self.ships. Instead, what it
seems to be doing is appending a reference to the ship I’m trying to make a
copy of. I was surprised that changing it to:

self.ships.append(self.ships[self.selectedship:self.selectedship+1][0])

seemed to have no effect.

I am completely stumped. I have no idea at all how to solve this problem. How
can I accomplish what I’m trying to do?

I don’t think it’s relevant, but I only tested this on Fedora 13; I haven’t
tested it on Windows yet.

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

[alm>]

I
think you were appending a reference. This fixes it.

self.ships.append(self.ships[self.selectedship].copy())

I’m
on XP, by the way, and only tried the issue. Seems OK.

Al

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en