<br><br><div class="gmail_quote">2011/11/10 Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Fri, Nov 11, 2011 at 12:58 AM, Jerry Zhang <<a href="mailto:jerry.scofield@gmail.com">jerry.scofield@gmail.com</a>> wrote:<br>
> Cls_a:<br>
>     def __init__(self):<br>
>         self.at1 = 1<br>
> Cls_b:<br>
>     def __init__(self):<br>
>         self.com1 = Cls_a()<br>
>     def __del__(self):<br>
>         del self.com1<br>
> Is it a right implementation for composition?<br>
<br>
</div>Yes, except that you don't need to explicitly del it. Python (at<br>
least, CPython) is reference-counted; your Cls_b object owns a<br>
reference to the Cls_a object, so (assuming nothing else has a<br>
reference) that Cls_a will be happily cleaned up when the Cls_b is.<br>
<br>
Python doesn't really talk about "composition" etc. It's much simpler:<br>
everything's an object, and you have references to that object. A<br>
named variable is a reference to some object. A member on an object<br>
is, too. Whenever an object is expired, all objects that it references<br>
lose one reference, and if that was the sole reference, those objects<br>
get expired too. It's a change of thinking, perhaps, but not a<br>
difficult one in my opinion; and it's so easy to work with when you<br>
grok it.<br></blockquote><div><br></div><div>Unfortunately there is a difference between composition and aggregation in my real word, and my application really care this since it is trying to simulate this real world model, so my system should track this difference <span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; ">accurately, otherwise the system may not work well. </span></div>
<div><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="font-size: 14px; line-height: 21px;"><br></span></font></div><div><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="font-size: 14px; line-height: 21px;">For example, </span></font></div>
<div><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="font-size: 14px; line-height: 21px;">a. the Cls_arm and Cls_body may be composition, but not aggregation. My app must ensure that " one arm instance only live with one body instance, if the body instance die, the arm instance must die.</span></font></div>
<div> b. the Cls_auto and the Cls_tyre may be aggregation. "One tyre still can live even the auto is dead."</div><div><br></div><div>Meanwhile, I have a ZODB running, which stores all the living objects. </div><div>
<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
ChrisA<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>