[Tutor] Can this be done easly

Knacktus knacktus at googlemail.com
Sun Sep 19 14:19:46 CEST 2010


Am 19.09.2010 10:49, schrieb Roelof Wobben:
>
>
> Hello,
>
> I have this programm :
>
> class Point:
>       def __init__(self, x=0, y=0):
>           self.x = x
>           self.y = y
>
> class Rectangle(Point):
>      def _init_(self, width=0, length=0):
>          self.width = width
>          self.length = length
You're inheriting the Point Class, but you don't initialise it.
For some detailled description of inheritance in Python I rather suggest 
to check out some tutorials instead of trying to explain it. Also, 
others on this list can do this probably better. Here's one reference:
http://diveintopython.org/object_oriented_framework/index.html

But now some remarks regarding your problem:

First, I would not consider a Rectangle as a special Point. It's not a 
relation like a Sportscar is a Car (is-a-relationship). It's more a 
relation a Rectangle has 4 Points (has-a-relationship), or 1 Point and a 
width and length. So, it's probably better to express your Rectangle 
class like this:

class Rectangle(object):
     def __init__(self, base_point, width=0, length=0):
         self.base_point = base_point
         self.width = width
         self.length = length

then you go (with German names ;-)):

punkt = Point(3,4)
rechteck = Rectangle(punkt,20,30)

In your Rectangle class, the __init__ method takes only two arguments 
(not counting the instance: self), but you're passing three arguments.

At the beginning, the error messages are a bit confusing, because they 
count the instance as one of the arguments. So it tells you, that you 
have given 4 arguments, but you might wonder "Hey, I gave you 3".

HTH,

Jan

>
> punt = Point(3,4)
> rechthoek = Rectangle (punt,20,30)
>
> Now I wonder how I can change this to rechthoek = rectangle (punt,20,20)
> This one gives as error message  :
>
> Traceback (most recent call last):
>    File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 12, in<module>
>      rechthoek = Rectangle (punt,20,30)
> TypeError: __init__() takes at most 3 arguments (4 given)
>
> Roelof
>    		 	   		
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list