Skip to content

Instantly share code, notes, and snippets.

@qsorix
Created May 13, 2010 11:50
Show Gist options
  • Save qsorix/399747 to your computer and use it in GitHub Desktop.
Save qsorix/399747 to your computer and use it in GitHub Desktop.
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
def __init__(self):
print "creating"
if __name__ == "__main__":
foo = MyClass()
bar = MyClass()
if foo == bar:
print "nice!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment