Advice needed on __del__

flupke flupke at nonexistingdomain.com
Tue May 10 08:33:39 EDT 2005


phil wrote:
<snip>

> A class instance based language doesn't have to BE C++ to have a
> destructor method.
> 
> All I wanted to know is: is the apparent treatment of __del__
> which SEEMS to work fine for me in 2.3.4 deprecated or mangled
> in some later release or future plans.
> If someone doesn't know the answer to this question or if noone
> knows the answer to this question, I will still love and
> respect the Python Community.  In large part cause it ain't C++>

I like to use destructors to and find the way __del__ works a bit 
strange. For instance if you want to use a class variable in the __del__ 
function it's possible that it's not reachable via 
<classname>.<variable> and then you have to call it via 
self.__class__.<variable>.
I find this non intuitive. I heard the explanations why but that doens't 
make it more intuitive.

Then i got a tip that you can register a function that needs to be 
called when the object is going to be deleted.
For instance to register a function __exit, you do this:

import atexit
...
class Bla:
     def __init__(self)
     ...
         atexit.register(self.__exit)
     ...
     def __exit(self):

It still doesn't feel right to me as i would rather use __del__ but 
since the behaviour can't be predicted, using atext is the next best thing

Benedict



More information about the Python-list mailing list