Composition instead of inheritance

Ethan Furman ethan at stoneleaf.us
Fri Apr 29 03:43:35 CEST 2011


Carl Banks wrote:
> That's not what we mean by composition.  Composition is when one object
 > calls upon another object that it owns to implement some of its behavior.
 > Often used to model a part/whole relationship, hence the name.

Hmmm. Okay -- any ideas for a better term?  Something that describes 
taking different source classes and fusing them into a new whole, 
possibly using single-inheritance... Frankenstein, maybe?  ;)


> The sorts of class that this decorator will work for are probably not
 > the ones that are going to have problems cooperating in the first place.
 > So you might as well just use inheritance; that way people trying to read
 > the code will have a common, well-known Python construct rather than a
 > custom decorator to understand.

 From thread 'python and super' on Python-Dev:
Ricardo Kirkner wrote:
 > I'll give you the example I came upon:
 >
 > I have a TestCase class, which inherits from both Django's TestCase
 > and from some custom TestCases that act as mixin classes. So I have
 > something like
 >
 > class MyTestCase(TestCase, Mixin1, Mixin2):
 >    ...
 >
 > now django's TestCase class inherits from unittest2.TestCase, which we
 > found was not calling super.

This is the type of situation the decorator was written for (although 
it's too simplistic to handle that exact case, as Ricardo goes on to say 
he has a setUp in each mixin that needs to be called -- it works fine 
though if you are not adding duplicate names).

~Ethan~



More information about the Python-list mailing list