__add__()和__add__()
class Student:
def __init__(self,name):
self.name=name
def __add__(self, other):
return self.name+other.name
def __len__(self):
return len(self.name)
stu=Student('顶顶顶')
stu1=Student('ddd')
s=stu1+stu#实现两个对象的加法运算(因为Student类中,编写--add--()的特殊方法)
print(s)
s=stu.__add__(stu1)
print('----------------------')
lst=[11,22,333,4444]
print(len(lst))
print(lst.__len__())#__len__输出单位长度
print(len(stu1))
__new__()和__init__()
class Preson(object):
def __new__(cls,*ages,**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):
self.name=name
self.age=age
print('objec这个类对象的id为{0}'.format(id(object)))
print('preson这个类对象的id为{0}'.format(id(Preson)))
p1=Preson('ddd',20)
print('p1这个Person类的实例对象的id{0}'.format(id(p1)))