Classes

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Fri Oct 31 22:24:11 CET 2014


On Friday, October 31, 2014 1:51:23 PM UTC-7, Chris Angelico wrote:
> On Sat, Nov 1, 2014 at 4:43 AM, 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.
> 
> Here we go again...
> 
> http://en.wikipedia.org/wiki/Circle-ellipse_problem
> 
> Square and Rectangle are just as "obviously" the other way around in
> the hierarchy, and it's wrong whichever way you do it. Not a good
> example.
> 
> ChrisA

I've never heard of the Circle-Ellipse problem, and my first instinct to Rob's post was to ask, why would you want to sub-class Rectangle into a Square class?  A square is just a special case of a Rectangle.  Attempting that kind of sub-classing would just cause problems.  The only thing you gain is a slight optimization in calculating perimeter by turning two multiplications and an addition into a single multiplication which really wouldn't have an effect on performance unless you were doing that calculation millions of times per second.



More information about the Python-list mailing list