-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdunder.py
More file actions
107 lines (81 loc) · 3.41 KB
/
Copy pathdunder.py
File metadata and controls
107 lines (81 loc) · 3.41 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Dunder методы, их имена начинаются и заканчиваются двойным подчеркиванием
__init__ по умолчанию не ждёт аргументы
__repr__ для прогеров, возвращает строку, по которой видно и можно воссоздать состояние объекта
__str__ для людей, возвращает строку для удобного чтения людьми. По умолчанию возвращает результат __repr__
__lt__, __gt__, __le__, __qe__, __eq__ - методы сравнения
__contains__ - определяет вхождение в коллекцию
__bool__ по умолчанию True
"""
class Banknote:
"""
Банкнота
"""
def __init__(self, value: int):
self.value = value
def __repr__(self) -> str:
return f'Banknote({self.value})'
def __str__(self) -> str:
return f'Банкнота номиналом {self.value} рублей'
def __eq__(self, other) -> bool:
# if other is None or not isinstance(other, Banknote):
# return False
return self.value == other.value if not (other is None or not isinstance(other, Banknote)) else False
def __lt__(self, other) -> bool:
return self.value < other.value if not (other is None or not isinstance(other, Banknote)) else False
def __gt__(self, other) -> bool:
return self.value > other.value if not (other is None or not isinstance(other, Banknote)) else False
def __le__(self, other) -> bool:
return self.value <= other.value if not (other is None or not isinstance(other, Banknote)) else False
def __ge__(self, other):
return self.value >= other.value if not (other is None or not isinstance(other, Banknote)) else False
# class Iterator:
# def __init__(self, container):
# self.container = container
# self.index = 0
#
# def __next__(self):
# while 0 <= self.index < len(self.container):
# value = self.container[self.index]
# self.index += 1
# return value
# raise StopIteration
class Wallet:
"""
Кошелёк
"""
def __init__(self, *banknotes: Banknote):
self.container: list = []
self.container.extend(banknotes)
self.index = 0
def __repr__(self):
return f'Wallet({self.container})'
def __contains__(self, item):
return item in self.container
def __bool__(self):
return len(self.container) > 0
def __len__(self) -> int:
return len(self.container)
def __call__(self, *args, **kwargs):
return f'{sum(e.value for e in self.container)} рублей'
# def __iter__(self):
# return Iterator(self.container)
def __getitem__(self, item: int):
if not (0 <= item < len(self.container)):
raise IndexError
return self.container[item]
def __setitem__(self, key: int, value: Banknote):
if not (0 <= key < len(self.container)):
raise IndexError
self.container[key] = value
if __name__ == '__main__':
banknote = Banknote(50)
fifty = Banknote(50)
hundred = Banknote(100)
wallet = Wallet(fifty, hundred, hundred)
print(Banknote(500) in wallet)
print(len(wallet))
wallet[0] = Banknote(500)
print(wallet[2])
for money in wallet:
print(money)