static object

Russell Owen nospam at nospam.invalid
Wed Jan 3 18:30:45 EST 2007


In article <459c306c$0$314$426a74cc at news.free.fr>,
 meelab <meelab at free.fr> wrote:

> Dear All,
> 
> I am looking for a way to create a "static object" or a "static class" -
> terms might be inappropriate - having for instance:
> 
> class StaticClass:
>     .
>     .
> 
> and then
> staticObject1 = StaticClass()
> staticObject2 = StaticClass()
> 
> so that staticObject1 and staticObject2 refers exactly to the same
> instance of object.

Personally I do the following (in its own module). There may be a better 
way, but this is simple and it works:

_theSingleton = None

def getSingleton():
   global _theSingleton
   if not _theSingleton:
      _theSingleton = _Singleton()
   return _theSingleton

class _Singleton:
   def __init__(self, ...):
      ...


-- Russell



More information about the Python-list mailing list