Python 设计模式 —— 单例

class Singleton(object):
	_instance = None
	def __new__(cls, *args, **kwargs):
		if not _instance:
			cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
		return cls._instance
class MySingleton(Singleton):
	a = 0
c1 = MySingleton()
c2 = MySingleton()
print(c1 == c2)
c1.a += 1
print(c2.a)

你可能感兴趣的:(OOP)