-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_classmethod.py
More file actions
executable file
·46 lines (32 loc) · 1014 Bytes
/
Copy path13_classmethod.py
File metadata and controls
executable file
·46 lines (32 loc) · 1014 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Currency:
def __init__(self, rate):
self.rate = rate
class ShopItem:
items = 0
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}'
def __getattribute__(self, item):
return super().__getattribute__(item)
def __getattr__(self, item):
print(f'__getattr__: {item}')
return 'stub'
@staticmethod
def convert_currency(amount, from_currency, to_currency):
return amount * from_currency.rate / to_currency.rate
@classmethod
def create_from_currency(cls, amount, from_currency, to_currency):
return cls(
cls.convert_currency(amount, from_currency, to_currency)
)
@classmethod
def create_and_count(cls, price):
cls.items += 1
return cls(price)
class Notebook(ShopItem):
pass
class Bath(ShopItem):
pass