How does Mr. Martelli's Borg recipe work ?

Ian Bicking ianb at colorstudy.com
Wed Jul 23 05:19:16 CEST 2003


On Tue, 2003-07-22 at 20:37, Mars wrote:
> I have looked long and hard at Mr. Martelli's Borg recipe:
> 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
> 
> It is a very useful substitute for a Singleton, but I can't figure out
> how it works. 

While I don't want to discourage you from learning more about the
internals of Python objects (certainly a worthwhile goal), "singletons"
are usually thought about too hard by people new to Python.  This is a
good way to make a singleton:

class _Something:
   ...

TheSomething = _Something()


Then just never refer to _Something again.  Import TheSomething (calling
it whatever you want), and use it, not its class.  It's a singleton
because there's only one of them.  Simplicity!

  Ian







More information about the Python-list mailing list