10.2. Borg

10.2.1. Rationale

  • EN: Borg

  • PL: Borg

  • Type: object

10.2.2. Implementation

class Borg:
    __shared_state: dict = {}

    def __init__(self):
        self.__dict__ = self.__shared_state

10.2.3. Borg vs Singleton

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).