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

Jerry Zhang jerry.scofield at gmail.com
Thu Nov 10 15:12:50 CET 2011

Thanks for your reply.

2011/11/10 Steven D'Aprano <steve at pearwood.info>

> 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.

Yeah, but these kinds of relationship still exists in real word whatever
UML is.

For example, the relationship between Cls_arm and Cls_body could be
composition, one arm instance only live with one body instance, and once
this body die, this arm also die. All of this should be described by python
if your application need.

>  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.

Yeah, python objects garbage may be another story. Think about you have a
ZODB running which stores your objects data, you will care their lifecycle.
 maybe because i did not make myself understood yet.

Here is a more detail example question.
I have a classes sets described by UML, which could be refered as Cls_A,
Cls_B, CLs_C, Cls_D, Cls_E. There are four relationship between
them--dependency, association, aggregation, composition. What i need to do
is define all of the classes by python, and,of course, the class definition
should describe there relationship correctly.
For example, implement composition relationship between Cls_A and Cls_B,
which may means a instance of Cls_B is created and destroyed by a Cls_A
instance, My code may be like this(not sure since i am not quite familiar
with python yet):

    def __init__(self):
        self.at1 = 1

    def __init__(self):
        self.com1 = Cls_a()
    def __del__(self):
        del self.com1
Is it a right implementation for composition?

and i need also do other relationship definition into class, like
dependency, association, aggregation...

Is there any article or code example on the relationships implementation?

> 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.

I am trying.

> --
> Steven
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111110/3e1aa8ab/attachment-0001.html>

More information about the Tutor mailing list