The python implementation of the "relationships between classes".

Jerry Zhang jerry.scofield at gmail.com
Thu Nov 10 08:58:41 EST 2011


Hi Chris,

Firstly thanks for your quick reply.

1. Your code example gives a point, but i may not reach my question yet,
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):

Cls_a:
    def __init__(self):
        self.at1 = 1

Cls_b:
    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?


Thanks!
Biao


2011/11/10 Chris Angelico <rosuav at gmail.com>

> On Fri, Nov 11, 2011 at 12:15 AM, Jerry Zhang <jerry.scofield at gmail.com>
> wrote:
> > For example, in composition model, the container object may be
> responsible
> > for the handling of embedded objects' lifecycle. What is the python
> pattern
> > of such implementation?
>
> Here's an example:
>
> class Test(object):
>  def __init__(self):
>    self.lst=[1,2,3]
>    self.map={"foo":12,"bar":34}
>
> This is a container that has a list and a dictionary in it. When an
> instance of the container is destroyed, its list and dictionary will
> be destroyed too (assuming nobody's made other references to them).
> You don't need to code anything explicitly for that.
>
> Chris Angelico
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111110/370ab9cf/attachment-0001.html>


More information about the Python-list mailing list