[Tutor] classes and the deepcopy function

Michael python at mrfab.info
Sat Jan 5 09:00:53 CET 2008


Hi Michael

Thanks for the quick reply, I think I get it. So becuase I did not 
declare them withing the init method using self they are shared by every 
object that is created, even completely brand new ones?

Is it normal practice to declare your variables in a class? I notice 
that you don't have to, you can create them as you go, but i thought 
declaring and initialising them in the class would make it easier to 
understand and see what the class is for and should contain.

Thanks again

Michael

Michael H. Goldwasser wrote:
> Hi Michael,
>
>   This is a very interesting example.  You do indeed have two distinct
>   copies.  The interdependence you are observing is because you have
>   defined CLASS-LEVEL variables (akin to static in Java) rather than
>   instance-level variables. This is because of their declaration
>   within the context of the class definition.
>
>   You should typically initialize instance variables within an
>   __init__ method and using names qualified with self.
>
>   Here is an updated version of your code.  If you still need another
>   explanation of why your previous code executed the way it did, just
>   let me know.
>
> With regard,
> Michael
>
> -----------------------------------------------------
>
> import copy
>
> class point:
>     "represents a point in 2d space"
>     def __init__(self):
>         self.x = 0
>         self.y = 0
>
>     def printpoints(self):
>         print "x is %g, y is %g" %(self.x, self.y)
>
> class rectangle:
>     "represents a rectangle"
>     def __init__(self):
>         self.width = 0
>         self.height = 0
>         self.corner = point()
>
> def move_rectangle(rect, dx, dy):
>     rect2 = copy.deepcopy(rect)
>     rect2.corner.x += dx
>     rect2.corner.y += dy
>     return rect2
>
> r1 = rectangle()
> r1.width = 20
> r1.height = 40
> r1.corner.x = 10
> r1.corner.y = 10
> r2 = move_rectangle(r1,5,2)
> print 'Rectangle', r1.width, r1.height
> r1.corner.printpoints()
> print 'Rectangle 2', r2.width, r2.height
> r2.corner.printpoints()
>
>
>
>
>
> On Saturday January 5, 2008, Michael wrote: 
>
>   
>>    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?
>>    
>>    Thanks
>>    Michael
>>    --------------------------
>>    import copy
>>    
>>    class point:
>>        "represents a point in 2d space"
>>        x = 0
>>        y = 0
>>        def printpoints(self):
>>            print "x is %g, y is %g" %(self.x, self.y)
>>    
>>    class rectangle:
>>        "represents a rectangle"
>>        width = 0
>>        height = 0
>>        corner = point()
>>    
>>    def move_rectangle(rect, dx, dy):
>>        rect2 = copy.deepcopy(rect)
>>        rect2.corner.x += dx
>>        rect2.corner.y += dy
>>        return rect2
>>    
>>    r1 = rectangle()
>>    r1.width = 20
>>    r1.height = 40
>>    r1.corner.x = 10
>>    r1.corner.y = 10
>>    r2 = move_rectangle(r1,5,2)
>>    print 'Rectangle', r1.width, r1.height
>>    r1.corner.printpoints()
>>    print 'Rectangle 2', r2.width, r2.height
>>    r2.corner.printpoints()
>>     
>
>
>   



More information about the Tutor mailing list