-
Notifications
You must be signed in to change notification settings - Fork 0
/
DP_observer.py
executable file
·58 lines (48 loc) · 1.65 KB
/
DP_observer.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
class GlobalWealth(object):
def __init__(self):
self._global_wealth = 10.0
self._observers = []
def get_wealth(self):
return self._global_wealth
def set_wealth(self, value):
self._global_wealth = value
for callback in self._observers:
print('anouncing change')
callback(self._global_wealth)
'''
property() can be used in case you made a variable from x to __x and and still want people to use it
Good for backward compatibility.
Eg. x = property(new_getter_for__x, new_setter_for__x)
Therefore classname.x = 10, this will call new_setter_for__x(10) and set __x
Alternatively we can use
@property
def temperature(self):
print("Getting value")
return self._temperature
@temperature.setter
def temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = value
'''
global_wealth = property(get_wealth, set_wealth)
def bind_to(self, callback):
print('bound')
self._observers.append(callback)
class Person(object):
def __init__(self, data):
self.wealth = 1.0
self.data = data
self.data.bind_to(self.update_how_happy)
self.happiness = self.wealth / self.data.global_wealth
def update_how_happy(self, global_wealth):
self.happiness = self.wealth / global_wealth
def __del__(self):
print("Deleting")
if __name__ == '__main__':
data = GlobalWealth()
p = Person(data)
print(p.happiness)
data.global_wealth = 1.0
print(p.happiness)