class User:
def __new__(cls, *args, **kwargs):
print("now it's in new")
return super().__new__(cls) # 如果不返回对象,则不会进入init方法
def __init__(self, name):
self.name = name
print("now it's in init")
if __name__ == '__main__':
user = User(name='dylan')
print(user.name) # 如果不返回类,则会提示没有该属性
//结果
now it's in new
now it's in init
dylan
#new是用来控制对象的生成过程,在对象生成之前
#init是用来完善对象的
#如果new不返回对象, 则不会调用init函数