Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Deleting objects - my earler post got garbled (http://www.velocityreviews.com/forums/t330176-deleting-objects-my-earler-post-got-garbled.html)

Thomas Philips 04-22-2004 08:09 PM

Deleting objects - my earler post got garbled
 
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

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= 04-22-2004 08:51 PM

Re: Deleting objects - my earler post got garbled
 
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


John J. Lee 04-22-2004 09:51 PM

Re: Deleting objects - my earler post got garbled
 
tkpmep@hotmail.com (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

Terry Reedy 04-22-2004 11:54 PM

Re: Deleting objects - my earler post got garbled
 
Your original post may have been garbled, but our answers were not.





Asun Friere 04-23-2004 05:10 AM

Re: Deleting objects - my earler post got garbled
 
"Martin v. L÷wis" <martin@v.loewis.de> wrote in message news:<40883067.4000702@v.loewis.de>...

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


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

Andrew Bennetts 04-23-2004 05:26 AM

Re: Deleting objects - my earler post got garbled
 
On Thu, Apr 22, 2004 at 10:10:14PM -0700, Asun Friere wrote:
> "Martin v. L÷wis" <martin@v.loewis.de> wrote in message news:<40883067.4000702@v.loewis.de>...
>
> > 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.



=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= 04-23-2004 06:02 AM

Re: Deleting objects - my earler post got garbled
 
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



All times are GMT. The time now is 10:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.