> Hi > > I was trying to learn about classes in Python and have been playing > around but I am having a problem with the deepcopy function. I want to > have a function that returns a clean copy of an object that you can > change without it changing the original, but no matter what I do the > original changes as well. Can anyone give ma a pointer?
What's really cute about this is the underlying pointers to PyObject s giving you troubles. ;-) So you see, you have lots of pointers! > def move_rectangle(rect, dx, dy): > rect2 = copy.deepcopy(rect) > rect2.corner.x += dx > rect2.corner.y += dy > return rect2 I want to mention something, even though the whole discussion has been beneficial to me as well as to others I'm sure, that you don't need a deepcopy here at all. I know that perhaps this is a simplified version of code that you might be working on, but perhaps the full version can be adapted as well??? def move_rectangle(rect, dx, dy): rect2 = rectange() rect2.width = rect.width rect2.height = rect.height rect2.corner.x = rect.corner.x+dx rect2.corner.y = rect.corner.y+dy return rect2 Hmmm... perhaps the necessary but cumbersome width and the height assignments are what makes the deepcopy so helpful in this example... Certainly they foreshadow the application of a lot of work with similar examples and larger objects.... HTH _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor