Created
November 20, 2016 23:50
-
-
Save LotusChing/f205310b44363ed2ccd9fc9fe41dc8f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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