Skip to content

Instantly share code, notes, and snippets.

@LotusChing
Created November 20, 2016 23:50
Show Gist options
  • Save LotusChing/f205310b44363ed2ccd9fc9fe41dc8f0 to your computer and use it in GitHub Desktop.
Save LotusChing/f205310b44363ed2ccd9fc9fe41dc8f0 to your computer and use it in GitHub Desktop.
# coding:utf-8
def singleton(cls):
instances = {}
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper
@singleton
class Single(object):
def __init__(self, n):
self.n = n
s1 = Single(10)
s2 = Single(11)
print('class: {} values: {}'.format(s1, s1.n))
print('class: {} values: {}'.format(s2, s2.n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment