[Tutor] Deleting singleton objects

Hans Nowak roodbaard@earthlink.net
Wed, 22 May 2002 23:20:49 -0400


On 23 May 2002, at 10:16, BELSEY, Dylan wrote:

> Hi,
> 	I am trying to implement singletons in Python and need to remove
> them , once I have finished with them,  from the namespace for certain
> functionality within a GUI system.  Below is some simplified code of the
> situation which replicates my problem.  The problem is that I can't seem to
> totally remove the object from the active "dictionary".  If I could then
> this would be indicated by the calling of the __del__() function once the
> reference counter has reached zero.  Since this is not occurring it is
> obvious that there still exists some reference to the singleton object but I
> don't know where it is.  

I do. :)  It's right here:

> class hello:
>     __single = None

You use the class attribute __single to store a reference to the instance 
made. Not a bad idea, but when you want to delete the singleton entirely, 
you'll have to set this value again (to None, I guess, for consistency).

You'll have to do this by hand, by the way. The __del__ method cannot do this 
for you, because it's only called if there are no more references to the 
instance... IOW, as long as __single contains a reference, __del__ isn't 
called.

HTH,

--Hans Nowak (roodbaard@earthlink.net)
http://www.awaretek.com/nowak/