-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess1.py
77 lines (58 loc) · 1.75 KB
/
access1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
traceMe = False
def trace(*args):
if traceMe:
print('[' + ' '.join(map(str, args)) + ']')
def Private(*privates):
def onDecorator(aClass):
class onInstance:
def __init__(self, *args, **kwargs):
self.wrapped = aClass(*args, **kwargs)
def __getattr__(self, item):
trace('get:', item)
if item in privates:
raise TypeError('private attribute fetch: ' + item)
else:
return getattr(self.wrapped, item)
def __setattr__(self, key, value):
trace('set:', key, value)
if key == 'wrapped':
self.__dict__[key] = value
elif key in privates:
raise TypeError('private attribute change:' + key)
else:
setattr(self.wrapped, key, value)
return onInstance
return onDecorator
if __name__ == '__main__':
traceMe = True
@Private('data', 'size')
class Doubler:
def __init__(self, label, start):
self.label = label
self.data = start
def size(self):
return len(self.data)
def double(self):
for i in range(self.size()):
self.data[i] = self.data[i] * 2
def display(self):
print('{} => {}'.format(self.label, self.data))
X = Doubler('X is', [1, 2, 3])
Y = Doubler('Y is', [-10, -20, -30])
print(X.label)
X.display()
X.double()
X.display()
print(Y.label)
Y.display()
Y.double()
Y.label = 'Spam'
Y.display()
"""
print(X.size())
print(X.data)
X.data=[1,1,1]
X.size=lambda S:0
print(Y.data)
print(Y.size())
"""