[Tutor] ensuring a __del__ method happens

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 27 Mar 2001 11:42:16 -0800 (PST)


On Tue, 27 Mar 2001, Sean 'Shaleh' Perry wrote:

> > It sounds like you'll want to clean up Foo, no matter what, when your
> > program finishes.  If so, you'll want to look at the atexit module:
> > 
> >     http://python.org/doc/current/lib/module-atexit.html
> > 
> > I have to admit: I've never touched this beast before, but it sounds
> > exactly like what you want.
> > 
> 
> can I back port this to 1.5?  This code is not 2.x dependant.

Not quite sure.  You can get equivalent behavior from the
sys.exitfunc() function:

    http://python.org/doc/current/lib/module-sys.html

So we could do something like this:

###
>>> import sys
>>> def goodbye(): print "Goodbye, goodbye"
...          
>>> sys.exitfunc = goodbye
>>>                                ## At this point, I press Ctrl-D
Goodbye, goodbye
###

And this should be Python 1.52 compatible.  Hope this helps!