-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path4_monostate.py
58 lines (41 loc) · 1.09 KB
/
4_monostate.py
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
# all members are static :)
class CEO:
__shared_state = {
'name': 'Steve',
'age': 55
}
def __init__(self):
self.__dict__ = self.__shared_state
def __str__(self):
return f'{self.name} is {self.age} years old'
class Monostate:
_shared_state = {}
def __new__(cls, *args, **kwargs):
obj = super(Monostate, cls).__new__(cls, *args, **kwargs)
obj.__dict__ = cls._shared_state
return obj
class CFO(Monostate):
def __init__(self):
self.name = ''
self.money_managed = 0
def __str__(self):
return f'{self.name} manages ${self.money_managed}bn'
if __name__ == '__main__':
ceo1 = CEO()
print(ceo1)
ceo1.age = 66
ceo2 = CEO()
ceo2.age = 77
print(ceo1)
print(ceo2)
ceo2.name = 'Tim'
ceo3 = CEO()
print(ceo1, ceo2, ceo3)
cfo1 = CFO()
cfo1.name = 'Sheryl'
cfo1.money_managed = 1
print(cfo1)
cfo2 = CFO()
cfo2.name = 'Ruth'
cfo2.money_managed = 10
print(cfo1, cfo2, sep='\n')