I have declared a class Triangle
in Python which takes base and height as __init__
arguments and has a method area
that computes and returns the area of the triangle. The __eq__
method of the Triangle
class compares the area of the triangles and return the value. The class is defined as below:
class Shape(object):
def area(self):
raise AttributeException("Subclasses should override this method.")
class Triangle(Shape):
def __init__(self, base, height):
"""
base = base of the triangle.
height = height of the triangle
"""
self.base = base
self.height = height
def area(self):
return (self.base * self.height)/2
def __str__(self):
return 'Triangle with base ' + str(self.base) + ' and height ' + str(self.height)
def __eq__(self, other):
"""
Two triangles are equal if their area is equal
"""
return (self.area() == other.area())
Now I ran the program and created two instances to Triangle
t1
and t3
, and gave them different base and height but their area is equal. So t1 == t3
should be True
which is returned as True
only. But strangely, t1 > t3
is also returned as True
, which I don't understand why?
>>> t1 = Triangle(3,4)
>>> t3 = Triangle(2, 6)
>>> t1.area()
6
>>> t3.area()
6
>>> t1 == t3
True
>>> t1 > t3
True
>>> t1 < t3
False
>>>