implementing singleton class at the module level

Satchidanand Haridas sharidas at zeomega.com
Thu Feb 10 08:49:54 EST 2005


Hi,

I was looking at ways to implement a Singleton class. I saw some methods 
described on the PythonSingleton wiki 
(http://c2.com/cgi/wiki?PythonSingleton). I implemented the following.

<code>

module: A.py
----------------------
class Singleton:
    def __init__(self):
       #do something


singleton_instance = Singleton()



Then in any other module (eg B.py):

from A import singleton_instance


</code>

singleton_instance will be created only once and can be reused in other 
modules. But is this ok? I am trying to figure out what are the 
disadvantages of using the above method. I would appreciate any 
comments. thanks.

regards,
Satchit




More information about the Python-list mailing list