Partial 1.0 - Partial classes for Python

skip at pobox.com skip at pobox.com
Thu Feb 8 10:05:12 EST 2007


    Michele> That is a common design, but I don't like it, since it becomes
    Michele> very easy to get classes with dozens of methods inherited from
    Michele> everywhere, a modern incarnation of the spaghetti-code
    Michele> concept. I find it much better to use composition, i.e. to
    Michele> encapsulate the various behaviors in different objects and to
    Michele> add them as attributes.

Composition is great when you know how largish classes are going to be
composed ahead of time and/or already have the pieces available in the form
of other classes you want to reuse.  I use this fragment-by-multiple-
inheritance (I hesitate to call it a) pattern when I realize after a long
period of organic growth that a single-inheritance class has gotten too big.
It's often relatively easy to carve the class up into multiple related base
classes.  The next step after that might be to morph those independent base
classes back into delegated attributes.

Skip



More information about the Python-list mailing list