单例模式,多次实例化只返回同一个类实例。
class SimpleOne(object):
__one = None
def __init__(self):
super().__init__()
def __new__(cls, *args, **kwargs):
if not cls.__one:
cls.__one = object.__new__(cls, *args, **kwargs)
return cls.__one
if __name__ == "__main__":
id_1 = id(SimpleOne())
id_2 = id(SimpleOne())
print("id_1 equal id_2", id_1 == id_2)