Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python borg pattern problem

I'm having problems implementing a borg in python. I found an example in an answer to this question but it's not working for me, unless I'm missing something. Here's the code:


class Config:
    """
    Borg singleton config object
    """
    __we_are_one = {}
    __myvalue = ""

    def __init__(self):
        #implement the borg pattern (we are one)
        self.__dict__ = self.__we_are_one
        self.__myvalue = ""

    def myvalue(self, value=None):
        if value:
           self.__myvalue = value
        return self.__myvalue

conf = Config()
conf.myvalue("Hello")
conf2 = Config()
print conf2.myvalue()

I assume this is meant to print "Hello", but for me it just prints a blank line. Any ideas why this might be?

like image 847
chrism1 Avatar asked Apr 14 '09 14:04

chrism1


People also ask

What is Borg in Python?

The Borg class implements the Borg design pattern which provides a singleton like pattern for Python. A Borg object can be accessed by calling the getInstance() function. This functions returns an instance of the Borg class which stores its state between successive calls to get the Borg object.

Is a Python module a singleton?

Modules are “singletons” in Python because import only creates a single copy of each module; subsequent imports of the same name keep returning the same module object.

What is a singleton class Python?

A Singleton pattern in python is a design pattern that allows you to create just one instance of a class, throughout the lifetime of a program. Using a singleton pattern has many benefits. A few of them are: To limit concurrent access to a shared resource. To create a global point of access for a resource.


1 Answers

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
like image 196
Jarret Hardie Avatar answered Oct 13 '22 18:10

Jarret Hardie