Borg Pattern Class usable from other classes?

Jan Wender j.wender at science-computing.de
Thu Oct 11 08:52:36 EDT 2001


Hi,
I tried to use the Borg Pattern from Alex Martelli
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531). 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 a.py, and also different access paths for the dict,
to no avail.

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

if __name__ == "__main__":
    a = Borg()
    a.t = "Huba"
    print "a", a.t
    b=Borg()
    print "b", b.t
    import b
    d=b.Test()
# --8<---snip---
# b.py
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,
Cheerio,
--
J.Wender at science-computing.de  -  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