It is __del__ calling twice for some instances?

Max Yuzhakov gmt at sdf-eu.org
Thu Aug 17 18:28:31 EDT 2006


Duncan Booth wrote:

 DB>  You should post a working code sample which generates your output if you 
 DB>  want a more useful answer.

Hello!

Today I have found a compact variant of a code which shows my question:
-----------------------------------------------------------------------
#!/usr/local/bin/python -d
# -*- coding: koi8-u -*-

class foo:
    def __init__(self, other):
        self.other = other

        global ini_cnt
        ini_cnt +=1

    def __del__(self):
        global del_cnt
        del_cnt +=1

def stat():
    print "-"*20
    print "ini_cnt = %d" % ini_cnt
    print "del_cnt = %d" % del_cnt
    print "difference = %d" % (ini_cnt-del_cnt)

ini_cnt = 0
del_cnt = 0
loop_cnt = 75

a = foo(None)

for i in xrange(loop_cnt):
    a = foo(a)

stat()
a = None
stat()
-----------------------------------------------------------------------

And result is:
--------------------
ini_cnt = 76
del_cnt = 0
difference = 76
--------------------
ini_cnt = 76
del_cnt = 77
difference = -1

Why for some instance __del__ called twice?
Such behaviour of __del__ seems to me unpredictable.

Thanks for Your attention!
-- 
GMT More Then ...



More information about the Python-list mailing list