Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Deleting objects

Reply
Thread Tools

Deleting objects

 
 
Thomas Philips
Guest
Posts: n/a
 
      04-22-2004
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
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      04-22-2004
(Thomas Philips) writes:

[snip]

> 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.


Err, __del__ isn't a destructor, it's a finalizer. It's called by the
Python runtime when it has determined that the object is garbage,
which is usually immediately after the last reference to it is
dropped.

Cheers,
mwh

--
Good? Bad? Strap him into the IETF-approved witch-dunking
apparatus immediately! -- NTK now, 21/07/2000
 
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
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
RMI deleting remote objects and garbage collection jimjim Java 0 02-27-2004 10:45 AM
deleting list objects dasod C++ 2 07-04-2003 06:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57