-
Notifications
You must be signed in to change notification settings - Fork 1
/
descriptors.out
358 lines (357 loc) · 10.3 KB
/
descriptors.out
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
Python 3.2.3 (default, Jun 17 2012, 21:56:28)
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(NonInteractiveFileReadingPresentationConsole)
>>> ###
>>> # Discovering Descriptors
>>> #
>>> # 6th July
>>> #
>>> # Peter Inglesby
>>> # @inglesp / [email protected]
>>> #
>>> # https://github.com/inglesp/Discovering-Descriptors
>>> ###
>>>
>>> # Goals:
>>> # * explain the descriptor protocol
>>> # * demonstrate how descriptors are used in implementation of Python
>>> # * provide examples of how descriptors can be used in your own code
>>>
>>> # Let's get going
>>> class C:
... class_attr = 123
... def __init__(self):
... self.instance_attr = 456
...
>>> i = C()
>>> i.instance_attr
456
>>> i.class_attr
123
>>>
>>> # What happens when we look up an attribute on an instance?
>>> i.__dict__
{'instance_attr': 456}
>>> C.__dict__
dict_proxy({'__module__': '__console__', 'class_attr': 123, '__dict__': <attribute '__dict__' of 'C' objects>, '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None, '__init__': <function __init__ at 0x100538af0>})
>>>
>>> # Where do descriptors come in?
>>> class RandomAttribute:
... def __init__(self, n):
... self.n = n
... def __get__(self, inst, cls):
... import random
... print("calling __get__ on instance of RandomAttribute")
... return random.randint(1, self.n)
...
>>> class C:
... random_attr = RandomAttribute(10)
...
>>> i = C()
>>> i.__dict__
{}
>>> C.__dict__
dict_proxy({'__dict__': <attribute '__dict__' of 'C' objects>, '__module__': '__console__', '__weakref__': <attribute '__weakref__' of 'C' objects>, 'random_attr': <__console__.RandomAttribute object at 0x100566990>, '__doc__': None})
>>> i.random_attr
calling __get__ on instance of RandomAttribute
8
>>> i.random_attr
calling __get__ on instance of RandomAttribute
1
>>> i.random_attr
calling __get__ on instance of RandomAttribute
2
>>>
>>> # A descriptor is an object that defines at least one of:
>>> # * __get__()
>>> # * __set__()
>>> # * __delete__()
>>>
>>> # How about something more interesting?
>>> class CachedAttribute:
... def __init__(self, name, func):
... self.name = name
... self.func = func
... def __get__(self, inst, cls):
... print("calling __get__ on instance of CachedAttribute")
... inst.__dict__[self.name] = self.func(inst)
... return inst.__dict__[self.name]
...
>>> class C:
... def expensive_calculation(self):
... import time
... time.sleep(5)
... return 42
... attr = CachedAttribute('attr', expensive_calculation)
...
>>> i = C()
>>> i.__dict__
{}
>>> i.attr
calling __get__ on instance of CachedAttribute
42
>>> i.__dict__
{'attr': 42}
>>> i.attr
42
>>>
>>> # One more (useless) example
>>> class NoisyDescriptor:
... def __get__(self, inst, cls):
... print("calling __get__ on instance of NoisyDescriptor")
... print(" self:", self)
... print(" inst:", inst)
... print(" cls: ", cls)
...
>>> class C:
... attr = NoisyDescriptor()
...
>>> i = C()
>>> i.attr
calling __get__ on instance of NoisyDescriptor
self: <__console__.NoisyDescriptor object at 0x100576890>
inst: <__console__.C object at 0x100576990>
cls: <class '__console__.C'>
>>> i
<__console__.C object at 0x100576990>
>>> C.attr
calling __get__ on instance of NoisyDescriptor
self: <__console__.NoisyDescriptor object at 0x100576890>
inst: None
cls: <class '__console__.C'>
>>>
>>> # Some motivation
>>> class C:
... def a_method(self):
... print("called a method on instance %s" % id(self))
...
>>> i = C()
>>> i.a_method()
called a method on instance 4300696336
>>> C.a_method(i)
called a method on instance 4300696336
>>> i.a_method
<bound method C.a_method of <__console__.C object at 0x100576b10>>
>>> C.a_method
<function a_method at 0x100582738>
>>> i.__dict__
{}
>>> C.__dict__['a_method']
<function a_method at 0x100582738>
>>> C.__dict__['a_method'].__get__(i, C)
<bound method C.a_method of <__console__.C object at 0x100576b10>>
>>> C.__dict__['a_method'].__get__(None, C)
<function a_method at 0x100582738>
>>>
>>> # What about setting attributes?
>>> class C:
... class_attr = 123
... def __init__(self):
... self.instance_attr = 456
...
>>> i = C()
>>> i.__dict__
{'instance_attr': 456}
>>> C.__dict__
dict_proxy({'__module__': '__console__', 'class_attr': 123, '__dict__': <attribute '__dict__' of 'C' objects>, '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None, '__init__': <function __init__ at 0x100582a68>})
>>> i.instance_attr = 321
>>> i.__dict__
{'instance_attr': 321}
>>> i.class_attr = 654
>>> i.__dict__
{'instance_attr': 321, 'class_attr': 654}
>>> C.__dict__
dict_proxy({'__module__': '__console__', 'class_attr': 123, '__dict__': <attribute '__dict__' of 'C' objects>, '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None, '__init__': <function __init__ at 0x100582a68>})
>>>
>>> # A useless example
>>> class NoisyDescriptor:
... def __set__(self, inst, val):
... print("calling __set__ on instance of NoisyDescriptor")
... print(" self:", self)
... print(" inst:", inst)
... print(" val: ", val)
...
>>> class C:
... attr = NoisyDescriptor()
...
>>> i = C()
>>> i.attr = 123
calling __set__ on instance of NoisyDescriptor
self: <__console__.NoisyDescriptor object at 0x100576c50>
inst: <__console__.C object at 0x100576e10>
val: 123
>>> C.attr = 123
>>> C.__dict__
dict_proxy({'__dict__': <attribute '__dict__' of 'C' objects>, '__module__': '__console__', '__weakref__': <attribute '__weakref__' of 'C' objects>, 'attr': 123, '__doc__': None})
>>>
>>> # A less useless example
>>> class TypedAttribute:
... def __init__(self, name, typ):
... self.name = name
... self.typ = typ
... def __set__(self, inst, val):
... if isinstance(val, self.typ):
... inst.__dict__[self.name] = val
... else:
... raise TypeError
...
>>> class Person:
... name = TypedAttribute('name', str)
... age = TypedAttribute('age', int)
...
>>> i = Person()
>>> i.name = 'Bill'
>>> i.age = 25
>>> i.__dict__
{'age': 25, 'name': 'Bill'}
>>> i.age = '25'
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 9, in __set__
TypeError
>>> i.__dict__['age'] = 'definitely not a number'
>>> i.age
'definitely not a number'
>>>
>>> # Slots
>>> class C:
... __slots__ = ['x', 'y']
...
>>> i = C()
>>> i.x = 123
>>> i.x
123
>>> i.y = 456
>>> i.y
456
>>> i.z = 789
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'C' object has no attribute 'z'
>>> i.__dict__
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'C' object has no attribute '__dict__'
>>> C.__dict__
dict_proxy({'y': <member 'y' of 'C' objects>, 'x': <member 'x' of 'C' objects>, '__module__': '__console__', '__slots__': ['x', 'y'], '__doc__': None})
>>> C.__dict__['x'].__get__(i, C)
123
>>> i.x
123
>>> C.__dict__['x'].__set__(i, 789)
>>> i.x
789
>>>
>>> # What are slots for?
>>> # * Efficiency!
>>> # * Faster access
>>> # * Lower occupancy
>>>
>>> # Properties
>>> class C:
... def __init__(self, x):
... self._x = x
... def get_x(self):
... print('getting x')
... return self._x
... def set_x(self, x):
... print('setting x')
... self._x = x
... x = property(get_x, set_x)
...
>>> i = C(123)
>>> i.__dict__
{'_x': 123}
>>> i.x
getting x
123
>>> i.x = 456
setting x
>>> i.__dict__
{'_x': 456}
>>> C.__dict__['x']
<property object at 0x10057daa0>
>>> C.__dict__['x'].__get__(i, C)
getting x
456
>>> i.x
getting x
456
>>> C.__dict__['x'].__set__(i, 789)
setting x
>>> i.x
getting x
789
>>>
>>> # A more useful example
>>> class MarsProbe(object):
... def get_distance_meters(self):
... return self._distance_meters
... def set_distance_meters(self, dist):
... self._distance_meters = dist * 1.0
... def get_distance_yards(self):
... return self._distance_meters / 0.9144
... def set_distance_yards(self, dist):
... self._distance_meters = dist * 0.9144
... distance_meters = property(get_distance_meters, set_distance_meters)
... distance_yards = property(get_distance_yards, set_distance_yards)
...
>>> i = MarsProbe()
>>> i.distance_yards = 120
>>> i.distance_meters
109.728
>>>
>>> # How to choose between descriptors and properties?
>>> # * Properties work best when they know about the class
>>> # * Descriptors are more general, can often apply to any class
>>> # * Use descriptors if behaviour is different for classes and instances
>>> # * Properties are syntactic sugar
>>> # * Experiment!
>>>
>>> # What next?
>>> # * Read
>>> # - Data Model reference
>>> # - 'Descriptor HowTo Guide'
>>> # - 'Unifying types and classes in Python 2.2'
>>> # - Guido's History of Python blog
>>> # * Read code
>>> # - Lots of good examples in Django, such as related objects
>>> # - Hyrbid attributes in SQLAlchemy
>>> # - Tools/demo/eiffel.py
>>> # - $ grep __get__ site_packages
>>> # * Play
>>> # - Implement methods, __slots__, properties in pure Python
>>> # - Tinker with the CPython source (grep for 'tp_descr_get')
>>> # - Work out why C.__dict__ has an attribute called '__dict__'
>>>
>>> # Finally:
>>> # * Ignore everything I've just said
>>>
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>
>>> # Thanks!
>>>
>>>
>>>
>>>