-
Notifications
You must be signed in to change notification settings - Fork 8
/
mod_03_mro_and_super.py
573 lines (433 loc) · 15.5 KB
/
mod_03_mro_and_super.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#-*- coding: utf-8 -*-
###
# MODULE 03: MRO: super and getattr
###
# Let's implement a verbose dict intercepting attribs and items access
class VerboseDict(dict):
def __getattribute__(self, name):
print "__getattribute__", name
return dict.__getattribute__(self, name)
def __getitem__(self, key):
print "__getitem__", key
return dict.__getitem__(self, key)
def __setitem__(self, key, value):
print "__setitem__", key, value
return dict.__setitem__(self, key, value)
def __getattr__(self, name):
print "__getattr__", name
return dict.__getattr__(self, name)
def __setattr__(self, name, value):
print "__setattr__", name, value
return dict.__setattr__(self, name, value)
# Let's try it
vd = VerboseDict({'a': 1, 'b': 2})
vd['c'] = 3
vd.x = 7
vd.a = 0
vd.a
vd
#===============================================================================
# What if we want to merge this behavior with our AttrDict?
#===============================================================================
class AttrDict(dict):
def __getattr__(self, name):
try:
return self[name]
except KeyError, e:
raise AttributeError(e)
def __setattr__(self, name, value):
if name in self:
self[name] = value
else:
dict.__setattr__(self, name, value)
def __delattr__(self, name):
if name in self:
del self[name]
else:
dict.__delattr__(self, name)
# Option 1: new class inheriting from both
class VerboseAttrDict(VerboseDict, AttrDict):
pass
# Let's use this dict
vd = VerboseAttrDict({'a': 1, 'b': 2})
vd['c'] = 3
vd.x = 7
vd.a = 0
vd.a
vd
# Option 2: new class inheriting from both (change the order)
class VerboseAttrDict(AttrDict, VerboseDict):
pass
# Let's use this dict
vd = VerboseAttrDict({'a': 1, 'b': 2})
vd['c'] = 3
vd.x = 7
vd.a = 0
vd.a
vd
# Notice how ancestors (superclasses) order when inheriting matters!
# Option 3: implement new class with both verbose and attr behavior --> new code
# Option 4: change VerboseDict to inherit from AttrDict --> change code to call the right ancestor
# ...
#===============================================================================
# The solution is 'super' and cooperative methods
# - super(type[, object-or-type])
# - Typically: super(CurrentClass, self).current_method(*current_args, **current_kwargs)
# - Returns a proxy object that delegates method calls to a parent or sibling class of 'type'.
# - Only works for new-style classes
# - Arguments are optional in Py3k
#
# - http://docs.python.org/2/library/functions.html#super
#===============================================================================
# Let's implement verbose dict with super
class VerboseDict(dict):
def __getattribute__(self, name):
print "__getattribute__", name
return super(VerboseDict, self).__getattribute__(name)
def __getitem__(self, key):
print "__getitem__", key
return super(VerboseDict, self).__getitem__(key)
def __setitem__(self, key, value):
print "__setitem__", key, value
return super(VerboseDict, self).__setitem__(key, value)
def __getattr__(self, name):
print "__getattr__", name
return super(VerboseDict, self).__getattr__(name)
def __setattr__(self, name, value):
print "__setattr__", name, value
return super(VerboseDict, self).__setattr__(name, value)
# Let's implement attribs dict with super
class AttrDict(dict):
def __getattr__(self, name):
try:
return self[name]
except KeyError, e:
raise AttributeError(e)
def __setattr__(self, name, value):
if name in self:
# super(AttrDict, self).__setitem__(name, value)
# self.__setitem__(name, value)
self[name] = value
else:
super(AttrDict, self).__setattr__(name, value)
def __delattr__(self, name):
if name in self:
del self[name]
else:
super(AttrDict, self).__delattr__(name)
# Option 1: new class inheriting from both... wiht super!!
class VerboseAttrDict(VerboseDict, AttrDict):
pass
# Let's use this dict
vd = VerboseAttrDict({'a': 1, 'b': 2})
vd['c'] = 3
vd.x = 7
vd.a = 0
print vd.a
print vd
# Option 4: change VerboseDict to inherit from AttrDict --> with super no changes are required
class VerboseDict(AttrDict):
def __getattribute__(self, name):
print "__getattribute__", name
return super(VerboseDict, self).__getattribute__(name)
def __getitem__(self, key):
print "__getitem__", key
return super(VerboseDict, self).__getitem__(key)
def __setitem__(self, key, value):
print "__setitem__", key, value
return super(VerboseDict, self).__setitem__(key, value)
def __getattr__(self, name):
print "__getattr__", name
return super(VerboseDict, self).__getattr__(name)
def __setattr__(self, name, value):
print "__setattr__", name, value
return super(VerboseDict, self).__setattr__(name, value)
# Let's use this dict
vd = VerboseDict({'a': 1, 'b': 2})
vd['c'] = 3
vd.x = 7
vd.a = 0
vd.a
vd
#===============================================================================
# - super is an implicit ancestors' methods invocation mechanism
# - super is the key of Guido's "cooperative super call" pattern
# - aka. "call-next-method"
#
# - http://www.python.org/download/releases/2.2.3/descrintro/#cooperation
#===============================================================================
# Let's see in detail how super and MRO work
class A(object):
def method(self):
print type(self), "A's method"
class B(A):
def method(self):
print type(self), "B's method"
super(B, self).method()
class C(A):
def method(self):
print type(self), "C's method"
super(C, self).method()
class D(B, C):
def method(self):
print type(self), "D's method"
super(D, self).method()
#==============================================================================
# We have a diamond inheritance schema:
#
# A
# / \
# B C
# \ /
# D
#==============================================================================
# Let's instantiate D and call the method
d_inst = D()
d_inst.method()
# Another example
class A2(object):
def method(self):
print type(self), "A2's method"
class C2(A2):
def method(self):
print type(self), "C2's method"
super(C2, self).method()
class B2(C2):
def method(self):
print type(self), "B2's method"
super(B2, self).method()
class D2(B2):
def method(self):
print type(self), "D2's method"
super(D2, self).method()
#==============================================================================
# We have a linear inheritance schema:
#
# A2
# |
# C2
# |
# B2
# |
# D2
#==============================================================================
# Let's instantiate D2 and call the method again
d_inst = D2()
d_inst.method()
# Let's check its Method Resolution Order (MRO)
print D.__mro__
print D2.__mro__
#===============================================================================
# Let's see how 'super' works step by step:
#
# 0- The MRO is always obtained from the class of 'self'
#
# 1- In D's method, super(D, self).method() will find and call B.method(self),
# since B is the first base class following D in D.__mro__ which defines method.
# - If method was not defined in B the process would continue to next base class.
#
# 2- In B.method, super(B, self).method() is called. Since self is a D instance,
# the MRO is (D, B, C, A, object) but as we are in B, the next base class is C.
#
# 3- In C again method is declared so it is called, and in turn it calls super(C, self).method().
#
# 4- Still using the same MRO, we see that the class following C is A, so A.method is called.
# No super call is made at this point and the chain of invocations finishes.
#===============================================================================
#===============================================================================
# - This process ensures that all ancestors of a class are always inspected
# before the ancestors of its ancestors
# - The same process applies when a class declaration is empty, like our fist
# implementation of VerboseAttrDict
#
# - http://www.python.org/download/releases/2.2.3/descrintro/#mro
#===============================================================================
# Let's see what happens with attributes (getattr)
class A3(object):
class_attr = "A3"
class B3(A3):
pass
class C3(A3):
class_attr = "C3"
class D3(B3, C3):
pass
#==============================================================================
# We have a diamond inheritance schema:
#
# A3 (class_attr = "A3")
# / \
# B3 C3 (class_attr = "C3")
# \ /
# D3
#==============================================================================
# Let's instantiate D and call the method again
d_inst = D3()
d_inst.class_attr
#===============================================================================
# MRO was changed in 2.2 (and 2.3) because with new-style all classes inherit
# from 'object', so suddenly diamond diagrams would appear:
#
# - old-tyle multiple inheritance diagram:
#
# B C
# \ /
# D
#
#
# - the same implementation with new-tyle:
#
# object
# / \
# B C
# \ /
# D
#
# - With the old (wrong) MRO, it would be: D, B, object, C, object!!
# - With the new one it is: D, B, C, object
#===============================================================================
# Let's go back again to our verbose attribute dict...
class Verbose(object):
def __getattribute__(self, name):
print "__getattribute__", name
return super(Verbose, self).__getattribute__(name)
def __getitem__(self, key):
print "__getitem__", key
return super(Verbose, self).__getitem__(key)
def __setitem__(self, key, value):
print "__setitem__", key, value
return super(Verbose, self).__setitem__(key, value)
def __getattr__(self, name):
print "__getattr__", name
return super(Verbose, self).__getattr__(name)
def __setattr__(self, name, value):
print "__setattr__", name, value
return super(Verbose, self).__setattr__(name, value)
class Attr(object):
def __getattr__(self, name):
try:
return super(Attr, self).__getitem__(name)
except KeyError, e:
raise AttributeError(e)
def __setattr__(self, name, value):
if name in self:
super(Attr, self).__setitem__(name, value)
else:
super(Attr, self).__setattr__(name, value)
def __delattr__(self, name):
if name in self:
super(Attr, self).__delitem__(name)
else:
super(Attr, self).__delattr__(name)
# Option 5: exploit cooperative super call with mixins
class VerboseAttrDict(Verbose, Attr, dict):
pass
# Let's use this dict
vd = VerboseAttrDict({'a': 1, 'b': 2})
vd['c'] = 3
vd.x = 7
vd.a = 0
vd.a
print vd
print VerboseAttrDict.__mro__
##===============================================================================
##===============================================================================
## TIME TO START WORKING!
##
## EXERCISE 3:
## - Implement all needed changes to let the tests pass. In particular, implement AmazingDict:
## - Access keys as attributes only if they already exist
## - Lower attributes and key names for query or modification
## - Convert attributes or keys datetime values to strings when they are modified
## - Print all attributes and keys accesses for query or modification
## - Tip: http://docs.python.org/2/reference/datamodel.html?highlight=__contains__#object.__contains__
##
## INSTRUCTIONS:
## - Go to exercises/exercise_3 and edit exercise_3.py
## - Change the classes implementations to let tests_3.py pass
## - Check tests executing 'nosetests -sv'
##===============================================================================
##===============================================================================
# Solution step 1: implement __contains__ in Lower class
class Lower(object):
def __getattribute__(self, name):
return super(Lower, self).__getattribute__(name.lower())
def __getitem__(self, key):
return super(Lower, self).__getitem__(key.lower())
def __setitem__(self, key, value):
if isinstance(value, (str, unicode)):
value = value.lower()
return super(Lower, self).__setitem__(key.lower(), value)
def __getattr__(self, name):
return super(Lower, self).__getattr__(name.lower())
def __setattr__(self, name, value):
if isinstance(value, (str, unicode)):
value = value.lower()
return super(Lower, self).__setattr__(name.lower(), value)
def __contains__(self, item):
return super(Lower, self).__contains__(item.lower())
# Nothing to change in Attr class implementation
class Attr(object):
'''Access keys as attributes only if they already exist
'''
def __getattr__(self, name):
try:
return super(Attr, self).__getitem__(name)
except KeyError, e:
raise AttributeError(e)
def __setattr__(self, name, value):
if name in self:
super(Attr, self).__setitem__(name, value)
else:
super(Attr, self).__setattr__(name, value)
# Solution step 2: make Verbose class inherit from object
class Verbose(object):
'''Print all attributes and keys accesses for query or modification
'''
def __getattribute__(self, name):
print "__getattribute__", name
return super(Verbose, self).__getattribute__(name)
def __getitem__(self, key):
print "__getitem__", key
return super(Verbose, self).__getitem__(key)
def __setitem__(self, key, value):
print "__setitem__", key, value
return super(Verbose, self).__setitem__(key, value)
def __getattr__(self, name):
print "__getattr__", name
return super(Verbose, self).__getattr__(name)
def __setattr__(self, name, value):
print "__setattr__", name, value
return super(Verbose, self).__setattr__(name, value)
# Nothing to change in DateStr class implementation
from datetime import datetime
class DateStr(object):
def __setitem__(self, key, value):
if isinstance(value, datetime):
value = value.isoformat()
return super(DateStr, self).__setitem__(key, value)
def __setattr__(self, name, value):
if isinstance(value, datetime):
value = value.isoformat()
return super(DateStr, self).__setattr__(name, value)
# Solution step 3: setup correct order of AmazingDict class ancestorts
class AmazingDict(Attr, Lower, DateStr, Verbose, dict):
pass
# Let's check the result
d = AmazingDict({'id': 1234})
print d.iD
now = datetime.now()
d2 = AmazingDict()
d2['date'] = now
print d2.date
print type(d2.date)
print d2.date == now.isoformat()
d3 = AmazingDict({'id': 1234})
print d3
d3.ID = 1111
print d3
#===============================================================================
# MORE INFO:
# - http://docs.python.org/2/library/functions.html#super
# - http://www.python.org/download/releases/2.2.3/descrintro/#cooperation
# - http://www.python.org/download/releases/2.2.3/descrintro/#mro
#===============================================================================