-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttributeReadAccessor.py
51 lines (41 loc) · 1.31 KB
/
AttributeReadAccessor.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
# Example 1
class Person_1:
def __getattr__(self, name):
alt_name = '_'+name
print(f'Could not find {name}, trying {alt_name}...')
try:
return super().__getattribute__(alt_name)
except AttributeError:
raise AttributeError(f'Could not find {name} or {alt_name}')
p_1 = Person_1()
try:
p_1.age
except AttributeError as ex:
print(type(ex).__name__, ex)
# Example 2
class Person_2:
def __init__(self, age):
self._age = age
def __getattr__(self, name):
alt_name = '_' + name
print(f'Could not find {name}, trying {alt_name}...')
try:
return super().__getattribute__(alt_name)
except AttributeError:
raise AttributeError(f'Could not find {name} or {alt_name}')
p_2 = Person_2(22)
print(p_2.age)
# Example_3
class DefaultClass:
def __init__(self, attribute_default=None):
self._attribute_default = attribute_default
def __getattr__(self, name):
print(f'{name} not found, creating it and setting it to default...')
setattr(self, name, self._attribute_default)
return self._attribute_default
class Person_3(DefaultClass):
def __init__(self, name):
super().__init__('Unavailable')
self.name = name
p3 = Person_3('Raymond')
print(p3.age)