473,699 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting objects

Say I have an object (foo), that contains an
array (bar) of references to other objects.

Now I want to puff some of the objects from the
array so that I remove the array element, and
destroy the oject.

but when I do:

del foo.bar[0]

Python says:
object doesn't support item deletion

So do I need to define __del__? And what would I
put there?

What if I wanted to remove the array element
but still have the object exist?

What happens if I succeed in destroying an object
that other objects still think they are referencing?

Thanks,

Toby

Jul 18 '05 #1
3 5834

<us**@domain.in valid> wrote in message
news:c5******** *************** *******@news.te ranews.com...
Say I have an object (foo), that contains an
array (bar) of references to other objects.

Now I want to puff some of the objects from the
array so that I remove the array element, and
destroy the oject.

but when I do:

del foo.bar[0]

Python says:
object doesn't support item deletion


What is the actual type of foo.bar? >>>type(foo.bar ) # prints what?

tjr
Jul 18 '05 #2
On Wed, 2004-01-14 at 17:23, us**@domain.inv alid wrote:
Say I have an object (foo), that contains an
array (bar) of references to other objects.

Now I want to puff some of the objects from the
array so that I remove the array element, and
destroy the oject. but when I do:

del foo.bar[0]

Python says:
object doesn't support item deletion


It'd be helpful if you supplied the code in question. Then again, we
wouldn't be able to let our imagination wander with what puff might
mean. <wink>

Anyway, is this the sort of thing you're talking about?

#!/usr/bin/env python

class Foo:

def __init__(self):
self.bar = []

def __str__(self):
return '<Foo><bar>%s</bar></Foo>' % (str(self.bar), )

l = range(10)

foo = Foo()
foo.bar.append( l)
del foo.bar[0]
print 'foo = %s' % (str(foo),)
print 'l = %s' % (l,)

Cheers,

// m
Jul 18 '05 #3
us**@domain.inv alid wrote:
Say I have an object (foo), that contains an
array (bar) of references to other objects.

Now I want to puff some of the objects from the
array so that I remove the array element, and
destroy the oject.
You don't need to jump through hoops for this. All you need to do is:

del foo

If there are no other references to the object that was called foo
above, then (and only then) it will be destroyed.

Python keeps a reference count for each object. When the count hits 0
(no references pointing to object) it is destroyed.

Because of this, attributes of foo (eg foo.bar) will automatically be
destroyed when foo is destroyed (assuming they don't have references
from elsewhere).

but when I do:

del foo.bar[0]

Python says:
object doesn't support item deletion

So do I need to define __del__? And what would I
put there?
What is the type of foo.bar?

What if I wanted to remove the array element
but still have the object exist?
del foo.bar

This will remove the reference foo.bar.
What happens if I succeed in destroying an object
that other objects still think they are referencing?


Try as you might, shooting yourself in the foot is pretty hard in Python
(see above :)

--
Shalabh

Jul 18 '05 #4

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

Similar topics

2
519
by: Thomas Philips | last post by:
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
6
2034
by: Thomas Philips | last post by:
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):
181
8804
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 He says he's going to dispose of map, filter, reduce and lambda. He's going to give us product, any and all, though, which is nice of him.
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
1883
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...
4
14020
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
51
10410
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
62
17789
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image(); img.src = ; Then I use the image a few more times by adding it into an Array object:
0
999
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...
5
505
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store pointers of type structure "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new" and push back into the vector,this is done inside a for loop for
0
8689
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
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
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
5875
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
4376
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...
1
3058
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

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.