Sal
Peter Hoffmann Director Data Engineering at Blue Yonder. Python Developer, Conference Speaker, Mountaineer

Is there a simple, elegant way to define Singletons in Python?

This my Answer to the stackoverflow question: Is there a simple, elegant way to define Singletons in Python?:

A slightly different approach to implement the singleton in python is the borg pattern by Alex Martelli (google employee and python genius).

class Borg:
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state

So instead of forcing all instances to have the same identity they share state.