Deleting objects - my earler post got garbled
John J. Lee
jjl at pobox.com
Thu Apr 22 17:51:33 EDT 2004
tkpmep at 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
More information about the Python-list
mailing list