Comparison Magic Methods
Similarly to the mathematical magic methods, here we take self and other as arguments and compare their desired attributes. These magic methods should always return True or False.
| Magic Method | Operation |
|---|---|
__eq__(self, other) | == |
__gt__(self, other) | > |
__ge__(self, other) | >= |
__lt__(self, other) | < |
__le__(self, other) | <= |
__ne__(self, other) | != |
Note
To understand the naming of these methods, look at some examples:
eqisequal.gtisgreaterthan.leisless orequal.neisnotequal.
12345a = 12.3 b = 5.7 print("a >= b is", a >= b) print("a.__ge__(b) is", a.__ge__(b))
Look at the example of Road class:
123456789101112131415161718class Road: def __init__(self, length): self.length = length def __eq__(self, other): if isinstance(other, Road): return self.length == other.length # compare with road return self.length == other # compare with number road_1 = Road(20) road_2 = Road(30) road_3 = Road(20) print(road_1 == road_2) print(road_1 == road_3) print(road_1 == 20) print(road_1 == 20.0) print(road_1 == 1412)
Note
To optimize the code, you can utilize existing comparison methods. For example, if you have implemented the
__lt__()method (<), by implementing the__ge__()method (>=), you can usereturn not (instance1 < instance2)which will return the opposite result of the already implemented<operator.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Ställ mig frågor om detta ämne
Sammanfatta detta kapitel
Visa verkliga exempel
Awesome!
Completion rate improved to 2.78
Comparison Magic Methods
Svep för att visa menyn
Similarly to the mathematical magic methods, here we take self and other as arguments and compare their desired attributes. These magic methods should always return True or False.
| Magic Method | Operation |
|---|---|
__eq__(self, other) | == |
__gt__(self, other) | > |
__ge__(self, other) | >= |
__lt__(self, other) | < |
__le__(self, other) | <= |
__ne__(self, other) | != |
Note
To understand the naming of these methods, look at some examples:
eqisequal.gtisgreaterthan.leisless orequal.neisnotequal.
12345a = 12.3 b = 5.7 print("a >= b is", a >= b) print("a.__ge__(b) is", a.__ge__(b))
Look at the example of Road class:
123456789101112131415161718class Road: def __init__(self, length): self.length = length def __eq__(self, other): if isinstance(other, Road): return self.length == other.length # compare with road return self.length == other # compare with number road_1 = Road(20) road_2 = Road(30) road_3 = Road(20) print(road_1 == road_2) print(road_1 == road_3) print(road_1 == 20) print(road_1 == 20.0) print(road_1 == 1412)
Note
To optimize the code, you can utilize existing comparison methods. For example, if you have implemented the
__lt__()method (<), by implementing the__ge__()method (>=), you can usereturn not (instance1 < instance2)which will return the opposite result of the already implemented<operator.
Tack för dina kommentarer!