I got a python code now (thanks a lot scotty3785):
from dataclasses import dataclass
@dataclass
class House:
Address: str
Bedrooms: int
Bathrooms: int
Garage: bool
Price: float
def getPrice(self):
print(self.Price)
def newPrice(self, newPrice):
self.Price = newPrice
def __str__(self):
if self.Garage==1:
x="Attached garage"
else:
x="No garage"
return f"""{self.Address}
Bedrooms: {self.Bedrooms} Bathrooms: {self.Bathrooms}
{x}
Price: {self.Price}"""
h1 = House("1313 Mockingbird Lane", 3, 2.5, True, 300000)
h2 = House("0001 Cemetery Lane", 4, 1.75, False, 400000)
print(h1)
print(h2)
h1.newPrice(500000)
h1.getPrice()
h2<h1
Now I need to compare the prices of both h1 and h2 using h2<h1
and give a Boolean as output.
CodePudding user response:
You can either implement the special method __lt__
on your class or pass the order
and eq
arguments as True
to the dataclass
decorator(be aware that it will compare your elements as tuple). So you should have something like this:
@dataclass(eq=True, order=True)
class House:
Address: str
Bedrooms: int
Bathrooms: int
Garage: bool
Price: float
# your code...
or, the most appropriate to your case:
@dataclass(order=True)
class House:
Address: str
Bedrooms: int
Bathrooms: int
Garage: bool
Price: float
def __lt__(self, other_house):
return self.Price < other_house.Price