Composition instead of inheritance

Ben Finney ben+python at benfinney.id.au
Fri Apr 29 05:50:53 CEST 2011


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> On Thu, 28 Apr 2011 15:35:47 -0700, 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.
>
> 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

The difference being that, with delegation, the object being called is
unrelated to this one. With composition, the object being called is not
wholly separate, but is instead an attribute on the current object.

The “another object that it owns” was, I presume, meant to communicate
this “an object which is an attribute of the current one” relationship.

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

The term “composition” in the programming sense comes from OO design,
not functional nor mathematical terminology.

-- 
 \          “I don't like country music, but I don't mean to denigrate |
  `\          those who do. And for the people who like country music, |
_o__)                        denigrate means ‘put down’.” —Bob Newhart |
Ben Finney



More information about the Python-list mailing list