Skip to content

Commit 3f75ce1

Browse files
committed
Add comparison example for __lt__ dunder method in README.md and implement corresponding example script
1 parent 2d2ce78 commit 3f75ce1

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

python_utils/dunder_methods/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- [Equality check for all attributes](__eq__1.py)
77
- [Equality check for a single attribute](__eq__2.py)
88

9+
##### `__lt__`
10+
- [Comparison for less-than operation](__lt__1.py)
11+
912
##### `__format__`
1013
- [Custom string formatting](__format__1.py)
1114
- [Custom string formatting](__format__2.py)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Self
2+
3+
4+
class Fruit:
5+
def __init__(self, *, name: str, grams: float) -> None:
6+
self.name = name
7+
self.grams = grams
8+
9+
def __lt__(self, other: Self) -> bool:
10+
return self.grams < other.grams
11+
12+
13+
def main() -> None:
14+
f1: Fruit = Fruit(name='Apple', grams=100)
15+
f2: Fruit = Fruit(name='Banana', grams=150)
16+
17+
print(f1 > f2) # False
18+
print(f2 > f1) # True
19+
20+
21+
if __name__ == '__main__':
22+
main()

0 commit comments

Comments
 (0)