Skip to content

Instantly share code, notes, and snippets.

@elben
Created April 16, 2010 22:07
Show Gist options
  • Save elben/369037 to your computer and use it in GitHub Desktop.
Save elben/369037 to your computer and use it in GitHub Desktop.
def singleton(cls):
instances = {} # Line 2
def getinstance():
if cls not in instances:
instances[cls] = cls() # Line 5
return instances[cls]
return getinstance
class Counter:
def __init__(self):
self.count = 0
def inc(self):
self.count += 1
print type(Counter) # <type 'classobj'> # Line 15
Counter = singleton(Counter)
print type(Counter) # <type 'function'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment