473,666 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,n ame):
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,enem y,energy):
enemy.hit(energ y)

def hit(self,energy ):
self.strength -= energy
if(self.strengt h <= 50):
self.__del__()

I instantiate one instance of each class:
Hero = Player("Me")
Villain = Alien("Not Me")

If Hero hits Villain with
Hero.blast(Vill ain, 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(H ero,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 2032
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.strengt h <= 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(H ero,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,n ame):
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,enem y,energy):
enemy.hit(energ y)

def hit(self,energy ):
self.strength -= energy
if(self.strengt h <= 50):
self.__del__()

I instantiate one instance of each class:
Hero = Player("Me")
Villain = Alien("Not Me")

If Hero hits Villain with
Hero.blast(Vill ain, 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.loewi s.de> wrote in message news:<40******* *******@v.loewi s.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.loewi s.de> wrote in message news:<40******* *******@v.loewi s.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
2137
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, i.e. my html page shows gibberish or I get a server error altogether, which is traced from log file to garbled html code. Has anyone seen this? I wanted to fix this, before I do a complete reinstall of php.
4
497
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 for a while, the db objects heads a tree of lots of data.
6
2429
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 http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&threadm=AXqqc.89218%24PJ1.865449%40wagner.videotron.net&rnum=1&prev=/groups%3Fq%3Dmatan%2Bnassau%26hl%3Den%26lr%3D%26ie%3DUTF-8%26sa%3DG%26scoring%3Dd ) VariableExp *x = new VariableExp("X"); VariableExp *y = new VariableExp("Y");...
9
1881
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. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather than references for the STL library? I'd also prefer to avoid using const_cast, if it is indeed...
6
1313
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 VB.NET, if it is even possible of course?!? Anyway, if anyone knows a way of doing this I would be most appreciative of some advice, thanks yet again in advance! Nick.
51
10371
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
9
1724
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 specified in the following format too: There is a DataView object that I want to prohibit deleting of some of its DataRowView objects. that is: when drv.Delete() is called (drv is the
1
3182
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 contains a form, multiple queries and multiple reports. The purpose of the form is to allow the user to run various queries against my company's Sybase server and display a report. Since the queries return ADO recordsets from the Sybase server,...
0
996
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 from memory when all references to that object have | been deleted. Is this so? A Python interpreter *may* delete an object when, but only when, it becomes inaccessible from the currently running program. What interpreters do depends on the...
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.