The borg module contains the Borg class which provides an implementation of the Borg design pattern, which is a Singleton like pattern for 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.
Instances of the Borg class can implement the init() function which is called when the Borg class is first created and initialized. This allows the concrete Borg classes to set up the initial state for their object.
Example:
class Example(Borg):
def init(self, *args, **kwargs):
self.x = kwargs['x']
class First:
def __init__(self):
# An example of passing a value to the Borg instance
b = Example(x=200)
print "First, b.x:", b.x
class Second:
def __init__(self):
# Optionally could use Borg.getInstance()
b = Example.getInstance()
print "Second, b.x:", b.x
b.x = 500
if __name__ == '__main__':
First()
Second()
b = Example.getInstance()
print "Third, b.x:", b.x
# Prints:
# First, b.x: 200
# Second, b.x: 200
# Third, b.x: 500
Return the value of a private class attribute.