Composition instead of inheritance

John Nagle nagle at animats.com
Fri Apr 29 07:10:06 CEST 2011


On 4/28/2011 3:35 PM, 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.  Often used to model a part/whole relationship, hence
> the name.

     The distinction isn't that strong in Python, where an object
can't be part of another object.  Object fields are references, not
the objects themselves.

     In C++, you really can have an object as a field of an enclosing
object.  The inner object's destructor will be called first.
Destructors and ownership tend not to be too important in Python,
because storage management is automatic.
					
				John Nagle



More information about the Python-list mailing list