__del__ and reference count problem

flupke flupke at nonexistingdomain.com
Thu Apr 21 17:22:42 EDT 2005


ajikoe at gmail.com wrote:
> look at this discussion:
> http://www.dbforums.com/archive/index.php/t-1100372.html
> 
> it looks like we have to use other way, hold the data we want to
> preseve in an object, because it is possible the class is removed
> before the instance cleaned, and we can not expect __del__ 100% in
> handling finalizing process.
> 
> Pujo
> 

That forum helped. I changed this in my code and it worked
def __del__(self):
     self.__class__.refcount -= 1
     if (self.__class__.refcount == 0):
         print "Last table standing"
     else:
         print "There is/are still %d table(s) left." % 
self.__class__.refcount

Output:
table <__main__.Table instance at 0x009D5B98>
refcount = 1
table <__main__.Table instance at 0x009D5BC0>
refcount = 2
Supplier returned from a 1: not implemented
Supplier returned from b 2: not implemented
There are 2 tables active.
There are 2 tables active.
There is/are still 1 table(s) left.
Last table standing

Seems to work allright !

Thanks ! :)
Benedict



More information about the Python-list mailing list