# __author: "ZXYang"
# date: 2020/11/29
"""
__new__方法
"""
class Func(object):
def __init__(self, name):
self.name = name
print("这是__init__方法")
# new方法先执行
def __new__(cls, *args, **kwargs):
print("这是__new__方法")
return super().__new__(cls)
# he = Func("LI")
# print(he.name)
"""
单例模式
"""
class Test(object):
# count = 0
isinstance = None
def __new__(cls, *args, **kwargs):
if not cls.isinstance:
cls.isinstance = object.__new__(cls)
# cls.count += 1
return cls.isinstance
else:
return cls.isinstance
h1 = Test()
h2 = Test()
h3 = Test()
print(id(h1), id(h2), id(h3))