strange __del__ behavior

Jp Calderone exarkun at divmod.com
Wed Jun 9 20:50:51 EDT 2004


John Hunter wrote:
> 
> For debugging purposes, I would like to know when C instances are
> being deleted; that's why I inserted the __del__ print method.  Is
> there any way to get that info w/o defining __del__ and thereby
> screwing up python's garbage collection?

   Weakrefs avoid the problem of screwing up garbage collection.  You 
may want to use something like this:

from weakref import ref
class CollectionReporter:
     def __init__(self):
         self.data = {}

     def _remover(self, id, msg):
         def removed(weakref):
             del self.data[id]
             print msg
         return removed

     def reportOnCollection(self, obj, msg):
         self.data[id(obj)] = ref(obj, self._remover(id(obj), msg))

   Jp




More information about the Python-list mailing list