Help Operator overloading Python intermediate | Sololearn: Learn to code for FREE!


Help Operator overloading Python intermediate

This is the project concept: We are improving our drawing application. Our application needs to support adding and comparing two Shape objects. Add the corresponding methods to enable addition + and comparison using the greater than > operator for the Shape class. The addition should return a new object with the sum of the widths and heights of the operands, while the comparison should return the result of comparing the areas of the objects. This is the code that I wrote to solve the project: 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): return Shape(self.width + other.width, self.height + other.height) def gt(self, other): return self.area() > other.area() 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) This is the error output message: Line 23 Result = s1 +s2 TypeError: unsoported operand type(s) for +: 'Shape' and 'Shape' Can you guys, please help to fix it?

5/28/2022 2:21:23 AM

Luzardo Lopez

3 Answers

New Answer


Luzardo Lopez Everything is right just change your functions add to __add__ get to __get__


Thank you AJ


No __get__ doesn't exist as >, __gt__ does