-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttribute Lookup.py
More file actions
196 lines (115 loc) · 2.73 KB
/
Attribute Lookup.py
File metadata and controls
196 lines (115 loc) · 2.73 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# coding: utf-8
# In[1]:
class A(object):
pass
obj = A()
obj.a
# In[2]:
class A(object):
def __getattr__(self, name):
print('Getting an non-existing property on object: {}'.format(name))
return 'Some default value'
obj = A()
obj.a
# In[3]:
class A(object):
a = 123
obj = A()
obj.a
# In[4]:
# First we need to define a descriptor class
class Desc(object):
def __get__(self, instance, klass):
print('Getting value from non-data descriptor')
return 'default value defined by __get__'
class A(object):
a = Desc()
obj = A()
obj.a
# In[6]:
class A(object):
def __init__(self):
self.a = 123
obj = A()
obj.a
# In[20]:
class Desc(object):
def __init__(self, val=None):
self.val = val
def __get__(self, instance, klass):
print('Getting value from non-data descriptor')
return self.val
def __set__(self, instance, val):
print('data descriptor __set__')
self.val = val
class A(object):
a = Desc(111)
obj = A()
print('a' in A.__dict__)
print(hasattr(A.__dict__['a'], '__get__') and hasattr(A.__dict__['a'], '__set__'))
# obj.__dict__['a'] = 123
obj.a
# In[22]:
class B():
pass
B.b
# In[24]:
class Meta(type):
def __getattr__(cls, name):
print('Getting an non-existing property on Class: {}'.format(name))
return 'Some default value'
class B():
__metaclass__ = Meta
B.b
# In[26]:
class Meta(type):
b = 123
class B(object):
__metaclass__ = Meta
B.b
# In[48]:
class ClassDesc(object):
def __get__(self, cls, meta):
print('Getting value from non-data descriptor')
return 'default value defined by __get__'
class Meta(type):
b = ClassDesc()
class B(object):
__metaclass__ = Meta
B.b
# In[28]:
class B(object):
b = 123
B.b
# In[39]:
class Meta(type):
def __new__(cls, name, bases, cdict):
cdict['b'] = 1234
return type.__new__(cls, name, bases, cdict)
class B(object):
__metaclass__ = Meta
B.b
# In[50]:
class ClassDesc(object):
def __init__(self, val=None):
self.val = val
def __get__(self, cls, meta):
print('Getting value from non-data descriptor')
return self.val
def __set__(self, cls, val):
print('data descriptor __set__')
self.val = val
class Meta(type):
b = ClassDesc(1111)
def __new__(cls, name, bases, cdict):
cdict['b'] = 1234
return type.__new__(cls, name, bases, cdict)
class B(object):
__metaclass__ = Meta
print('b' in Meta.__dict__)
print(hasattr(Meta.__dict__['b'], '__get__') and hasattr(Meta.__dict__['b'], '__set__'))
B.b
# In[51]:
B.b = 1
# In[ ]:
# In[ ]: