Python Design Patterns - composition vs. inheritance

Odalrick odalrick at
Sat Nov 17 10:10:27 CET 2007

On 16 Nov, 16:35, Carl Banks <pavlovevide... at> wrote:
> On Thu, 15 Nov 2007 21:25:16 -0800, Dennis Lee Bieber wrote:
> > On Thu, 15 Nov 2007 16:57:57 -0800 (PST), Carl Banks
> > <pavlovevide... at> declaimed the following in comp.lang.python:
> >> A source of confusion with "is a" is that it doesn't necessarily imply
> >> a good inheritance relationship (aka Liskov substitutability). Consider
> >> a Rectangle class that has methods set_width() and set_height().
> >> Should Square class inherit from Rectangle?  A Square is a Rectangle,
> >> but it's not suitable as a subclass of Rectangle, because the width and
> >> height can't be set independently.  You can't substitute a Square for a
> >> Rectangle and get reasonable behavior.
> >    Well, you would if you override the two set_* methods to set both
> > height and width to the same value <G>
> But that breaks expectations: a user doesn't expect set_width() to affect
> the height.

I can't speak for everyone but I certainly expect setting the width of
a Square to change it's height. In fact, that would probably be the
reason I used a Square rather than a Rectangle in the first place.

More information about the Python-list mailing list