A startup puzzle

Bengt Richter bokr at oz.net
Tue Sep 30 09:44:53 EDT 2003


On Mon, 29 Sep 2003 23:24:51 GMT, "Steve Holden" <sholden at holdenweb.com> wrote:
[...]
>
>I've followed this thread, and I'm having trouble understanding why nobody's
>suggested using Alex martelli's "borg" pattern, which he originally called a

I think because (IIUC) the OP wants to import * to avoid naming a container,
and he's willing to live read-only for the resulting bindings (though the
bindings can of course be to mutable things).

>"statel;ess proxy", where all objects of a given type share the same state
>information. Sounds to me that's what you really want, no?
>

>This would mean that in leo_global you would have something like this:
>
>leo_global.py:
>-------------
>class Borg(object):
>    _state = {}
>    def __new__(cls, *p, **k):
>        self = object.__new__(cls, *p, **k)
>        self.__dict__ = cls._state
>        return self
>
>
>module1.py
>-------------
>from leo_global import Borg
>
>myborg = Borg()
>myborg.var1 = "This is var 1"
>
>import module2
>
>print "Module 1's borg has var1 =", myborg.var1, id(myborg.var1)
>print "Module 1's borg has var2 =", myborg.var2, id(myborg.var2)
>
>
> module2.py:
>-------------
>from leo_global import Borg
>
>myborg = Borg()
>myborg.var2 = "This is var 2"
>
>print "Module 2's borg has var1 =", myborg.var1, id(myborg.var1)
>print "Module 2's borg has var2 =", myborg.var2, id(myborg.var2)
>
>The result of all this appears to be what you want: a simple way of creating
>an easily-referenced shared state. Vide:
>
>C:\Steve\Projects\Python>python module1.py
>Module 2's borg has var1 = This is var 1 7766584
>Module 2's borg has var2 = This is var 2 8008928
>Module 1's borg has var1 = This is var 1 7766584
>Module 1's borg has var2 = This is var 2 8008928
>
Yes, but UIAM [1]

    import leo_global as myborg  # leo_global.py being an empty file for this example

instead of

    from leo_global import Borg
    myborg = Borg()

would let you do what you are showing in the above ;-)

[1] Got a little nervous about that, so I tried it:

============================================================
[ 6:43] C:\pywk\clp\leo\SteveHolden>dir leo_global.py
 Volume in drive C is System
 Volume Serial Number is 14CF-C4B9

 Directory of C:\pywk\clp\leo\SteveHolden

03-09-30  06:35                      0 leo_global.py
               1 File(s)              0 bytes
                             56,197,120 bytes free

[ 6:43] C:\pywk\clp\leo\SteveHolden>type module1.py
#module1.py
#-------------
#from leo_global import Borg
#
#myborg = Borg()
import leo_global as myborg
myborg.var1 = "This is var 1"

import module2

print "Module 1's borg has var1 =", myborg.var1, id(myborg.var1)
print "Module 1's borg has var2 =", myborg.var2, id(myborg.var2)




[ 6:44] C:\pywk\clp\leo\SteveHolden>type module2.py
# module2.py:
#-------------
#from leo_global import Borg
#
#myborg = Borg()
import leo_global as myborg
myborg.var2 = "This is var 2"

print "Module 2's borg has var1 =", myborg.var1, id(myborg.var1)
print "Module 2's borg has var2 =", myborg.var2, id(myborg.var2)


[ 6:44] C:\pywk\clp\leo\SteveHolden>python module1.py
Module 2's borg has var1 = This is var 1 9439256
Module 2's borg has var2 = This is var 2 9439496
Module 1's borg has var1 = This is var 1 9439256
Module 1's borg has var2 = This is var 2 9439496

=================================================================================
Regards,
Bengt Richter




More information about the Python-list mailing list