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

Aahz aahz at pythoncraft.com
Sat Mar 30 20:22:46 EST 2002


In article <slrnaacavs.2gs.philh at comuno.freeserve.co.uk>,
phil hunt <philh at comuno.freeserve.co.uk> wrote:
>On 30 Mar 2002 11:04:13 -0500, Andrew Koenig <ark at research.att.com> wrote:
>>
>>I wonder if this is the circle-ellipse problem over again?
>
>What is the circle-ellipse problem?

I'm not sure, but I'm going to guess for my own edification:

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

This kind of thing is what makes the Deitel book on Python really bad,
because it doesn't give a clean picture of proper OOP (they make circle
a subclass of point, and cylinder a subclass of circle, which is even a
worse botch than trying to deal with circle/ellipse).

In general, I think the evidence has shown that subclassing is an
exercise fraught with danger and should be de-emphasized as a primary
technique in OOP.  I'm very glad that Python uses implicit protocols
through magic methods for much of its OO processing; that makes it much
easier to cut through the Gordian Knot.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?



More information about the Python-list mailing list