weakrefs and bound methods

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Oct 7 13:14:35 EDT 2007


On Sun, 07 Oct 2007 16:38:23 +0000, Michele Simionato wrote:

> On Oct 7, 12:26 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> 
>> Drop all those `__del__()` methods as they prevent the garbage
>> collector from collecting "cycles".
> 
> I fully agree and I will add that __del__ methods are always a bad idea.

Always?

I recently wrote a bit of code where I needed to check that releasing the 
first object in a tree-like structure would allow Python to garbage 
collect all the other objects in a tree. I thought it would, but I wanted 
to be sure ("don't guess, test"), so I wrote a simple class, gave it a 
__del__ method that just printed self, inserted them in the tree, and 
then deleted the first one.

Worked like a charm.

Without __del__, what should I have done to test that my code was 
deleting objects and not leaking memory?

What should I do when my objects need to perform some special processing 
when they are freed, if I shouldn't use __del__?



-- 
Steven.



More information about the Python-list mailing list