Borg - is that a singleton?

Graham Fawcett gmfawcett at
Sat Mar 1 03:53:37 CET 2003

"Giovanni Bajo" <noway at> wrote in message news:<pbK7a.164439$ZE.4850775 at>...
> Hello,
> Since Borg (as in
> rebinds the
> dictionaries to Borg.__shared_state, which is unique across the program, I
> fail to see how this can be used as singleton without counter-intuitive
> side-effects like:

<snipped code>

> since 'x' now refers to the same attribute for any borg'd instance, even
> across different classes. Citing GoF, usually you have only one monitor and
> only one printer in your system, but you don't want the current draw
> position to be the same on both (or whatever, just a silly example). 

Remember that Borg is a *design pattern*. I don't think it was ever
intended for use as a base class of your class hierarchy.

You would "borgify" your classes *individually* :

class Foo:
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state

class Bar:   
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state
In other words, imitate the Borg class, don't subclass it. There are,
after all, only two relevant lines of code to add -- an easy task.

-- Graham

More information about the Python-list mailing list