473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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
Jul 18 '05 #1
6 2019
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

Jul 18 '05 #2
tk****@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
Jul 18 '05 #3
Your original post may have been garbled, but our answers were not.


Jul 18 '05 #4
"Martin v. Löwis" <ma****@v.loewis.de> wrote in message news:<40**************@v.loewis.de>...
There is no way to explicitly delete an object in Python.


So what does "del <object>" actually do then?
Jul 18 '05 #5
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

Jul 18 '05 #6
On Thu, Apr 22, 2004 at 10:10:14PM -0700, Asun Friere wrote:
"Martin v. Löwis" <ma****@v.loewis.de> wrote in message news:<40**************@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.
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: steve | last post by:
Hi, My Windows machine running PHP Version 4.3.4, Apache, mysql, Zend IDE has developed a strange problem whereas the html code coming out of php sometimes gets garbled in unpredicatable ways,...
4
by: user | last post by:
Say I have a database object 'db', that contains table objects, that contain field objects so that I can do things like this: print db.table.field.value() Now, after plundering the database...
6
by: Matan Nassau | last post by:
Hello. i have a composite which i want to delete. this is a composite which represents a boolean expression (see a previous post of mine with more details at...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
6
by: Nak | last post by:
Hi there, I was wondering if it was possible to pass objects in windows messages, obviously via pointers I would presume, but I can't quite work out how to turn an object into a pointer in...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
9
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be...
1
by: sphinney | last post by:
All, I'm not sure how to adequately explain my problem in two sentences or less, so at the risk of providing TMI, here's the condensed verion. I have developed an Access 2002 database file that...
0
by: Terry Reedy | last post by:
"Jacob Davis" <j.foster.davis@gmail.comwrote in message news:C673A5C7-9971-4CAA-8CEB-3993C3E93F9C@gmail.com... | I read in some earlier messages that an object in Python is only | removed or freed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.