[Tutor] Can this be done easly

Roelof Wobben rwobben at hotmail.com
Sun Sep 19 17:48:02 CEST 2010




----------------------------------------
> Date: Sun, 19 Sep 2010 14:19:46 +0200
> From: knacktus at googlemail.com
> To: tutor at python.org
> Subject: Re: [Tutor] Can this be done easly
>
> 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
>> 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
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Hello, 
 
When I change everything to this :
 
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
        
punt = Point(3,4)
rechthoek = Rectangle (punt,20,30)
 
I get this message :
 
Traceback (most recent call last):
  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 13, in <module>
    rechthoek = Rectangle (punt,20,30)
TypeError: object.__new__() takes no parameters
 
Roelof
  		 	   		  


More information about the Tutor mailing list