Singleton?

Jason Orendorff jason at jorendorff.com
Sun Jan 27 13:42:27 EST 2002


James T. Dennis writes:
>  Would this be considered to be a valid implementation of 
>  the GoF "singleton" pattern?
>
> [...]
> 
>  My test suite passes, of course.  But I'm not sure if this
>  would count as a valid "singleton" though it does seem to 
>  exhibit the desire behavior.  It this "cheating?"  What are
>  the consequences of this approach?

(grin) Cheating??  Your approach is about as good as any.

There are many approaches.  My favorite, when it's feasible, is:

  class _X:
      """ NOTE: Do not instantiate this class!  Use x instead. """
      pass

  x = _X()  # singleton instance

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list