Python基础课:类的特殊方法
class Rectangle: '这是一个矩形类' def __init__(self,length,width): if isinstance(length,(int,float)) and isinstance(width,(int,float)): self.length = length self.width = width else: raise TypeError #return None def area(self): areas = self.length * self.width return areas def __str__(self): return '宽为%s,高为%s'%(self.width,self.length) def __repr__(self): return '面积为:%s'%(self.area()) def __call__(self): return '这是一个call方法' def __add__(self,other): add_length = self.length + other.length add_width = self.width + other.width return add_length,add_width b = Rectangle(3,5) b.__dict__ b.__doc__ #print(b) b c = Rectangle(4,6)