+ 2

__gt__() python

Hello everyone, i have a little problem with the __gt__() method. 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): if Shape(self.width*self.height > other.width*other.height): return True else: return False 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) it returns "TypeError: __init__() missing 1 required positional argument: 'h'" how can i fix it if i wanna know if the area of the s1 is bigger than the area of s2? also, can i call somehow the "area()" function from above? thanks for your help

11th Jan 2022, 4:48 PM
Ramiz Besic
Ramiz Besic - avatar
3 ответов
+ 4
To compare instance area to another you can simply do this ... return self.area() > other.area()
11th Jan 2022, 5:04 PM
Ipang
+ 2
yes you are right! Thanks!
11th Jan 2022, 5:11 PM
Ramiz Besic
Ramiz Besic - avatar
+ 1
# Why you call the class in __gt__ ? You can do as follows: 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 self.width + other.width, self.height + other.height def __gt__(self, other): if self.width*self.height > other.width*other.height: return True else: return False 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 Jan 2022, 5:22 PM
JaScript
JaScript - avatar