__del__ not working with cyclic reference? (and memory-leaked?)
Steven Taschuk
staschuk at telusplanet.net
Wed Jul 2 10:36:41 EDT 2003
Quoth Jane Austine:
[...]
> (I should not use __del__ for singleton patterns, but
> any other alternatives?)
You could register an atexit procedure; see the atexit module in
the standard library.
You *can* use __del__ for a singleton as long as there is no
cyclic reference, or if the interpreter deals with the cycle
anyway. Under 2.2.2 and 2.3b1 this seems to work:
# foo.py
class MySingleton(object):
def __del__(self):
print 'foo'
mysingleton = MySingleton()
The difference is that here the instance is referred to by the
module dict rather than the MySingleton class dict. (I think the
reason this makes a difference is that interpreter shutdown
includes a zapping of module dicts, breaking this cycle:
>>> import foo
>>> d = foo.__dict__
>>> d['mysingleton'].__class__.__dict__['__del__'].func_globals is d
True
Note that if your __del__ refers to global variables, they might
have been zapped by the time it gets called.)
--
Steven Taschuk w_w
staschuk at telusplanet.net ,-= U
1 1
More information about the Python-list
mailing list