Differences between revisions 2 and 3
Revision 2 as of 2003-11-17 22:09:57
Size: 755
Editor: yermat
Comment:
Revision 3 as of 2003-11-17 22:10:13
Size: 761
Editor: yermat
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
You can use both as simple class or as a singleton.
You do not need to write code for each class you want to act as singleton.
 * You can use both as simple class or as a singleton.
 * You do not need to write code for each class you want to act as singleton.

Singleton

Voyez aussi http://c2.com/cgi/wiki?PythonSingleton

classmethod


pro

  • You can use both as simple class or as a singleton.
  • You do not need to write code for each class you want to act as singleton.


cons

   1 # Code is Public Domain.
   2 class Singleton:
   3     _singleton = None
   4     
   5     def getSingleton(cls):
   6         if not Singleton._singleton:
   7             Singleton._singleton = cls()
   8         return Singleton._singleton
   9 
  10     getSingleton = classmethod(getSingleton)
  11 
  12 
  13 class Test(Singleton):
  14     def test(self):
  15         print self.__class__,id(self)
  16 
  17 
  18 t1 = Test.getSingleton()
  19 t2 = Test.getSingleton()
  20 
  21 t1.test()
  22 t2.test()

PatternProgramming (last edited 2008-11-15 13:59:47 by localhost)

Unable to edit the page? See the FrontPage for instructions.