+ 1

(solved)There are two questions, the second I have done but the first why it can't call area. Any advices ?

class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width*self.height #your code goes here def __add(self,other): self.width = self.width+other.width self.height = self.height+other.height return Shape(self.width, self.height) def __gt__(self,other): area1 = self.width*self.height area2 = other.width*other.height return area1 > area2 w1 = int(input()) h1 = int(input()) w2 = int(input()) h2 = int(input()) s1 = Shape(w1, h1) s2 = Shape(w2, h2) result = s1 + s2 print(result.area()) print(s1 > s2)

11th Feb 2021, 2:34 AM
Ratna13
Ratna13 - avatar
5 Respuestas
+ 2
because if you do shape1 + shape2, you doesn't expect to have result stored inside shape1... if you want so, you do shape1 = shape1 + shape2
11th Feb 2021, 5:05 AM
visph
visph - avatar
+ 2
# magic method __add__ take leading AND trailing double dunder # when you add width, and then height, I think you do not modify original (self) width and height def __add__(self,other): width = self.width+other.width height = self.height+other.height return Shape(width, height)
11th Feb 2021, 3:26 AM
visph
visph - avatar
0
Why self must be removed ?
11th Feb 2021, 5:00 AM
Ratna13
Ratna13 - avatar
0
Ohhh, I see, I know my mistake. Thank you visph
11th Feb 2021, 5:05 AM
Ratna13
Ratna13 - avatar
0
Okay, I know why I have to name not as same as in init, because it will change the originals. Btw, thanks for million, finally I got the meaning 😂
11th Feb 2021, 5:10 AM
Ratna13
Ratna13 - avatar