'''__new__用于创建对象, __init__把创建的对象初始化''' class Person(object): def __new__(cls, *args, **kwargs): print('__new__被调用执行了,cls的id值为{0}'.format(id(cls))) obj=super().__new__(cls) print('创建的对象的id为:{0}'.format(id(obj))) return obj def __init__(self, name, age): print('__init__被调用了,self的id值为:{0}'.format(id(self))) self.name = name self.age = age print('object这个类对象的id为:{0}'.format(id(object))) print('Person这个类对象的id为:{0}'.format(id(Person))) #创建Person类的实例对象 p1=Person('张三',20) print('p1这个Person类的实例对象的id:{0}'.format(id(p1)))
E:\PycharmProjects\pythonProject\venv\Scripts\python.exe E:/PycharmProjects/pythonProject/demon1/demo49.py object这个类对象的id为:140716676791120 Person这个类对象的id为:2222433748464 __new__被调用执行了,cls的id值为2222433748464 创建的对象的id为:2222433123392 __init__被调用了,self的id值为:2222433123392 p1这个Person类的实例对象的id:2222433123392 进程已结束,退出代码0
标签:__,self,创建对象,Person,init,new,id From: https://www.cnblogs.com/988MQ/p/16726532.html