How does Mr. Martelli's Borg recipe work ?

Bengt Richter bokr at oz.net
Wed Jul 23 17:05:50 EDT 2003


On Wed, 23 Jul 2003 12:48:00 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:

>Quoth John Roth:
>  [...]
>> I can kind of understand the justification for the Borg pattern
>> in Python releases before 2.2, because there was no way of
>> creating a true singleton in those releases. However, in 2.2 and
>> later, it's really easy to create one using new style classes.
>  [...implementing singletons with __new__...]
>> That being the case, I'd like to see the Borg pattern go the way
>> of a fondly remembered hack that is no longer necessary.
>
>Just out of curiosity: why do you prefer singletons to Borgs in
>the first place?
>
>(I don't see Borg as a hack to get the behaviour of a singleton; I
>see it as a more direct way to solve the problem which singletons
>are supposed to solve.  Thus to me Borg is actually preferable, in
>those exceedingly rare cases when that problem actually arises.)
>
How about just

    import zerolengthfile as borginstancename

and using it? E.g.,

 [14:04] C:\pywk\clp>dir zer*, a.* b.*
 <snips>
 03-07-23  13:50                      0 zero_len.py
 03-07-23  14:01                     28 a.py
 03-07-23  14:02                     28 b.py
 
 [14:05] C:\pywk\clp>type a.py
 import zero_len as aborg
 
 
 [14:05] C:\pywk\clp>type b.py
 import zero_len as bborg
 
 
 [14:05] C:\pywk\clp>python
 Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import zero_len as inter
 >>> import a
 >>> import b
 >>> dir()
 ['__builtins__', '__doc__', '__name__', 'a', 'b', 'inter']
 >>> dir(a)
 ['__builtins__', '__doc__', '__file__', '__name__', 'aborg']
 >>> dir(b)
 ['__builtins__', '__doc__', '__file__', '__name__', 'bborg']
 >>> inter.x = 123
 >>> a.aborg.x
 123
 >>> b.bborg.x
 123
 >>> b.bborg.y = 456
 >>> a.aborg.y
 456
 >>> inter.y
 456
 >>>
 
Regards,
Bengt Richter




More information about the Python-list mailing list