4.2. Borg

  • Ensure all instances of a class share the same state

The Borg design pattern, also known as the Monostate pattern, is a design pattern that ensures all instances of a class share the same state. In Python, this is typically implemented using a shared dictionary.

The real reason that borg is different comes down to subclassing. If you subclass a borg, the subclass' objects have the same state as their parents classes objects, unless you explicitly override the shared state in that subclass. Each subclass of the singleton pattern has its own state and therefore will produce different objects. Also in the singleton pattern the objects are actually the same, not just the state (even though the state is the only thing that really matters). [1]

4.2.1. Problem

>>> class Config:
...     pass
>>>
>>>
>>> a = Config()
>>> b = Config()

Now, let's set some configuration values on the first instance:

>>> a.username = 'alice'
>>> a.password = 'secret'
>>> a.host = 'example.com'

Getting the value of the host attribute from the first instance will work as expected, but getting the value from the second instance will raise an AttributeError:

>>> a.username
'alice'
>>>
>>> b.username
Traceback (most recent call last):
AttributeError: 'Config' object has no attribute 'username'

Both instances are different objects:

>>> a
<__main__.Config object at 0x110228590>
>>>
>>> b
<__main__.Config object at 0x1076f2c10>

We can see that the first instance has the configuration values set, but the second instance does not:

>>> vars(a)
{'username': 'alice', 'password': 'secret', 'host': 'example.com'}
>>>
>>> vars(b)
{}

4.2.2. Solution

>>> class Borg:
...     _state = {}
...
...     def __init__(self):
...         self.__dict__ = self._state
>>>
>>>
>>> class Config(Borg):
...     pass
>>>
>>>
>>> a = Config()
>>> b = Config()

Now, let's set some configuration values on the first instance:

>>> a.username = 'alice'
>>> a.password = 'secret'
>>> a.host = 'example.com'

Getting the value of the host attribute from the first instance will work as expected, and getting the value from the second instance will also work:

>>> a.username
'alice'
>>>
>>> b.username
'alice'

Both instances are different objects:

>>> a
<__main__.Config object at 0x110228590>
>>>
>>> b
<__main__.Config object at 0x1076f2c10>

We can see that the first instance has the configuration values set, and the second instance has the same values as well:

>>> vars(a)
{'username': 'alice', 'password': 'secret', 'host': 'example.com'}
>>>
>>> vars(b)
{'username': 'alice', 'password': 'secret', 'host': 'example.com'}

4.2.3. References

4.2.4. Assignments

# %% About
# - Name: DesignPatterns Creational Borg
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Implement Borg pattern
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zaimplementuj wzorzec Borg
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> a = Borg()
>>> b = Borg()

>>> a is b
False

>>> a.name = 'Mark'
>>> b.name
'Mark'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
Borg: type

# %% Data

# %% Result
class Borg:
    ...