Composition instead of inheritance

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Apr 29 04:31:15 CEST 2011


On Thu, 28 Apr 2011 15:35:47 -0700, Carl Banks wrote:

> On Thursday, April 28, 2011 10:15:02 AM UTC-7, Ethan Furman wrote:
>> For anybody interested in composition instead of multiple inheritance,
>> I have posted this recipe on ActiveState (for python 2.6/7, not 3.x):
>> 
>> http://code.activestate.com/recipes/577658-composition-of-classes-
instead-of-multiple-inherit/
>> 
>> Comments welcome!
> 
> 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.

I thought that was delegation. As in, when one object delegates some or 
all of it's behaviour to another object:

http://code.activestate.com/recipes/52295


> Often used to model a part/whole relationship, hence the name.

In mathematics, composition means to make a function by applying a 
function to the output of another. E.g.:

f o g(x) is equivalent to f(g(x))

(Strictly speaking, the "o" in the f o g should be a little circle rather 
than lowercase "O".)



-- 
Steven



More information about the Python-list mailing list