Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Deleting objects

Reply
Thread Tools

Deleting objects

 
 
user@domain.invalid
Guest
Posts: n/a
 
      01-14-2004
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

 
Reply With Quote
 
 
 
 
Mark McEahern
Guest
Posts: n/a
 
      01-15-2004
On Wed, 2004-01-14 at 17:23, http://www.velocityreviews.com/forums/(E-Mail Removed)lid 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


 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-15-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Shalabh Chaturvedi
Guest
Posts: n/a
 
      01-15-2004
(E-Mail Removed)lid 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
RMI deleting remote objects and garbage collection jimjim Java 0 02-27-2004 10:45 AM
deleting list objects dasod C++ 2 07-04-2003 06:24 PM



Advertisments