Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15561

Re: The python implementation of the "relationships between classes".

Date 2011-11-10 12:31 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: The python implementation of the "relationships between classes".
References (3 earlier) <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> <CAMuTYXj+FOy8tgGAkt-iOUq4O8Ph5-W2OHd=4QSE1OWstu0U0Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2623.1320958355.27778.python-list@python.org> (permalink)

Show all headers | View raw


Benjamin Kaplan wrote:
> 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.

Having said all that, you could do something like:

class BodyPart(object):
     _alive = True
     def __nonzero__(self):
         return self._alive
     def die(self):
         self._alive = False

class Head(BodyPart):
     "will contain things like Brain, Eyes, etc"
     size = 5

class Body(BodyPart):
     def __init__(self):
         self._head = Head()
     def go_to_heaven(self):
         self._head.die()
         self.die()

John_Doe = Body()

if John_Doe:
     print "John Doe is alive!"
John_Doe.go_to_heaven()
if John_Doe:
     print "uh oh - something wrong"
else:
     print "John Doe is no longer with us"
     print "and his head is %s" % ('alive' if John_Doe._head else 'dead')

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: The python implementation of the "relationships between classes". Ethan Furman <ethan@stoneleaf.us> - 2011-11-10 12:31 -0800

csiph-web