Deleting objects - my earler post got garbled

Discussion in 'Python' started by Thomas Philips, Apr 22, 2004.

  1. I have a question about deleting objects. My game has two classes,
    Player and Alien, essentially identical, instances of which can shoot
    at each other. Player is described below

    class Player(object):
    #Class attributes for class Player
    n=0 #n is the number of players

    #Private methods for class Player
    def __init__(self,name):
    self.name = name
    self.strength = 100
    Player.n +=1

    def __del__(self):
    Player.n -=1
    print "I guess I lost this battle"

    #Public methods for class Player
    def blast(self,enemy,energy):
    enemy.hit(energy)

    def hit(self,energy):
    self.strength -= energy
    if(self.strength <= 50):
    self.__del__()

    I instantiate one instance of each class:
    Hero = Player("Me")
    Villain = Alien("Not Me")

    If Hero hits Villain with
    Hero.blast(Villain, 100),

    Villain dies and executes its destructor (__del__). The game then
    ends. However,

    1. When I execute the program in IDLE, IT FINISHES BY EXECUTING THE
    DESTRUCTOR FOR BOTH HERO AND VILLAIN. How can this be? As one of the
    two objects was destroyed prior to the end of the game, how can it be
    re-destroyed when the program ends?

    2. After Hero hits Villain with an energy of 100, Villain executes its
    destructor, but I find I can then type
    Villain.blast(Hero,100)
    and have the alien (whose demise had me gloating) blast me right back!
    Why is it that the blast() method works for an object whose destructor
    has been executed?

    Thomas Philips
    Post a follow-up to this message
     
    Thomas Philips, Apr 22, 2004
    #1
    1. Advertising

  2. Thomas Philips wrote:
    > I have a question about deleting objects. My game has two classes,
    > Player and Alien, essentially identical, instances of which can shoot
    > at each other. Player is described below


    There are a few problems with your posting. First of all, you don't
    show the code of Alien.

    > def hit(self,energy):
    > self.strength -= energy
    > if(self.strength <= 50):
    > self.__del__()


    Then, you are invoking __del__ explicitly. This is probably not
    what you want to do. Invoking __del__ does *not* cause the object
    to be deleted.

    I repeat. Invoking __del__ does *not* cause the object to be
    deleted.

    Instead, it is vice versa: Deleting the object causes __del__
    to be invoked. The object is deleted when the last reference
    to the object goes away, *not* when __del__ is invoked.

    So in your example, you cause multiple calls to __del__.
    This is probably not what you want to do.

    There is no way to explicitly delete an object in Python.

    > 1. When I execute the program in IDLE, IT FINISHES BY EXECUTING THE
    > DESTRUCTOR FOR BOTH HERO AND VILLAIN. How can this be? As one of the
    > two objects was destroyed prior to the end of the game, how can it be
    > re-destroyed when the program ends?


    This is hard to tell, because you don't show the code of Alien.

    > 2. After Hero hits Villain with an energy of 100, Villain executes its
    > destructor, but I find I can then type
    > Villain.blast(Hero,100)
    > and have the alien (whose demise had me gloating) blast me right back!
    > Why is it that the blast() method works for an object whose destructor
    > has been executed?


    Because invoking __del__ does not cause the object to go away. There
    is no way to explicitly delete an object in Python.

    Regards,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Apr 22, 2004
    #2
    1. Advertising

  3. Thomas Philips

    John J. Lee Guest

    (Thomas Philips) writes:

    > I have a question about deleting objects. My game has two classes,
    > Player and Alien, essentially identical, instances of which can shoot
    > at each other. Player is described below
    >
    > class Player(object):
    > #Class attributes for class Player
    > n=0 #n is the number of players
    >
    > #Private methods for class Player
    > def __init__(self,name):
    > self.name = name
    > self.strength = 100
    > Player.n +=1
    >
    > def __del__(self):
    > Player.n -=1
    > print "I guess I lost this battle"
    >
    > #Public methods for class Player
    > def blast(self,enemy,energy):
    > enemy.hit(energy)
    >
    > def hit(self,energy):
    > self.strength -= energy
    > if(self.strength <= 50):
    > self.__del__()
    >
    > I instantiate one instance of each class:
    > Hero = Player("Me")
    > Villain = Alien("Not Me")
    >
    > If Hero hits Villain with
    > Hero.blast(Villain, 100),
    >
    > Villain dies and executes its destructor (__del__). The game then
    > ends. However,

    [...questions, questions, questions...]

    Don't trouble yourself with these questions Thomas, you really don't
    want to know.

    Just Don't Do That: simply represent your character's death by some
    means other than destroying your Hero. __del__ methods are to be
    avoided unless you really can't see another way. And as for *calling*
    __del__ explicitly... well, I think my laziness in not liking to think
    about what happens then is a virtue :)

    thank-Guido-this-isn't-C++-ly y'rs,


    John
     
    John J. Lee, Apr 22, 2004
    #3
  4. Thomas Philips

    Terry Reedy Guest

    Your original post may have been garbled, but our answers were not.
     
    Terry Reedy, Apr 23, 2004
    #4
  5. Thomas Philips

    Asun Friere Guest

    "Martin v. Löwis" <> wrote in message news:<>...

    > There is no way to explicitly delete an object in Python.


    So what does "del <object>" actually do then?
     
    Asun Friere, Apr 23, 2004
    #5
  6. On Thu, Apr 22, 2004 at 10:10:14PM -0700, Asun Friere wrote:
    > "Martin v. Löwis" <> wrote in message news:<>...
    >
    > > There is no way to explicitly delete an object in Python.

    >
    > So what does "del <object>" actually do then?


    As http://docs.python.org/ref/del.html says:

    ...

    Deletion of a name removes the binding of that name from the local or
    global namespace, depending on whether the name occurs in a global
    statement in the same code block. If the name is unbound, a NameError
    exception will be raised.

    It is illegal to delete a name from the local namespace if it occurs as
    a free variable in a nested block.

    Deletion of attribute references, subscriptions and slicings is passed
    to the primary object involved; deletion of a slicing is in general
    equivalent to assignment of an empty slice of the right type (but even
    this is determined by the sliced object).

    Essentially, the del statement deletes a reference to an object. There may
    be other references to the object, and even if there aren't the garbage
    collector might not collect the object immediately, if at all.

    -Andrew.
     
    Andrew Bennetts, Apr 23, 2004
    #6
  7. Asun Friere wrote:
    >>There is no way to explicitly delete an object in Python.

    >
    >
    > So what does "del <object>" actually do then?


    The syntax is not "del <object>", but "del <variable>".

    It unbinds <variable> so that <variable> does no longer
    refer to the object it used to refer to, very similar to
    saying

    <variable> = None

    Whether that causes deletion of the object depends on
    whether there are other reference to the same object,
    in different variables.

    Regards,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Apr 23, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Covert
    Replies:
    2
    Views:
    318
    Steve Covert
    Nov 11, 2003
  2. Peter Row

    cookie value being garbled.

    Peter Row, May 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    476
    Peter Row
    May 24, 2004
  3. patrick

    garbled JFrame

    patrick, Dec 5, 2003, in forum: Java
    Replies:
    0
    Views:
    471
    patrick
    Dec 5, 2003
  4. Nishi Bhonsle
    Replies:
    2
    Views:
    487
    William Brogden
    Sep 1, 2004
  5. blbmdsmith
    Replies:
    1
    Views:
    682
    Graham Dumpleton
    Dec 14, 2006
Loading...

Share This Page