Brain Dead Singleton

Robin Becker robin at SPAMREMOVEjessikat.fsnet.co.uk
Sat Jun 5 07:54:24 EDT 2004


Peter Hansen wrote:

> Robin Becker wrote:
> 
>> Colin Brown wrote:
>>
>>> Whenever I want a singleton "class", I just use a module with 
>>> functions and
>>> global variables. Modules only import once.
>>>
>> should often work, but this illustrates how it can go wrong
> 
> [...]
> 
>> del sys.modules['my_singleton_module']
> 
> [...]
> 
>> so if sys.modules gets interfered with approriately, your singleton 
>> can start breeding.
> 
> 
> We're all adults.  How often do you see (or write) code that
> goes around deleting modules from sys.modules.  Colin's
> suggesting is just fine for most sane situations.
> 
> -Peter

well I actually have a use case for deleting modules from sys.modules. 
In some legacy code for ReportLab documentation, import was used in the form

import chapter1
import chapter2
.....

in two different documents causing confusion. An easy fix involved 
restoring sys.modules to a pristine state before each document was 
generated.
-- 
Robin Becker



More information about the Python-list mailing list