[Tutor] Deleting singleton objects

Alexandre Ratti alex@gabuzomeu.net
Thu, 23 May 2002 19:34:44 +0200


At 11:20 23/05/2002 -0400, tutor-request@python.org wrote:
>From: alan.gauld@bt.com
>Subject: RE: [Tutor] Deleting singleton objects
>Date: Thu, 23 May 2002 13:27:40 +0100

> > is there a more efficient or standard way of implementing a
> > singleton in Python, that is a lot "cleaner".

>Also search this list archive on Activestate where (I think it
>was Danny) did a nice version based on the class __call__() method.

This may be the version Alan refers to. It is very short:

class Spam:
     """Singleton class"""
     def __call__(self):
         return self

Spam = Spam()

if __name__ == "__main__":
     # Testing
     toto = Spam()
     titi = Spam()
     assert id(toto) == id(titi)

Source : posted by Matt Kangas on 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558


Cheers.

Alexandre