[Tutor] special call for deleted objects?

Terry Carroll carroll at tjc.com
Fri Jan 30 01:15:21 EST 2004


On Fri, 30 Jan 2004, Thomas Clive Richards wrote:

> replace them with None. However, these instances should do some cleanup 
> before they dissapear. I *could* create a cleanup method, and call it 
> specifically every time I want to delete the instance, and then replace it 
> with None, but I thought there might be a special method that automatically 
> gets called when an object is deleted.

That's __del__, but I don't think you have much control over when the
object gets deleted.  When you del an object, its reference count gets
decremented, and when it hits zero, it's eligible to be deleted by garbage
collection, whenever garbage collection occurs.  *That's* when __del__ 
will get called on that object.

In fact "It is not guaranteed that __del__() methods are called for 
objects that still exist when the interpreter exits."

If you want a specific cleanup to be coded, I'd make it explicit.
  




More information about the Tutor mailing list