<br><br><div class="gmail_quote">2011/11/11 Benjamin Kaplan <span dir="ltr"><<a href="mailto:benjamin.kaplan@case.edu">benjamin.kaplan@case.edu</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="HOEnZb"><div class="h5">On Thu, Nov 10, 2011 at 1:06 PM, Jerry Zhang <<a href="mailto:jerry.scofield@gmail.com">jerry.scofield@gmail.com</a>> wrote:<br>
><br>
><br>
> I just did an example code to describe what i am looking for.<br>
> /*------------------------------------------------------------------------------------------------*/<br>
> # ...<br>
><br>
> class Head:<br>
> def __init__(self):<br>
> self.size = 5<br>
><br>
> class Hat:<br>
> def __init__(self):<br>
> self.color = red<br>
> def took_on(self, body):<br>
> self.body = body<br>
> def took_off(self, body):<br>
> del self.body<br>
><br>
> class Body:<br>
> def __init__(self):<br>
> self.head = Head()<br>
> def take_on_hat(self, hat):<br>
> self.hat = hat<br>
> hat.take_on(self)<br>
> def take_off_hat(self):<br>
> hat.take_off(self)<br>
> del self.hat<br>
> def go_to_heaven(self):<br>
> take_off_hat(self)<br>
> del self.head<br>
> /*----------------------------------------------------------------------------------------------------------*/<br>
><br>
> In this example, Head and body are COMPOSITION, which means<br>
> a. A head only live with one body, it can not live with other body. It can<br>
> not even live without body<br>
> b. If the body go to die, the head also go to die.<br>
><br>
> Body and Hat are aggregation, which means<br>
> a. A hat live isolate with body, its life circle is isolate<br>
> b. A hat only live with one body at a specific time, it can not live with<br>
> two body(association would be more than one)<br>
><br>
> So when the body die, the clean dead should include take_off Hat and del<br>
> Head, otherwise, the code definition is not prciselly describing the<br>
> relationship, which may cause a mess system, for example, a body has dead,<br>
> but one hat is still associated with a unreferenced body.<br>
> A point on this issue, maybe python is smart that the take_off_hat(self) is<br>
> not needed in go_to_heaven() method(i am not sure yet), but the del<br>
> self.head is sure needed, otherwise, we will have a no_body_head in our<br>
> ZODB, that is extremely horrible, right?<br>
><br>
> All of these points one, the four kinds of class relationship in UML<br>
> precisely describe the real word, if the implementation is not precisely,<br>
> you will have unexpected results.<br>
> Of course, python may be smart to achieve such goal with less effort, that<br>
> is why i am asking for a code example for all of the four relationships.<br>
<br>
</div></div>You're still misunderstanding Python's object model. del does NOT<br>
delete an object. It deletes a name. The only way for an object to be<br>
deleted is for it to be inaccessible (there are no references to it,<br>
or there are no reachable references to it).<br>
>>> foo = object()<br>
>>> bar = foo<br>
>>> foo<br>
<object object at 0x01CE14C0><br>
>>> bar<br>
<object object at 0x01CE14C0><br>
>>> del foo<br>
>>> bar<br>
<object object at 0x01CE14C0><br>
>>> foo<br>
<br>
Traceback (most recent call last):<br>
File "<pyshell#7>", line 1, in <module><br>
foo<br>
NameError: name 'foo' is not defined<br>
<br>
<br>
There is no way to force the go_to_heaven method to delete the head<br>
unless you can make sure that all other references to the head are<br>
weak references. If you need strictly-enforced relationships, Python<br>
is probably not the right language for the job- you'll be better off<br>
with C++ or Java.<br></blockquote><div><br></div><div>Thanks for your reply, but i know your point on this before post this issue. "doing the the job of python's garbage collecting" system is not i want.</div>
<div>What i am trying to do is a python implementation of the real "composition implementation".</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<span class="HOEnZb"><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></span></blockquote></div><br>