473,378 Members | 1,360 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,378 software developers and data experts.

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 5804

<us**@domain.invalid> wrote in message
news:c5******************************@news.teranew s.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.invalid 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.invalid 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
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...
6
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...
181
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 ...
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. ...
4
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
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
62
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();...
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...
5
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.