The title is provocative, of course ;) However, I was browsing through our codebase here at work and I noticed a few usages of __del__ as a resource finalizer (i.e. __del__ just calls a close method).
I consider this practice an error, since with __del__ you are never sure that the resource will be released (http://docs.python.org/ref/customization.html#l2h-175) and anyway this should be done with try .. finally or the 'with' statement. So I got into thinking: "yes, using __del__ as a resource finalizer is wrong, but then what are good use cases for it? Let's look at the standard library and see what people use __del__ for". So I did, and to my dismay 95% of the __del__ methods in the standard library are just calling a close method!! In particular this happens in the following modules: (Python 2.5 on Ubuntu): zipfile, wave, urllib, close, tarfile, sunau, shelve, httplib, gzip, fileinput, dumbdbm, audiodev, aifc, bsddb.dbshelve, tempfile, socket, platform, ... (I got tired after this point) I see one good use case for __del__ in wsgiref.validate: here __del__ prints a warning if the resource is *not* closed explicitely. Something similar happens in subprocess and popen2, where __del__ updates the list of active processes. So I am beginning to wonder if there exists good use cases for __del__, apart for debugging/checking purposes. Can you provide some? Yes, you may take that as a challenge ;) Michele Simionato P.S. BTW, I should mention that if you search comp.lang.python for __del__ you will find hundreds of people who were bitten by __del__, so I usually give advices such as "you should never __del__ in your code". If I am wrong in giving this advice, please let me know! P.P.S. Here and there I hear rumors about deprecating __del__ and nothing happens, are there any news about that? Expecially concerning Py3k? -- http://mail.python.org/mailman/listinfo/python-list