Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/python/design_getattribute_example1.py
1240 views
1
2
# Override access to one variable in a class, but return all others normally
3
4
class D(object):
5
def __init__(self):
6
self.test = 20
7
self.test2 = 40
8
def __getattribute__(self, name):
9
if name == 'test':
10
return 0
11
else:
12
return self.__dict__[name]
13
14
# The above wont work.
15
# This will give
16
# RuntimeError: maximum recursion depth exceeded in cmp
17
# Look at getattribute_example2.py for correct solution.
18
19
obj1 = D()
20
print(obj1.test)
21
print(obj1.test2)
22
23
24