0
class Rectangle:
    """created a class Rectangles, assigning values"""
    number_of_instances = 0
    print_symbol = "#"

    def __init__(self, width=0, height=0):
        self.height = height
        self.width = width
        Rectangle.number_of_instances += 1

    @property
    def width(self):
        return self.__width

    @width.setter
    def width(self, value):
        if not isinstance(value, int):
            raise TypeError("width must be an integer")
        elif value < 0:
            raise ValueError("width must be >= 0")

        self.__width = value

    @property
    def height(self):
        return self.__height

    @height.setter
    def height(self, value):
        if not isinstance(value, int):
            raise TypeError("height must be an integer")
        elif value < 0:
            raise ValueError("height must be >= 0")

        self.__height = value

    def area(self):
        return self.__height * self.__width

    def perimeter(self):
        if self.__height == 0 or self.__width == 0:
            return 0
        return (self.__height + self.__width) * 2

    def __str__(self):
        if self.__width == 0 or self.__height == 0:
            return ''
        for z in range(self.height - 1):
            print(str(self.print_symbol) * self.__width)
        return str(self.print_symbol * self.__width)

    def __repr__(self):
        return "Rectangle({}, {})".format(self.__width, self.__height)

    def __del__(self):
        print("Bye rectangle...")
        Rectangle.number_of_instances -= 1

    @staticmethod
    def bigger_or_equal(rect_1, rect_2):
        if not isinstance(rect_2, Rectangle):
            raise TypeError("rect_2 must be an instance of Rectangle")
        elif not isinstance(rect_1, Rectangle):
            raise TypeError("rect_1 must be an instance of Rectangle")
        elif rect_1 == rect_2:
            return rect_1
        elif rect_1 > rect_2:
            return rect_1
        elif rect_2 > rect_1:
            return rect_2

tried to compare but got an error...

TypeError: '>' not supported between instances of 'Rectangle' and 'Rectangle'

should I call any of the functions?

cards
  • 3,936
  • 1
  • 7
  • 25
Zebby
  • 11
  • 2
  • From which module the class `Rectangle` is from? – Carl HR Jun 26 '22 at 16:39
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 26 '22 at 16:41
  • 1
    In either case, you can reimplement `Rectangle` by making a new Rect class that inherits from `Rectangle`. Then inside this class, try implementing `__gt__()` or `__eq__()` as described here: https://stackoverflow.com/questions/5824382/enabling-comparison-for-classes – Carl HR Jun 26 '22 at 16:43
  • the error is on the static method – Zebby Jun 26 '22 at 16:49

2 Answers2

0
@staticmethod
def bigger_or_equal(rect_1, rect_2):

   if not isinstance(rect_1, Rectangle):
      raise TypeError("rect_1 must be an instance of Rectangle")

   elif not isinstance(rect_2, Rectangle):
      raise TypeError("rect_2 must be an instance of Rectangle")

   elif rect_1.area() == rect_2.area():
       return rect_1

   if rect_1.area() >= rect_2.area():
       return rect_1

   else:
       return rect_2
Ando Abza
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 05 '23 at 18:01
-1

Imagine that you created new class without any methods, and compare two objects of the class.

class MyClass:
    pass

if __name__ == '__main__':
    a = MyClass()
    b = MyClass()
    if a > b:
        print("a is bigger than b")
    else:
        print("a is not bigger than b")

Python cannot compare a and b, because there is no criteria for comparison between two objects. So we need to tell the interpreter when we consider a is bigger than b.

Many languages provide some way to define comparisons, like ==, !=, <, >.
In Python, you can define when we say a is bigger than b with overriding gt method.

class MyClass:
    def __init__(self, val1: int, val2: int):
        self.val1 = val1
        self.val2 = val2

    def __gt__(self, other):
        if isinstance(other, MyClass):
            return self.val1 * self.val2 > other.val1 * other.val2
        else:
            raise Exception("cannot compare(>) MyClass and {}".format(type(other)))


if __name__ == '__main__':
    a = MyClass(1, 5)
    b = MyClass(2, 3)
    if a > b:
        print("a is bigger than b")
    else:
        print("a is not bigger than b")

Here, I defined when we consider self is bigger than other. So interpreter call gt method when I use > for an object of Myclass.

If you want to consider rectangle is bigger than the other when width is bigger than other, you can write the code like

class Rectangle:
    """created a class Rectangles, assigning values"""
    number_of_instances = 0
    print_symbol = "#"

    def __init__(self, width=0, height=0):
        self.height = height
        self.width = width
        Rectangle.number_of_instances += 1

    def __gt__(self, other):
        if isinstance(other, Rectangle):
            return self.width > other.width
        else:
            raise Exception("cannot compare(>) Rectangle and {}".format(type(other)))

    ...

See https://docs.python.org/3/library/operator.html#operator

Taewoo Kim
  • 51
  • 3