-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__or__1.py
More file actions
28 lines (19 loc) · 751 Bytes
/
Copy path__or__1.py
File metadata and controls
28 lines (19 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from typing import Self
class Fruit:
def __init__(self, *, name: str, grams: float) -> None:
self.name = name
self.grams = grams
def __or__(self, other: Self) -> Self:
new_name = f'{self.name} & {other.name}'
new_grams = self.grams + other.grams
return Fruit(name=new_name, grams=new_grams)
def __repr__(self) -> str:
return f'Fruit(name={self.name}, grams={self.grams})'
def main() -> None:
f1: Fruit = Fruit(name='Apple', grams=1500)
f2: Fruit = Fruit(name='Banana', grams=2000)
f3: Fruit = Fruit(name='Orange', grams=1000)
combined: Fruit = f1 | f2 | f3
print(combined) # Fruit(name=Apple & Banana & Orange, grams=4500)
if __name__ == '__main__':
main()