[Tutor] The python implementation of the "class relationship".

Steven D'Aprano steve at pearwood.info
Thu Nov 10 14:56:07 CET 2011

Jerry Zhang wrote:
> As you know, there are several kinds of relationships between classes in
> the UML -- dependency, association, aggregation, composition.

"As you know"... no, I'm afraid I don't know. Believe it or not, it is 
possible to be an experienced, good programmer and still know nothing 
about Unified Modeling Language.

> Q1. Is there any article or code example on its implementation in python?
> Q2. More specific, in composition model, the container object may be
> responsible for the handling of embedded objects' lifecycle. The common way
> would be adding __del__ to the container class or something else?

If I have guessed correctly what you mean, no, containers are not 
responsible for handling embedded objects' lifecycle in Python. Every 
object is responsible for itself. Python objects are garbage collected 
when there are no longer any references to that object, regardless of 
whether those references are in the form of name bindings, containment 
inside a container or attribute, closures, or any other reference.

This list is for learning about the Python programming language, aimed 
mostly at beginners. You might have better luck getting detailed answers 
on the python-list at python.org mailing list, also available as 
comp.lang.python on Usenet.


More information about the Tutor mailing list