例子 class Singleton(object): _instance = None def __new__(cls, *args, **kw): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kw) return cls._instance class MyClass(Singleton): a = one = MyClass() two = MyClass() print…
单例:即一个类只能生成唯一的一个实例,python中的类如果没有被实例化,则cls._instance为None 如下: class Singleton(object): def __new__(cls,*args,**kw): if not hasattr(cls,"_instance"): orgi=super(Singleton,cls) cls._instance=orgi.__new__(cls,*args,**kw) return cls._instance class M…