deleting objects present in a list

Chris Rebert clp2 at rebertia.com
Tue Apr 20 21:44:37 CEST 2010


On Tue, Apr 20, 2010 at 12:21 PM, Sandy <dksreddy at gmail.com> wrote:
> Hi all,
> I have large number of objects created and to handle them properly, I
> store them in a list. How can I delete all of these objects (delete I
> mean here is to remove the object from memory not just from list)? I
> cannot use the list to iterate through the objects to delete them.
> Because 'del' only reduces the reference count and as it is present in
> the list it is not deleted. I cannot delete the list because I loose
> control over the objects.

And what exactly is supposed to happen to any other references to the
objects besides the references in the list?

If there are no such references, then deleting the objects from the
list will indeed delete them "for real" (although /exactly/ when the
unreferenced objects will be garbage-collected is
implementation-dependent; in CPython, it will be right then-and-there
due to its use of refcounting).

You might want to look at using weak references
(http://docs.python.org/library/weakref.html) for all references to
the objects other than the references in the list.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list