[Tutor] Aggregation vs Composition

Alan Gauld alan.gauld at yahoo.co.uk
Sun Dec 10 04:01:59 EST 2017


On 10/12/17 05:07, jia yue Kee wrote:

> in Dusty Philips's Python 3: Object-Oriented
> Programming book.

Caveat: I've not read this book so can only
guess at what the author might be meaning.

> Based on my reading, what I gathered was that Composition implies a
> relationship where the child cannot exist independent of the parent while
> Aggregation, on the other hand, implies a relationship where the child can
> exist independently of the parent.

Correct. But the key word here is "implies".
In some languages the differences can be directly
implemented in the language but in Python the
relationships are always just implied. The differences
do not really exist. (Unless you go to inordinate
lengths to hide and link the data, which is rarely,
if ever, justified.)

> However, in one of the paragraph of the book, *Dusty mentioned that
> composition is aggregation* 

I'm guessing he means that in Python we implement
both concepts in the same way whether in a collection
or a class (which in Python can be thought of as a
special type of collection). This is because all
values in Python are objects and variables(names)
are simply references to those objects. So every
object in Python must exist independent of its
parent to some degree.

> (refer to the snapshot below, the sentence
> which has been highlighted in yellow). 

This is a text mailing list so graphics get
stripped by the server, sorry. SEnd a URL if necessary.

> I am really confused by this

I think he is talking about the practical
limitations in Python such that pure Composition
does not really exist but is implemented *as a concept*
by aggregation.

But as I said I haven't read the book so am guessing.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list