Reputation: 21
I'm trying to write a method(it) that compares the size (area) of the rectangle with the area of another rectangle passed as a parameter:
class Rectangle:
def __init__(self, x, y):
self.width = x
self.height = y
def area(self):
a = self.width * self.height
return a
def __it__(self,second):
return self.area < second.area
But I keep getting error:
TypeError: unorderable types: Rectangle() < Rectangle()
I'm not to sure how to fix this problem
Upvotes: 1
Views: 117
Reputation: 18908
You had a typo. It's __lt__
, not __it__
, and you need to call the area()
as a function unless you set that as a property
.
Fixing all that...
>>> class Rectangle:
... def __init__(self, x, y):
... self.width = x
... self.height = y
... def area(self):
... a = self.width * self.height
... return a
... def __lt__(self,second):
... return self.area() < second.area()
...
>>> Rectangle(1,3) > Rectangle(4,5)
False
Upvotes: 4
Reputation: 1863
Area is a method, you're using it as though it's a variable. Adding parens should fix it (and if you're trying to do less than, it should be __lt__
):
def __lt__(self, second):
return self.area() < second.area()
Upvotes: 0