标签:style color ar sp on ad ef c python
class Singleton2(type):
def __init__(cls, name, bases, dict):
super(Singleton2, cls).__init__(name, bases, dict)
cls._instance = None
def __call__(cls, *args, **kw):
if cls._instance is None:
cls._instance = super(Singleton2, cls).__call__(*args, **kw)
return cls._instance
class MyClass(object):
__metaclass__ = Singleton2
one = MyClass()
two = MyClass()
two.a = 3
print one.a
#3
print id(one)
#31495472
print id(two)
#31495472
print one == two
#True
print one is two
#True
标签:style color ar sp on ad ef c python
原文地址:http://my.oschina.net/u/925666/blog/307053