Borg Pattern Class usable from other classes?

Jan Wender j.wender at
Thu Oct 11 14:52:36 CEST 2001

I tried to use the Borg Pattern from Alex Martelli
( I have one file
containing the Borg class. In another file I defined a second class which wants
to access the information from the Borg class. It instantiates an object from
the Borg class, but it seems that the class dictionary __shared_state gets newly
initialized, rendering it empty. Any ideas to get it working? I tried already to
use a module global dict in, and also different access paths for the dict,
to no avail.

The two files:
class Borg:
    __s = {}
    def __init__(s):
        s.__dict__ = s.__s

if __name__ == "__main__":
    a = Borg()
    a.t = "Huba"
    print "a", a.t
    print "b", b.t
    import b
# --8<---snip---
import a

class Test:
    def __init__(s):
        c = a.Borg()
        if hasattr(c, 't'): print 'c', c.t
        else: print 'c has no t'
# --8<---snip---

Thanks for any insights,
J.Wender at  -  Fon +4970719457-257 Fax-211
science+computing ag - Hagellocher Weg 71-75 - 72070 Tübingen
Beware, software never dies. (Wietse Venema)

More information about the Python-list mailing list