Subclass sadness (was Re: [Python-Dev] PEP 285: Adding a bool type)

Andrew Koenig ark at research.att.com
Sun Mar 31 00:46:12 EST 2002


Aahz> Instead of circle/ellipse, it's slightly easier to talk about
Aahz> square/rectangle.  In terms of standard geometry, a square is a
Aahz> subset of rectangles, such that height and width are identical.
Aahz> But in OO terms, it's a bit more complicated to inherit square
Aahz> from rectangle, because square has only side-length attribute
Aahz> where rectangle has two (height and width).  So to create a
Aahz> class square that inherits from rectangle, you either have to
Aahz> write special code that ties height and width together or you
Aahz> have to delete at least one of the attributes (in which case any
Aahz> code expecting to work with rectangle fails).

Right you are.

The other way around -- inheriting rectangle from square -- would
seem to make more sense because a rectangle has all the information
that a square does, and then some.  But that doesn't really work either,
because then you can no longer assume that every kind of square is
really a square.  After all, it might be a rectangle.

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark



More information about the Python-list mailing list