Deleting objects - my earler post got garbled

T

Thomas Philips

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?=

Thomas said:
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
 
J

John J. Lee

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
 
A

Andrew Bennetts

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?=

Asun said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top