代码
参考:http://www.python.org/dev/peps/pep-0318/
参考:http://www.python.org/dev/peps/pep-0318/
def
singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
print " new "
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
pass
x1 = MyClass()
# print new
x2 = MyClass()
本文转自博客园刘凯毅的博客,原文链接:py 单例,如需转载请自行联系原博主。
instances = {}
def getinstance():
if cls not in instances:
print " new "
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
pass
x1 = MyClass()
# print new
x2 = MyClass()