Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # To make python classes more powerful we should implement "rich comparisons".
- # Hashes are another dunder method which can be useful when creating many 'generic' objects
- class Car:
- def __init__(self, make: str, model: str, hp: int):
- self.make = make
- self.model = model
- self.hp = hp
- def __repr__(self):
- return f"A {self.make} {self.model} with {self.hp} horsepower"
- def __hash__(self):
- return hash(tuple(self.__dict__.values()))
- def __eq__(self, other):
- return (self.make, self.model, self.hp) == (other.make, other.model, other.hp)
- def __lt__(self, other):
- return self.hp < other.hp
- def __le__(self, other):
- return self.hp <= other.hp
- def __gt__(self, other):
- return self.hp > other.hp
- def __ge__(self, other):
- return self.hp >= other.hp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement