-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_math_magic_usage.py
More file actions
executable file
·47 lines (35 loc) · 1.04 KB
/
Copy path7_math_magic_usage.py
File metadata and controls
executable file
·47 lines (35 loc) · 1.04 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
class ShopItem:
def __init__(self, price):
self._price = price
def __add__(self, other):
return ShopItem(self._price + other._price)
def __str__(self):
return f'{self.__class__.__name__}: {self._price}'
class Notebook(ShopItem):
pass
class Bath(ShopItem):
pass
class Basket:
def __init__(self):
self._items = {}
self._idx = 0
self._iter_items = []
def __setitem__(self, key, value):
try:
value._price
self._items[key] = value
except Exception as e:
raise ValueError(f'can\'t add to basket {value}: {e}')
def __getitem__(self, item):
return self._items.get(item)
def __iter__(self):
if not self._iter_items:
self._iter_items = tuple(self._items.values())
self._idx = 0
return self
def __next__(self):
if self._idx < len(self._iter_items):
self._idx += 1
return self._iter_items[self._idx - 1]
else:
raise StopIteration