Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15555
| 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 | <bsk16@case.edu> |
| 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 | <CANVwdDrZD0Ay7y93uP1r9u+nYS6p==kXq9XZ3m5VHP6w5g1Y+Q@mail.gmail.com> |
| References | <CANVwdDr-6x8bt4iNL+Q=jZ7+edk7XbR2K_LeJC3Z60qy=vVFQA@mail.gmail.com> <CAPTjJmoOBTiRE8vUNz_xQcwDZA6gazG1fAMkFfhPMomtAKtp6w@mail.gmail.com> <CANVwdDr6qBueBGXykLrhH+MLiwyozD_U1+YRBnrWNfge0=uvDQ@mail.gmail.com> <CAPTjJmqkH6WzbEXWLf6T9yyJyfkH-yCFk1HWd_Vrqwi7iz3FrQ@mail.gmail.com> <CANVwdDqYt+YiJ-hz1F7r+HWnQvFRaJzHvESGoiJu5TKdKE5W7Q@mail.gmail.com> <1320941348.7601.57.camel@tim-laptop> <CANVwdDrZD0Ay7y93uP1r9u+nYS6p==kXq9XZ3m5VHP6w5g1Y+Q@mail.gmail.com> |
| Date | Thu, 10 Nov 2011 14:32:24 -0500 |
| Subject | Re: The python implementation of the "relationships between classes". |
| From | Benjamin Kaplan <benjamin.kaplan@case.edu> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2613.1320953615.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Thu, Nov 10, 2011 at 1:06 PM, Jerry Zhang <jerry.scofield@gmail.com> wrote:
>
>
> I just did an example code to describe what i am looking for.
> /*------------------------------------------------------------------------------------------------*/
> # ...
>
> class Head:
> def __init__(self):
> self.size = 5
>
> class Hat:
> def __init__(self):
> self.color = red
> def took_on(self, body):
> self.body = body
> def took_off(self, body):
> del self.body
>
> class Body:
> def __init__(self):
> self.head = Head()
> def take_on_hat(self, hat):
> self.hat = hat
> hat.take_on(self)
> def take_off_hat(self):
> hat.take_off(self)
> del self.hat
> def go_to_heaven(self):
> take_off_hat(self)
> 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 can
> 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, that
> 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 = object()
>>> bar = foo
>>> foo
<object object at 0x01CE14C0>
>>> bar
<object object at 0x01CE14C0>
>>> del foo
>>> bar
<object object at 0x01CE14C0>
>>> foo
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: The python implementation of the "relationships between classes". Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-11-10 14:32 -0500
csiph-web