Classes

Chris Angelico rosuav at gmail.com
Fri Oct 31 22:36:03 CET 2014


On Sat, Nov 1, 2014 at 8:24 AM,  <sohcahtoa82 at gmail.com> wrote:
> 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.
>

Yep. A square is indeed a special case of rectangle (and a circle is a
special case of ellipse), so it would make sense to have an
"is_square()" method (or isSquare or squarep or whatever you want to
call it). The hairiness of subclassing either to make the other means
it's an unideal example.

In fact, I would say the Python @property decorator is less well
suited to an inheritance situation than to a chronological change.
"This used to be a simple attribute, now it has
validation/formatting/logging/whatever." But it's hard to come up with
an example to illustrate this cleanly for a student. Personally, I'd
be inclined to skip properties altogether; learn about them later,
much later, and only if you actually need them.

ChrisA



More information about the Python-list mailing list