trouble subclassing str

Donn Cave donn at u.washington.edu
Fri Jun 24 16:16:43 EDT 2005


In article <1119638083.069776.249140 at g43g2000cwa.googlegroups.com>,
 "Paul McGuire" <ptmcg at austin.rr.com> wrote:
...
> This reminds me of some maddening O-O discussions I used to
> have at a former place of employment, in which one developer cited
> similar behavior for not having Square inherit from Rectangle - calling
> Square.setWidth() would have to implicitly call setHeight() and vice
> versa, in order to maintain its squarishness, and thereby broke Liskov.
> I withdrew from the debate, citing lack of context that would have
> helped resolve how things should go.  At best, you can *probably* say
> that both inherit from Shape, and can be drawn, have an area, a
> bounding rectangle, etc., but not either inherits from the other.

This Squares and Rectangles issue sounds debatable in a
language like C++ or Java, where it's important because
of subtype polymorphism.  In Python, does it matter?
As a user of Square, I'm not supposed to ask about its
parentage, I just try to be clear what's expected of it.
There's no static typing to notice whether Square is a
subclass of Rectangle, and if it gets out that I tried
to discover this issubclass() relationship, I'll get a
lecture from folks on comp.lang.python who suspect I'm
confused about polymorphism in Python.

This is a good thing, because as you can see it relieves
us of the need to debate abstract principles out of context.
It doesn't change the real issues - Square is still a lot
like Rectangle, it still has a couple of differences, and
the difference could be a problem in some contexts designed
for Rectangle - but no one can fix that.  If you need Square,
you'll implement it, and whether you choose to inherit from
Rectangle is left as a matter of implementation convenience.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list