forked from Lion-oss123/python-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputedproperty.py
More file actions
38 lines (26 loc) · 749 Bytes
/
computedproperty.py
File metadata and controls
38 lines (26 loc) · 749 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
ng computed property and setting
class operators(object):
def __getattr__(self, name):
if name == 'age':
return 40
else:
raise AttributeError(name)
def __setattr__(self, name, value):
print('set: %s %s' % (name, value))
if name == 'age':
self.__dict__['_age'] = value
else:
self.__dict__[name] = value
# OR BETTER WAY
class properties(object):
def getage(self):
return 40
def setage(self, value):
self._age = value
age = property(getage, setage, None, None)
if __name__ == '__main__':
x, y = operators(), properties()
for ins in (x, y):
print(ins.age)
ins.age = 20
print(ins._age)