Deleting objects

T

Thomas Philips

I'm teaching myself OOP using Michael Dawson's "Python Programming For
The Absolute Beginner" and 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, 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?

Thomas Philips
 
C

Christopher A. Craig

Villain dies and executes its destructor (__del__). The game then
ends. However, 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?

What's happening is that you're calling self.__del__() (which prints a
string, but does not actually delete the object) and at the end of the
program the object is deleted, which calls self.__del__(). I think
you have the causality backwards, object destruction causes
self.__del__() to be run, self.__del__ does not cause object
destruction.

You are, in fact, guaranteed that the object still exists immediately
after a call to self.__del__() because self must still exist for it to
have been there to be called. It's also impossible for an object to
commit suicide like that, when you call method(obj) a new reference is
created to "obj", so destroying that refrence cannot destroy the last
reference (since there must still be one in the calling namespace).

This is why delete in python is

del obj

instead of

del(obj)
 
T

Terry Reedy

Thomas Philips said:
I'm teaching myself OOP using Michael Dawson's "Python Programming For
The Absolute Beginner" and 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

You appear to have use a tab here and spaces below. Never mix, and stick
with spaces for posted code.
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__).

However, the name 'Villain' is still bound to the corresponding object in
globals(). Executing the __del__ method of an object does *NOT* delete the
object. This *only* thing special about that method is that it is called
'behind the scenes' when an objects refcount goes to 0.
The game then
ends. However, when I execute the program in IDLE, IT FINISHES BY
EXECUTING THE DESTRUCTOR FOR BOTH HERO AND VILLAIN.

__del__ methods are not destructors. There are merely called be the
interpreter's internal destructor function.
How can this be? As one of the two objects was destroyed prior to the
end of the game,

As explained above, it was not destroyed. You merely called a method that
happened to be named __del__.

Getting rid of *all* references to an object, in order to allow destruction
(but not guarantee it), can be difficult.
how can it be re-destroyed when the program ends?

Terry J. Reedy
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top