-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproblem3_sol.py
51 lines (43 loc) · 1.28 KB
/
problem3_sol.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
class B:
class _B:
def __init__(self, value):
self.value = value
_instance = None
# By defining the method as static method,
# you can call the method without creating an instance of the class.
# e.g. B.get_instance()
@staticmethod
def get_instance():
if B._instance is None:
B()
return B._instance
# `__getattr__` is a magic method executed when there is no member in the instance.
# Since class B does not have `value` as its member,
# accessing `value` of an instance B will get into this __getattr__.
# e.g.,
# >> b1 = B(10)
# >> b1.value # b1.value -> no attribute in B -> b1.__getattr__(value)
def __getattr__(self, name):
return getattr(B._instance, name)
def __init__(self, value=0):
if B._instance is None:
B._instance = B._B(value)
else:
# just update value
B._instance.value = value
if __name__ == "__main__":
b1 = B(10)
print(b1.value)
print(B.get_instance())
b2 = B(20)
print(b2.value)
print(b1.value)
print(B.get_instance())
"""
Expected Output (example)
> 10
> <__main__.B._B object at 0x10cbcc210>
> 20
> 20
> <__main__.B._B object at 0x10cbcc210>
"""