Singleton pattern in python
1 2 class Borg: 3 __shared_state = {} 4 def __init__(self): 5 self.__dict__ = self.__shared_state
What a pythonic way to use Singleton. It uses shared-state
approach where you can actually have many instances as you want
but they all share the same state. See Alex's recipe.
1 2 >>> b = Borg() 3 >>> b.x = 1 4 >>> c = Borg() 5 >>> c.x 6 1