DEV Community

Discussion on: We are the Borg

Collapse
nuculabs_dev profile image
Nucu Labs • Edited on
class Borg:
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state

In Python you can override the built-in __dict__ attribute with your own dictionary. The __dict__ holds all the attributes of the Borg class and __shared_state is a static variable. When instantiating a Borg class __init__ is called and it overrides the __dict__ with the static __shared_state, thus each class will have the same brain: __shared_state. If you modify variables, their values will be reflected in __shared_state. :D