Inspired by the ever-popular Borg pattern, objects inheriting from Hivemind share state if initialised with the same arguments
1 2 3 4 5 6 7 8 | from collections import defaultdict
from json import dumps
class Hivemind:
__shared_states = defaultdict(dict)
def __init__(self, *args, **kwargs):
key = type(self).__name__, dumps(args, sort_keys=True), dumps(kwargs, sort_keys=True)
self.__dict__ = self.__shared_states[key]
|