Extending a little on this question. Removing an object from a Python list is easy enough, but I'm going to have a "busy" list -- frequent removals and frequent additions -- so I want to guard against memory leaks.
Will just removing the object from the list destroy the object? (I'm assuming No.)
If my assumption is correct, then how can I remove it from the list and destroy it at the same time?
If it matters, the objects in question are instances of a class that I wrote. I plan on creating the objects "live" each time I need to insert a new one, e.g.
myList.insert(index,myClass(constructorParm1,constructorParm2))
So when I later need to delete some of the instances created that way, I want to be sure they're not just gone from the list, but gone period.
In cPython, objects are automatically destroyed when their reference count drops to 0.
If you delete a entry from your list, the reference count for the object in question is decremented. You do not need to do anything special to have it destroyed, nor should you try to. If any other reference to the object still exists, then destroying the object would break the other reference.
If you want to hold references to an object that do not stop the object from being garbage collected, use a weakref
reference instead.