@@ -26,35 +26,24 @@ class MyClass(object):
26
26
x = 4
27
27
28
28
def __init__ (self ):
29
- self .y = 5
29
+ self .x = 5
30
30
31
31
def __getattribute__ (self , item ):
32
- print ('MyClass.__getattribute__' )
32
+ print ('MyClass.__getattribute__' , item )
33
33
return super (MyClass , self ).__getattribute__ (item )
34
- # return object.__getattribute__(self, item)
35
34
36
35
def __getattr__ (self , item ):
37
- print ('MyClass.__getattr__' )
36
+ print ('MyClass.__getattr__' , item )
38
37
raise AttributeError (f'{ type (self ).__name__ } object had not attribute \' { item } \' ' )
39
38
40
39
def __setattr__ (self , key , value ):
41
- print ('MyClass.__setattr__' )
40
+ print ('MyClass.__setattr__' , key )
42
41
# self.key = value # 错误
43
42
self .__dict__ [key ] = value
44
43
45
- print ('开始实例化' )
46
44
cls = MyClass ()
47
- print ('--类属性访问--' )
48
- print (MyClass .x )
49
- print ('--类属性赋值--' )
50
- MyClass .x = 1
51
- print ('--实例属性访问--' )
52
- print (cls .y )
53
- print ('--实例属性赋值--' )
54
- cls .y = 1
55
- print ('--访问不存在的属性--' )
56
- print (MyClass .k )
57
- print (cls .k )
45
+ print (cls .__dict__ )
46
+ print (cls .__class__ .__dict__ )
58
47
59
48
60
49
def demo_3 ():
@@ -172,5 +161,145 @@ def fpp():
172
161
print (fpp .__get__ (A )()) # 类似于fpp(A)
173
162
174
163
164
+
165
+
166
+ def demo_8 ():
167
+
168
+ class Property (object ):
169
+ "Emulate PyProperty_Type() in Objects/descrobject.c"
170
+
171
+ def __init__ (self , fget = None , fset = None , fdel = None , doc = None ):
172
+ print ('-----' )
173
+ print ('fget' , fget )
174
+ print ('fset' , fset )
175
+ print ('fdel' , fdel )
176
+ print ('-----' )
177
+ self .fget = fget
178
+ self .fset = fset
179
+ self .fdel = fdel
180
+ if doc is None and fget is not None :
181
+ doc = fget .__doc__
182
+ self .__doc__ = doc
183
+
184
+ def __get__ (self , obj , objtype = None ):
185
+ print ('Property.__get__' )
186
+ if obj is None :
187
+ return self
188
+ if self .fget is None :
189
+ raise AttributeError ("unreadable attribute" )
190
+ return self .fget (obj )
191
+
192
+ def __set__ (self , obj , value ):
193
+ print ('Property.__set__' ,obj )
194
+ if self .fset is None :
195
+ raise AttributeError ("can't set attribute" )
196
+ self .fset (obj , value )
197
+
198
+ def __delete__ (self , obj ):
199
+ print ('Property.__delete__' )
200
+ if self .fdel is None :
201
+ raise AttributeError ("can't delete attribute" )
202
+ self .fdel (obj )
203
+
204
+ def getter (self , fget ):
205
+ print ('Property.getter' )
206
+ return type (self )(fget , self .fset , self .fdel , self .__doc__ )
207
+
208
+ def setter (self , fset ):
209
+ print ('Property.setter' )
210
+ return type (self )(self .fget , fset , self .fdel , self .__doc__ )
211
+
212
+ def deleter (self , fdel ):
213
+ print ('Property.deleter' )
214
+ return type (self )(self .fget , self .fset , fdel , self .__doc__ )
215
+
216
+ class C (object ):
217
+
218
+
219
+ def __init__ (self ):
220
+ self ._x = None
221
+
222
+ @Property
223
+ def x (self ):
224
+ print ('C.property' )
225
+ return self ._x
226
+
227
+ @x .setter
228
+ def x (self , value ):
229
+ print ('C.setter' )
230
+ self ._x = value
231
+
232
+ @x .deleter
233
+ def x (self ):
234
+ print ('C.deleter' )
235
+ del self ._x
236
+
237
+ c = C ()
238
+ print (C .__dict__ ['x' ])
239
+ print (c .__dict__ )
240
+ c .x = 8
241
+ print (c .x )
242
+ del c .x
243
+
244
+
245
+ class C (object ):
246
+ def __init__ (self ):
247
+ self ._x = None
248
+
249
+
250
+ def getx (self ):
251
+ print ('C.property' )
252
+ return self ._x
253
+
254
+
255
+ def setx (self , value ):
256
+ print ('C.setter' )
257
+ self ._x = value
258
+
259
+
260
+ def delx (self ):
261
+ print ('C.deleter' )
262
+ del self ._x
263
+
264
+ x = Property (getx ,setx ,delx )
265
+
266
+ c = C ()
267
+ c .x = 8
268
+ print (c .x )
269
+ del c .x
270
+
271
+
272
+ # 模拟显示过程
273
+ class C (object ):
274
+
275
+
276
+ def __init__ (self ):
277
+ self ._x = None
278
+
279
+ def getx (self ):
280
+ print ('C.property' )
281
+ return self ._x
282
+
283
+
284
+ def setx (self , value ):
285
+ print ('C.setter' )
286
+ self ._x = value
287
+
288
+
289
+ def delx (self ):
290
+ print ('C.deleter' )
291
+ del self ._x
292
+
293
+ x_get = Property (C .getx )
294
+ x_set = x_get .setter (C .setx )
295
+ x_del = x_set .deleter (C .delx )
296
+
297
+ x_set .__set__ (C (),8 )
298
+ print (x_get .__get__ (C ()))
299
+ x_del .__delete__ (C ())
300
+
301
+
175
302
if __name__ == '__main__' :
176
- demo_7 ()
303
+ # demo_2()
304
+ demo_8 ()
305
+
0 commit comments