[Tutor] Replacement for __del__

wesley chun wescpy at gmail.com
Tue May 19 01:45:45 CEST 2009


> I've been told numerous times that using __del__ is not the way to handle
> clean-up because it is finicky.

matthew,

welcome to Python and this Tutor mailing list!

and yes, i'll echo everyone else in saqying that using __del__ should
be avoided. the most critical issues regarding this is because there
are object references everywhere, and it's not prdictable when they
get to zero and GC'd, meaning you don't if/when it will execute.

also, during program shutdown, as objects are being deallocated during
termination, some of the objects in your __del__ may have already been
released so exceptions may occur. to get around this exceptions
occurring within __del__ are *ignored*.

more here:
http://docs.python.org/reference/datamodel.html#customization


> Fine. However, how should I handle the
> following problem:
> A folder is created during object instantiation. This is necessary because
> multiple other methods depend on the existence of that folder, and in the
> future things may be running in parallel so it has to be there for the
> entire life of the object. Before the object is deleted, I want that temp
> folder to be deleted (ala shutil.rmtree). Is there a better way to do this?

is your program threaded? if not, you can delete the filetree at the
end of your application, right before it ends. if it is threaded, you
may consider join()ing them all before deleting.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list