太详细了。感谢感谢
逐渐理解
真的好使
#给长方形的长和宽赋值时自动进行形状检测,改变shape属性
class Rectangle:
def __init__(self,w=0,h=0):
self.width=w
self.height=h
if w==h:
self.shape='正方形'
else:
self.shape='长方形'
def __setattr__(self,name,value):
self.__dict__= value
if self.width==self.height:
self.shape='正方形'
else:
self.shape='长方形'
def getArea(self):
return self.width*self.height
尝试了很多方法,都无法调通,求高手指导!
#给长方形的长和宽赋值时自动进行形状检测,改变shape属性
class Rectangle:
def __init__(self,w=0,h=0):
self.width=w
self.height=h
if w==h:
self.shape='正方形'
else:
self.shape='长方形'
def __setattr__(self,name,value):
self.__dict__= value
try:
if self.width==self.height:
self.__dict__['shape']='正方形'
else:
self.__dict__['shape']='长方形'
except AttributeError:
self.__dict__['height']=-1
def getArea(self):
return self.width*self.height
解决掉了,✌
{:10_297:}学习
__repr__(self) 定义当被 repr() 调用时的行为
__str__(self) 定义当被 str() 调用时的行为
这是啥意思啊?就是类被调用时,调用这个方法吗?
我有个问题啊,当你重新定一个反运算的时候,比如:
class Nint(int):
def __radd__(self,other):
return int.__sub__(self,other)
a=Nint(3)
1+a
2
1+2
3
为什么当是数字跟数字相加的时候,那个+没有被改变
如果能加上应用实例就更好啦
留着
{:5_109:}
加油!大家一起加油!
努力学习打卡
xieixe
努力學習中~
真.灵活即强大
打卡,不明觉厉
顶贴表示下支持
一元操作符里面的
__pos__(self) 定义正号的行为:+x
__neg__(self) 定义负号的行为:-x
跟视频上的含义不一样,,,到底哪个才是对的哟???
慢慢的就看不懂了 越来越复杂 内置的还能改
太多了{:10_249:}