[Tutor] FW: Can this be done easly

Roelof Wobben rwobben at hotmail.com
Sun Sep 19 20:33:46 CEST 2010




----------------------------------------
> To: tutor at python.org
> From: __peter__ at web.de
> Date: Sun, 19 Sep 2010 20:07:05 +0200
> Subject: Re: [Tutor] FW: Can this be done easly
>
> Roelof Wobben wrote:
>
>> For this exercise :
>>
>> 3.Write a function named move_rect that takes a Rectangle and two
>> parameters named dx and dy. It should change the location of the rectangle
>> by adding dx to the x coordinate of corner and adding dy to the y
>> coordinate of corner.
>>
>> Is this one of the possible solutions :
>>
>> class Point:
>> def __init__(self, x=0, y=0):
>> self.x = x
>> self.y = y
>>
>> class Rectangle(object):
>> def __init__(self, base_point, width=0, length=0):
>> self.base_point = base_point
>> self.width = width
>> self.length = length
>>
>> def moverect(rectangle, dx, dy):
>> rechthoek.base_point.y += dy
>> rechthoek.base_point.x +=dx
>> return rechthoek
>>
>> punt = Point(3,4)
>> rechthoek = Rectangle (punt,20,30)
>> test = moverect (Rectangle, 4,3)
>> print rechthoek.base_point.x
>
> At first glance I'd say so. At second glance I see that you pass the class
> and not an instance to the moverect() routine. Your program only seems to
> work because you are not using the parameter rectangle but the global
> rechthoek variable and as soon as you are trying to move rectangles with a
> different variable name everything will break.
> If I were to run your program I might even find more errors or problematic
> behaviours.
>
> In the long run it's not a sustainable model to verify the correctness of
> your programs by asking someone on the internet who is just as likely to be
> wrong as you or might even fool you.
>
> Instead add some tests. For example, you could precalculate the expected
> position and then check if the new position meets your expectation:
>
> r = Rectangle(Point(3, 4), 20, 30)
>
> moverect(r, 10, 11)
>
> if r.base_point.x == 13 and r.base_point.y == 15:
> print "looks good"
> else:
> print "needs work"
>
> Because tests are needed very often there are libraries accompanying the
> interpreter (unittest, doctest) to formalize them and for simple runtime
> checks there is the assert statement. Instead of the if...else you could
> write
>
> assert r.base_point.x == 13, "wrong x position %d" % r.base_point.x
> assert r.base_point.y == 15, "wrong y position %d" % r.base_point.y
>
> Peter
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
 
Hello, 
 
I changed the programm to this :
 
import unittest
class Point:
    def __init__(self, x=0, y=0): 
        self.x = x
        self.y = y
     
class Rectangle(object):
    def __init__(self, base_point, width=0, length=0):
        self.base_point = base_point
        self.width = width
        self.length = length
        
def moverect(roelof, dx, dy):
    roelof.base_point.y += dy
    roelof.base_point.x +=dx
    return roelof

r = Rectangle(Point(3, 4), 20, 30) 
moverect(r, 10, 11)
assert r.base_point.x == 13, "wrong x position %d" % r.base_point.x
assert r.base_point.y == 15, "wrong y position %d" % r.base_point.y
 
But no output at all
 
Roelof

  		 	   		  


More information about the Tutor mailing list