-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_privacy.py
More file actions
36 lines (26 loc) · 734 Bytes
/
_privacy.py
File metadata and controls
36 lines (26 loc) · 734 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
# coding=utf-8
class PrivateExc(Exception): pass
class Privacy:
def __setattr__(self, key, value):
if key in self.privates:
raise PrivateExc(key, self)
else:
self.__dict__[key] = value
def __getattr__(self, key):
if key in self.privates:
raise PrivateExc(key, self)
else:
return self.__dict__[key]
if __name__ == '__main__':
class Test1(Privacy):
privates = ['age']
class Test2(Privacy):
privates = ['name', 'pay']
def __init__(self):
self.__dict__['name'] = 'Tom'
x = Test1()
y = Test2()
x.name = 'David'
#y.name = 'John' # ERROR
#x.age = 30 # ERROR
y.age = 50