單例模式,多次實(shí)例化只返回同一個(gè)類實(shí)例慕购。
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)