Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!amsnews11.chello.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'mess': 0.07; 'unexpected': 0.07; 'python': 0.08; '(there': 0.09; 'deletes': 0.09; 'foo': 0.09; 'nameerror:': 0.09; 'weak': 0.09; 'subject:python': 0.11; 'c++': 0.12; 'def': 0.14; 'die,': 0.16; 'hat:': 0.16; 'head,': 0.16; 'inaccessible': 0.16; 'it).': 0.16; 'java.': 0.16; 'subject:between': 0.16; 'subject:classes': 0.16; 'received:74.125.82.44': 0.17; 'received:mail-ww0-f44.google.com': 0.17; 'language': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'issue,': 0.18; 'otherwise,': 0.19; 'defined': 0.19; '(most': 0.21; 'maybe': 0.21; 'body.': 0.23; 'header:In-Reply-To:1': 0.23; "python's": 0.24; 'traceback': 0.24; 'code': 0.25; 'needed,': 0.28; 'message- id:@mail.gmail.com': 0.29; 'asking': 0.29; 'class': 0.29; 'definition': 0.30; 'object.': 0.30; 'one)': 0.30; 'example': 0.30; '\xa0\xa0\xa0': 0.30; 'nov': 0.31; 'pm,': 0.31; 'thu,': 0.32; 'does': 0.32; 'to:addr:python-list': 0.32; 'there': 0.33; 'points': 0.33; 'force': 0.34; 'probably': 0.34; 'body,': 0.34; 'describing': 0.34; 'last):': 0.34; 'smart': 0.34; 'object': 0.35; 'unless': 0.35; 'file': 0.36; '...': 0.36; 'example,': 0.36; 'received:74.125.82': 0.37; 'but': 0.37; 'two': 0.37; 'references': 0.38; 'received:google.com': 0.38; 'describe': 0.38; 'word,': 0.38; 'received:74.125': 0.39; 'should': 0.39; 'why': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'extremely': 0.40; 'one,': 0.40; 'skip:_ 10': 0.40; 'delete': 0.40; 'did': 0.40; 'more': 0.60; 'body': 0.61; '2011': 0.62; 'life': 0.63; 'our': 0.63; 'cause': 0.66; 'kinds': 0.70; 'subject:The': 0.72; 'circle': 0.73; 'dead': 0.77; "'foo'": 0.84; 'isolate': 0.84; 'reachable': 0.84; 'uml': 0.84; 'zhang': 0.84; 'body:': 0.91 MIME-Version: 1.0 In-Reply-To: References: <1320941348.7601.57.camel@tim-laptop> Date: Thu, 10 Nov 2011 14:32:24 -0500 Subject: Re: The python implementation of the "relationships between classes". From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Whitelist: YES (by domain whitelist at mpv1.tis.cwru.edu) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1320953615 news.xs4all.nl 6984 [2001:888:2000:d::a6]:56587 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15555 On Thu, Nov 10, 2011 at 1:06 PM, Jerry Zhang wro= te: > > > I just did an example code to describe what i am looking for. > /*-----------------------------------------------------------------------= -------------------------*/ > # ... > > class Head: > =A0=A0=A0 def __init__(self): > =A0=A0=A0=A0=A0=A0=A0 self.size =3D 5 > > class Hat: > =A0=A0=A0 def __init__(self): > =A0=A0=A0=A0=A0=A0=A0 self.color =3D red > =A0=A0=A0 def took_on(self, body): > =A0=A0=A0=A0=A0=A0=A0 self.body =3D body > =A0=A0=A0 def took_off(self, body): > =A0=A0=A0=A0=A0=A0=A0 del self.body > > class Body: > =A0=A0=A0 def __init__(self): > =A0=A0=A0=A0=A0=A0=A0 self.head =3D Head() > =A0=A0=A0 def take_on_hat(self, hat): > =A0=A0=A0=A0=A0=A0=A0 self.hat =3D hat > =A0=A0=A0=A0=A0=A0=A0 hat.take_on(self) > =A0=A0=A0 def take_off_hat(self): > =A0=A0=A0=A0=A0=A0=A0 hat.take_off(self) > =A0=A0=A0=A0=A0=A0=A0 del self.hat > =A0=A0=A0 def go_to_heaven(self): > =A0=A0=A0=A0=A0=A0=A0 take_off_hat(self) > =A0=A0=A0=A0=A0=A0=A0 del self.head > /*-----------------------------------------------------------------------= -----------------------------------*/ > > In this example, Head and body are COMPOSITION, which means > a. A head only live with one body, it can not live with other body. It ca= n > not even live without body > b. If the body go to die, the head also go to die. > > Body and Hat are aggregation, which means > a. A hat live isolate with body, its life circle is isolate > b. A hat only live with one body at a specific time, it can not live with > two body(association would be more than one) > > So when the body die, the clean dead should include take_off Hat and del > Head, otherwise, the code definition is not prciselly describing the > relationship, which may cause a mess system, for example, a body has dead= , > but one hat is still associated with a unreferenced body. > A point on this issue, maybe python is smart that the take_off_hat(self) = is > not needed in go_to_heaven() method(i am not sure yet), but the del > self.head is sure needed, otherwise, we will have a no_body_head in our > ZODB, that is extremely horrible, right? > > All of these points one, the four kinds of class relationship in UML > precisely describe the real word, if the implementation is not precisely, > you will have unexpected results. > Of course, python may be smart to achieve such goal with less effort, tha= t > is why i am asking for a code example for all of the four relationships. You're still misunderstanding Python's object model. del does NOT delete an object. It deletes a name. The only way for an object to be deleted is for it to be inaccessible (there are no references to it, or there are no reachable references to it). >>> foo =3D object() >>> bar =3D foo >>> foo >>> bar >>> del foo >>> bar >>> foo Traceback (most recent call last): File "", line 1, in foo NameError: name 'foo' is not defined There is no way to force the go_to_heaven method to delete the head unless you can make sure that all other references to the head are weak references. If you need strictly-enforced relationships, Python is probably not the right language for the job- you'll be better off with C++ or Java.