It gives an erorr saying that __init__ is missing the argument 'h' in __gt__ although it's not missing! Some explain please. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

It gives an erorr saying that __init__ is missing the argument 'h' in __gt__ although it's not missing! Some explain please.

class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width*self.height def __add__(self, other): return Shape(self.width + other.width, self.height + other.height) def __gt__(self, other): return Shape(self.width * self.width > other.height * other.height) w1 = 1 h1 = 2 w2 = 3 h2 = 4 s1 = Shape(w1, h1) s2 = Shape(w2, h2) result = s1 + s2 print(result.area()) print(s1 > s2)

12th Aug 2023, 10:05 AM
Intermediate Depression
Intermediate Depression - avatar
6 Answers
+ 1
Intermediate Depression if you put the comparison inside Shape(), Python interprets this as creating a new shape... Shape(0,) or Shape(1,) The boolean result ( either 1 for True or 0 for False) is used as the first argument w, and there is no h argument, that is why there is the error msg about init missing h.
12th Aug 2023, 12:13 PM
Bob_Li
Bob_Li - avatar
+ 3
your __gt__ should return a boolean, not a Shape. maybe you want to compare areas, then it should be def __gt__(self, other): return (self.width*self.height)>(other.width*other.height)
12th Aug 2023, 11:09 AM
Bob_Li
Bob_Li - avatar
+ 2
Well done Bob_Li , you beat me to it 🤣👍
12th Aug 2023, 11:14 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Rik fast fingers > fat fingers 😁
12th Aug 2023, 11:15 AM
Bob_Li
Bob_Li - avatar
0
Bob_Li Oh no you didn't get me, i already know how to fix the code, that would be. def __gt__(self, other): return self.area() > other.area() I was just wondering what is wrong with "shape(slef.width * self.height > other.width * other.height" Why does it return an erorr ?
12th Aug 2023, 11:58 AM
Intermediate Depression
Intermediate Depression - avatar
0
Bob_Li TYSM
12th Aug 2023, 12:15 PM
Intermediate Depression
Intermediate Depression - avatar