github.com/hoffmann Peter Hoffmann on Stackoverflow @peterhoffmann on twitter Peter Hoffmann on Facebook Contact me per email Subscribe to Atom Feed

Peter Hoffmann

Software Engineer
prev page next page

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

Posted on August 28, 2008
#stackoverflow #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.