[Tutor] ensuring a __del__ method happens

Sean 'Shaleh' Perry shaleh@valinux.com
Tue, 27 Mar 2001 10:10:11 -0800 (PST)


On 27-Mar-2001 Daniel Yoo wrote:
> On Mon, 26 Mar 2001, Sean 'Shaleh' Perry wrote:
> 
>> SO, I have a class Foo which maps to a directory.  When you create
>> Foo, it makes a directory of the form /tmp/foo-pid.  I would like for
>> this dir to be removed when the object leaves scope (i.e. when __del__
>> is called).  However this does not seem to work if the object exists
>> when python exists.  Any ideas?
> 
> It might be better to have a close() method in your Foo that explicitly
> removes the temporary directory.  The semantics would be similar to how
> files close() themselves: they call close() through __del__, but they also
> allow the user to call close() directly to give finer control.
> 

this idea sounded good until I thought it through.  The problem is Foo lives
for most of the program.  If the program terminates for some reason (i.e. a
caught exception) I would like Foo to be cleaned up after.

> If there aren't any more references to your Foo object, it should call
> __del__.  Can you double check to see if something else has a link to your
> Foo?
> 

none that are obvious to me.  Would it matter that Foo is a derived class of
Bar and Bar lacks a __del__ method?