# __new__ 返回实例时,__init__ 才会被调用 class Singleton(object): _instance = None def __new__(cls, *args): if not isinstance(cls._instance, cls): cls._instance = super(Singleton, cls).__new__(cls, *args) return cls._instance def __init__(self, name): self.name = name if __name__ == '__main__': # 创建Singleton两个引用,指向同一个对象, # 证明: # 使用不同的初始化参数,初始化不同引用指向的对象, # 然后打印两个不同引用指向的对象的数据域,结果相同 s1 = Singleton("Google") s2 = Singleton("Versign") print(s1.name) print(s2.name) print(id(s1) == id(s2))