This post originated from an RSS feed registered with Python Buzz
by Terra Caines.
Original Post: shallow copies in python
Feed Title: Terra's blog
Feed URL: http://terracaines.blogspot.com/feeds/posts/default?alt=rss
Feed Description: I am developing a open source 3d tactical game in python. This is showing the steps.
When going to cast a second spell the grid targeting was hitting every previous unit and position.
I tracked it down to the fact that I made a copy of the executing object.
Now I made a copy to it so that it wouldn't interfere with the objects on the unit. So why did that cause more problems?
Because in my code for the object I would say add 3 units to a list. Because its a shallow copy it would also add it to the previous object. And you cant use deep copyies as that would duplicate the units.
However when I went to clear the list to reset it I just created a new one like self.list = []
So what it did was clear the copied variable and not the original. Thus making the copy actually made things worse as it made the copy interfere in only some ways. So I just removed the copy method and things started working again.
This is an interesting bug because I could have clearly spent less time on it and still fixed it by clearing the list. But I spent the extra time to actually remove the copy method because I knew that would cause me trouble in the future if I didn't
It should be noted though that the actions must be updated to deal with repeated use for example on my move object. This is however much more simple and better than trying to debug the copying stuff. Either could be done, but the latter is like doing security in php