Classes
Seymore4Head
Seymore4Head at Hotmail.invalid
Fri Oct 31 19:22:13 EDT 2014
On Fri, 31 Oct 2014 18:57:31 -0400, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
>On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
><Seymore4Head at Hotmail.invalid> declaimed the following:
>
>>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
>><rgaddi at technologyhighland.invalid> wrote:
>>
>>
>>>Define a Square class, subclassed from Rectangle. Use getters/setters
>>>to enforce that the length and width must be equal. Confirm that
>>>length and width remain locked, and that perimeter() and area() work
>>>correctly.
>>
>>class Rectangle:
>> def __init__(self,length,width):
>> self.length=length
>> self.width=width
>> def area(self):
>> return self.length*self.width
>> def perimeter(self):
>> return 2*self.length+2*self.width
>>class Square(Rectangle):
>> def set_side (self):
>> if self.length!=self.width:
>>
> Where's the rest of that -- not to mention you called it "set_side" but
>never pass a side into it for use.
>
>-=-=-=-=-=-=-=-
>
>class Rectangle(object):
> def __init__(self, length, width=None):
> self.length = length
> if width is None:
> self.width = length
> else:
> self.width = width
> def area(self):
> return self.length * self.width
> def perimeter(self):
> return 2 * (self.length + self.width)
>
Thanks for posting that. I had given up on trying it. I follow the
changes you made up this point. I will have to think some more to get
the rest of this.
The way you provided a catch for not having a width, I don't
understand the purpose of a Square subclass. Couldn't side just be
length?
BTW I am willing to forget any mention of getter/setter. We can just
pretend that never happened.
>class Square(Rectangle):
> def __init__(self, side):
> self.side = side
> def _getLength(self):
> return self.side
> def _getWidth(self):
> return self.side
> def _setLength(self, vlu):
> self.side = vlu
> def _setWidth(self, vlu):
> self.side = vlu
> length = property(_getLength, _setLength)
> width = property(_getWidth, _setWidth)
>
>aRect = Rectangle(2, 4)
>print aRect.length, aRect.width, aRect.area(), aRect.perimeter()
>aRect.length = 9
>print aRect.length, aRect.width, aRect.area(), aRect.perimeter()
>
>
>aSqr = Square(3)
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>aSqr.length = 4
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>aSqr.width = 5
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>aSqr.side = 7
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>
>-=-=-=-=-=-=-=-=-
>Microsoft Windows [Version 6.1.7601]
>Copyright (c) 2009 Microsoft Corporation. All rights reserved.
>
>C:\Users\Wulfraed\Documents>cd "Python Progs"
>
>C:\Users\Wulfraed\Documents\Python Progs>property2.py
>2 4 8 12
>9 4 36 26
>3 3 3 9 12
>4 4 4 16 16
>5 5 5 25 20
>7 7 7 49 28
>
>C:\Users\Wulfraed\Documents\Python Progs>
More information about the Python-list
mailing list