Re: [Tutor] FW: Can this be done easly

2010-09-19 Thread Roelof Wobben




> From: andreeng...@gmail.com
> Date: Sun, 19 Sep 2010 20:54:01 +0200
> Subject: Re: [Tutor] FW: Can this be done easly
> To: rwob...@hotmail.com
> CC: tutor@python.org
>
> On Sun, Sep 19, 2010 at 8:33 PM, Roelof Wobben wrote:
>
>> 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
>
> Which output had you expected, and why?
>
>
> --
> André Engels, andreeng...@gmail.com
 
Hello, 
 
Oke, I see it now. There is only output when it's not right.
 
Roelof

  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: Can this be done easly

2010-09-19 Thread Andre Engels
On Sun, Sep 19, 2010 at 8:33 PM, Roelof Wobben  wrote:

> 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

Which output had you expected, and why?


-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: Can this be done easly

2010-09-19 Thread Roelof Wobben




> To: tutor@python.org
> From: __pete...@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@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

  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: Can this be done easly

2010-09-19 Thread Andre Engels
On Sun, Sep 19, 2010 at 7:02 PM, Roelof Wobben  wrote:
>
>
>
> 
>> From: rwob...@hotmail.com
>> To: __pete...@web.de
>> Subject: RE: [Tutor] Can this be done easly
>> Date: Sun, 19 Sep 2010 17:01:22 +
>>
>>
>>
>>
>> 
>>> To: tutor@python.org
>>> From: __pete...@web.de
>>> Date: Sun, 19 Sep 2010 18:27:54 +0200
>>> Subject: Re: [Tutor] Can this be done easly
>>>
>>> Roelof Wobben wrote:
>>>
> Hint: why does this work:
>
>> def __init__(self, x=0, y=0):
>
> ...while this doesnt:
>
>> def _init_(self, base_point, width=0, length=0):
>
> Peter
>>>
 Maybe because base_point has no value ?
>>>
>>> No. One __init__ has two underscores (correct) on each side, the other
>>> _init_ only one (wrong).
>>>
>>> ___
>>> Tutor maillist - Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> Hello,
>
> Everybody thanks.
>
> 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


This test happens to work, but your program is incorrect.

In moverect, you should work with the local variable (rectangle), not
with the global one (rechthoek), because it should be possible to call
it for any Rectangle, not just with the Rectangle that happens to be
called rechthoek. Then, when you call it, you should specify the
Rectangle that you call it for, so

test = moverect (Rectangle, 4, 3)

should be

test = moverect(rechthoek, 4, 3)

Furthermore, you do not use test (which will be null anyway), so you
can shorten this to

moverect(rechthoek, 4, 3)

-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: Can this be done easly

2010-09-19 Thread Peter Otten
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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] FW: Can this be done easly

2010-09-19 Thread Roelof Wobben




> From: rwob...@hotmail.com
> To: __pete...@web.de
> Subject: RE: [Tutor] Can this be done easly
> Date: Sun, 19 Sep 2010 17:01:22 +
>
>
>
>
> 
>> To: tutor@python.org
>> From: __pete...@web.de
>> Date: Sun, 19 Sep 2010 18:27:54 +0200
>> Subject: Re: [Tutor] Can this be done easly
>>
>> Roelof Wobben wrote:
>>
 Hint: why does this work:

> def __init__(self, x=0, y=0):

 ...while this doesnt:

> def _init_(self, base_point, width=0, length=0):

 Peter
>>
>>> Maybe because base_point has no value ?
>>
>> No. One __init__ has two underscores (correct) on each side, the other
>> _init_ only one (wrong).
>>
>> ___
>> Tutor maillist - Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>

Hello,

Everybody thanks.

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

Roelof
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor