Python borg pattern problem
It looks like it's working rather too well :-)
The issue is that the assignment self.__myvalue = ""
in __init__
will always clobber the value of myvalue
every time a new Borg is, er, created. You can see this if you add some additional print statements to your test:
conf = Config()
conf.myvalue("Hello")
print conf.myvalue() # prints Hello
conf2 = Config()
print conf.myvalue() # prints nothing
print conf2.myvalue() # prints nothing
Remove the self.__myvalue
and things will be fine.
Having said that, the implementation of myvalue()
is a little weird. Better, I'd say, to have explicit getters and setters using properties. You'll also want some code in __init__
to initialize the value of myvalue
if it doesn't exist yet, or to at least handle that it might not exist in the getter. Perhaps something like:
class Config(object):
"""
Borg singleton config object
"""
_we_are_one = {}
def __init__(self):
#implement the borg pattern (we are one)
self.__dict__ = self._we_are_one
def set_myvalue(self, val):
self._myvalue = val
def get_myvalue(self):
return getattr(self, '_myvalue', None)
myvalue = property(get_myvalue, set_myvalue)
c = Config()
print c.myvalue # prints None
c.myvalue = 5
print c.myvalue # prints 5
c2 = Config()
print c2.myvalue #prints 5
Combining the removal of self.__myvalue = ""
with the new-style Borg and the suggestions to avoid __
in variable names, we get:
class Config(object):
"""
Borg singleton config object
"""
_we_are_one = {}
_myvalue = ""
def __new__(cls, *p, **k):
self = object.__new__(cls, *p, **k)
self.__dict__ = cls._we_are_one
return self
def myvalue(self, value=None):
if value:
self._myvalue = value
return self._myvalue
if __name__ == '__main__':
conf = Config()
conf.myvalue("Hello")
conf2 = Config()
print conf2.myvalue()