Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Deleting objects - my earler post got garbled

Reply
Thread Tools

Deleting objects - my earler post got garbled

 
 
Thomas Philips
Guest
Posts: n/a
 
      04-22-2004
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
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      04-22-2004
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

 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      04-22-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      04-22-2004
Your original post may have been garbled, but our answers were not.




 
Reply With Quote
 
Asun Friere
Guest
Posts: n/a
 
      04-23-2004
"Martin v. L÷wis" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

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


So what does "del <object>" actually do then?
 
Reply With Quote
 
Andrew Bennetts
Guest
Posts: n/a
 
      04-23-2004
On Thu, Apr 22, 2004 at 10:10:14PM -0700, Asun Friere wrote:
> "Martin v. L÷wis" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>
> > 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.


 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      04-23-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mod_python.so is garbled mod_python.so is garbled blbmdsmith Python 1 12-14-2006 12:15 AM
Garbled Display after SP2 Installed - Please help!! =?Utf-8?B?VGhlYQ==?= Microsoft Certification 3 05-02-2005 04:07 PM
cookie value being garbled. Peter Row ASP .Net 0 05-24-2004 08:38 AM
garbled JFrame patrick Java 0 12-05-2003 08:32 AM
Garbled appearance of .NET app on Presario 700 Steve Covert ASP .Net 2 11-11-2003 10:14 PM



Advertisments