what is singleton pattern?
the singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions a
def singleton(cls, *args, **kw):
instances = {}
def _singleton():
if cls not in instances:
instances[cls] = cls(*args, **kw)
return instances[cls]
return _singleton
@singleton
class Test(object):
pass
one = Test()
two = Test()
print "one id=%s" % id(one)
print "two id=%s" % id(two)
one id=140313354635728
two id=140313354635728
one 和 two的id一样,所以他们是同一个实例