summaryrefslogtreecommitdiff
path: root/pypers/all.tex
blob: e9672c4e9836ab0f229e18322cc1b9784f47cfa8 (plain)
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
\documentclass[10pt,english]{article}
\usepackage{babel}
\usepackage{shortvrb}
\usepackage[latin1]{inputenc}
\usepackage{tabularx}
\usepackage{longtable}
\setlength{\extrarowheight}{2pt}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{color}
\usepackage{multirow}
\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
\usepackage[a4paper]{geometry}
%% generator Docutils: http://docutils.sourceforge.net/
\newlength{\admonitionwidth}
\setlength{\admonitionwidth}{0.9\textwidth}
\newlength{\docinfowidth}
\setlength{\docinfowidth}{0.9\textwidth}
\newlength{\locallinewidth}
\newcommand{\optionlistlabel}[1]{\bf #1 \hfill}
\newenvironment{optionlist}[1]
{\begin{list}{}
  {\setlength{\labelwidth}{#1}
   \setlength{\rightmargin}{1cm}
   \setlength{\leftmargin}{\rightmargin}
   \addtolength{\leftmargin}{\labelwidth}
   \addtolength{\leftmargin}{\labelsep}
   \renewcommand{\makelabel}{\optionlistlabel}}
}{\end{list}}
% begin: floats for footnotes tweaking.
\setlength{\floatsep}{0.5em}
\setlength{\textfloatsep}{\fill}
\addtolength{\textfloatsep}{3em}
\renewcommand{\textfraction}{0.5}
\renewcommand{\topfraction}{0.5}
\renewcommand{\bottomfraction}{0.5}
\setcounter{totalnumber}{50}
\setcounter{topnumber}{50}
\setcounter{bottomnumber}{50}
% end floats for footnotes
% some commands, that could be overwritten in the style file.
\newcommand{\rubric}[1]{\subsection*{~\hfill {\it #1} \hfill ~}}
\newcommand{\titlereference}[1]{\textsl{#1}}
% end of "some commands"
\input{style.tex}
\title{OBJECT ORIENTED PROGRAMMING IN PYTHON}
\author{}
\date{}
\hypersetup{
pdftitle={OBJECT ORIENTED PROGRAMMING IN PYTHON},
pdfauthor={Michele Simionato}
}
\raggedbottom
\begin{document}
\maketitle

%___________________________________________________________________________
\begin{center}
\begin{tabularx}{\docinfowidth}{lX}
\textbf{Version}: &
	0.5 \\
\textbf{Author}: &
	Michele Simionato \\
\textbf{E-mail}: &
	mis6@pitt.edu \\
\textbf{Home-page}: &
	http://www.phyast.pitt.edu/~micheles/ \\
\textbf{Disclaimer}: &
	I release this book to the general public. 
It can be freely distributed if unchanged.
As usual, I don't give any warranty: while I have tried hard to ensure the
correctness of what follows, I disclaim any responsability in case of
errors . Use it at your own risk and peril ! \\
\end{tabularx}
\end{center}

\setlength{\locallinewidth}{\linewidth}



\hypertarget{contents}{}
\pdfbookmark[0]{Contents}{contents}
\subsection*{~\hfill Contents\hfill ~}
\begin{list}{}{}
\item {} \href{\#preface}{Preface}
\begin{list}{}{}
\item {} \href{\#the-philosophy-of-this-book}{The philosophy of this book}

\item {} \href{\#for-who-this-book-in-intended}{For who this book in intended}

\item {} \href{\#about-the-scripts-in-this-book}{About the scripts in this book}

\item {} \href{\#conventions-used-in-this-book}{Conventions used in this book}

\end{list}

\item {} \href{\#introduction}{Introduction}
\begin{list}{}{}
\item {} \href{\#why-oop}{Why OOP ?}

\item {} \href{\#why-python}{Why Python ?}

\item {} \href{\#further-thoughts}{Further thoughts}

\end{list}

\item {} \href{\#first-things-first}{FIRST THINGS, FIRST}
\begin{list}{}{}
\item {} \href{\#what-s-an-object}{What's an object?}

\item {} \href{\#objects-and-classes}{Objects and classes}

\item {} \href{\#objects-have-attributes}{Objects have attributes}

\item {} \href{\#objects-have-methods}{Objects have methods}

\item {} \href{\#summing-objects}{Summing objects}

\item {} \href{\#inspecting-objects}{Inspecting objects}

\item {} \href{\#built-in-objects-iterators-and-generators}{Built-in objects: iterators and generators}

\end{list}

\item {} \href{\#the-convenience-of-functions}{THE CONVENIENCE OF FUNCTIONS}
\begin{list}{}{}
\item {} \href{\#id16}{Introduction}

\item {} \href{\#a-few-useful-functions}{A few useful functions}

\item {} \href{\#functions-are-objects}{Functions are objects}

\item {} \href{\#profiling-functions}{Profiling functions}

\item {} \href{\#about-python-speed}{About Python speed}

\item {} \href{\#tracing-functions}{Tracing functions}

\item {} \href{\#tracing-objects}{Tracing objects}

\item {} \href{\#inspecting-functions}{Inspecting functions}

\end{list}

\item {} \href{\#the-beauty-of-objects}{THE BEAUTY OF OBJECTS}
\begin{list}{}{}
\item {} \href{\#user-defined-objects}{User defined objects}

\item {} \href{\#objects-have-static-methods-and-classmethods}{Objects have static methods and classmethods}

\item {} \href{\#objects-have-their-privacy}{Objects have their privacy}

\item {} \href{\#objects-have-properties}{Objects have properties}

\item {} \href{\#objects-have-special-methods}{Objects have special methods}

\item {} \href{\#objects-can-be-called-added-subtracted}{Objects can be called, added, subtracted, ...}

\end{list}

\item {} \href{\#the-power-of-classes}{THE POWER OF CLASSES}
\begin{list}{}{}
\item {} \href{\#the-concept-of-inheritance}{The concept of inheritance}

\item {} \href{\#inheritance-versus-run-time-class-modifications}{Inheritance versus run-time class modifications}

\item {} \href{\#inheriting-from-built-in-types}{Inheriting from built-in types}

\item {} \href{\#controlling-the-creation-of-objects}{Controlling the creation of objects}

\item {} \href{\#multiple-inheritance}{Multiple Inheritance}

\item {} \href{\#cooperative-hierarchies}{Cooperative hierarchies}

\item {} \href{\#inheritance-and-privacy}{Inheritance and privacy}

\end{list}

\item {} \href{\#the-sophistication-of-descriptors}{THE SOPHISTICATION OF DESCRIPTORS}
\begin{list}{}{}
\item {} \href{\#motivation}{Motivation}

\item {} \href{\#functions-versus-methods}{Functions versus methods}

\item {} \href{\#methods-versus-functions}{Methods versus functions}

\item {} \href{\#static-methods-and-class-methods}{Static methods and class methods}

\item {} \href{\#properties}{Properties}

\item {} \href{\#user-defined-attribute-descriptors}{User-defined attribute descriptors}

\item {} \href{\#data-descriptors}{Data descriptors}

\item {} \href{\#the-super-attribute-descriptor}{The \texttt{super} attribute descriptor}

\item {} \href{\#method-wrappers}{Method wrappers}

\end{list}

\item {} \href{\#the-subtleties-of-multiple-inheritance}{THE SUBTLETIES OF MULTIPLE INHERITANCE}
\begin{list}{}{}
\item {} \href{\#a-little-bit-of-history-why-python-2-3-has-changed-the-mro}{A little bit of history: why Python 2.3 has changed the MRO}

\item {} \href{\#the-c3-method-resolution-order}{The C3 Method Resolution Order}

\item {} \href{\#examples}{Examples}

\item {} \href{\#bad-method-resolution-orders}{Bad Method Resolution Orders}

\item {} \href{\#understanding-the-method-resolution-order}{Understanding the Method Resolution Order}

\item {} \href{\#counting-instances}{Counting instances}

\item {} \href{\#the-pizza-shop-example}{The pizza-shop example}

\item {} \href{\#fixing-wrong-hierarchies}{Fixing wrong hierarchies}

\item {} \href{\#modifying-hierarchies}{Modifying hierarchies}

\item {} \href{\#inspecting-python-code}{Inspecting Python code}

\end{list}

\item {} \href{\#the-magic-of-metaclasses-part-i}{THE MAGIC OF METACLASSES - PART I}
\begin{list}{}{}
\item {} \href{\#metaclasses-as-class-factories}{Metaclasses as class factories}

\item {} \href{\#metaclasses-as-class-modifiers}{Metaclasses as class modifiers}

\item {} \href{\#a-few-caveats-about-the-usage-of-metaclasses}{A few caveats about the usage of metaclasses}

\item {} \href{\#metaclasses-and-inheritance}{Metaclasses and inheritance}

\item {} \href{\#conflicting-metaclasses}{Conflicting metaclasses}

\item {} \href{\#cooperative-metaclasses}{Cooperative metaclasses}

\item {} \href{\#metamethods-vs-class-methods}{Metamethods vs class methods}

\end{list}

\item {} \href{\#the-magic-of-metaclasses-part-2}{THE MAGIC OF METACLASSES - PART 2}
\begin{list}{}{}
\item {} \href{\#the-secrets-of-the-metaclass-hook}{The secrets of the \texttt{{\_}{\_}metaclass{\_}{\_}} hook}

\item {} \href{\#anonymous-inner-metaclasses}{Anonymous inner metaclasses}

\item {} \href{\#passing-parameters-to-meta-classes}{Passing parameters to (meta) classes}

\item {} \href{\#meta-functions}{Meta-functions}

\item {} \href{\#anonymous-cooperative-super-calls}{Anonymous cooperative super calls}

\item {} \href{\#more-on-metaclasses-as-class-factories}{More on metaclasses as class factories}

\item {} \href{\#programming-with-metaclasses}{Programming with metaclasses}

\item {} \href{\#metaclass-aided-operator-overloading}{Metaclass-aided operator overloading}

\end{list}

\item {} \href{\#advanced-metaprogramming-techniques}{ADVANCED METAPROGRAMMING TECHNIQUES}
\begin{list}{}{}
\item {} \href{\#on-code-processing}{On code processing}

\item {} \href{\#regular-expressions}{Regular expressions}

\item {} \href{\#more-on-metaclasses-and-subclassing-built-in-types}{More on metaclasses and subclassing built-in types}

\item {} \href{\#a-simple-state-machine}{A simple state machine}

\item {} \href{\#creating-classes}{Creating classes}

\item {} \href{\#modifying-modules}{Modifying modules}

\item {} \href{\#metaclasses-and-attribute-descriptors}{Metaclasses and attribute descriptors}

\item {} \href{\#id46}{Modifying hierarchies}

\item {} \href{\#tracing-hierarchies}{Tracing hierarchies}

\item {} \href{\#modifying-source-code}{Modifying source code}

\item {} \href{\#metaclass-regenerated-hierarchies}{Metaclass regenerated hierarchies}

\end{list}

\item {} \href{\#the-programmable-programming-language}{THE PROGRAMMABLE PROGRAMMING LANGUAGE}
\begin{list}{}{}
\item {} \href{\#enhancing-the-python-language}{Enhancing the Python language}

\item {} \href{\#restricting-python-dynamism}{Restricting Python dynamism}

\item {} \href{\#changing-the-language-without-changing-the-language}{Changing the language without changing the language}

\item {} \href{\#recognizing-magic-comments}{Recognizing magic comments}

\item {} \href{\#interpreting-python-source-code-on-the-fly}{Interpreting Python source code on the fly}

\item {} \href{\#implementing-lazy-evaluation}{Implementing lazy evaluation}

\item {} \href{\#implementing-a-ternary-operator}{Implementing a ternary operator}

\end{list}

\end{list}

\setcounter{chapter}{-1}

%___________________________________________________________________________

\hypertarget{preface}{}
\pdfbookmark[0]{Preface}{preface}
\section*{Preface}
\begin{quote}
\begin{flushleft}
\emph{There~is~only~one~way~to~learn:~trough~examples}
\end{flushleft}
\end{quote}


%___________________________________________________________________________

\hypertarget{the-philosophy-of-this-book}{}
\pdfbookmark[1]{The philosophy of this book}{the-philosophy-of-this-book}
\subsection*{The philosophy of this book}

This book is written with the intent to help the programmer going trough
the fascinating concepts of Object Oriented Programming (OOP), in their
Python incarnation. Notice that I say to help, not to teach. Actually,
I do not think that a book can teach OOP or any other non-trivial matter 
in Computer Science or other disciplines. Only the
practice can teach: practice, then practice, and practice again. 
You must learn yourself from your experiments, not from the books. 
Nevertheless, books are useful. They cannot teach, but they can help. 
They should give you new ideas that you was not thinking about, they should
show tricks you do not find in the manual, and in general they should be of
some guidance in the uphill road to knowledge. That is the philosophy
of this book. For this reason

1. It is not comprehensive, not systematic; 
it is intended to give ideas and basis: from
that the reader is expected to cover the missing part on his own,
browsing the documentation, other sources and other books, and finally
the definite autority, the source itself.

2. It will not even try to teach the \emph{best} practices. I will show what you can
do with Python, not what you ``should'' do. Often I will show solutions that are
not recommended. I am not a mammy saying this is
good, this is bad, do this do that.

3. You can only learn from your failures. If you think ``it should work, if I do
X and Y'' and it works, then you have learned nothing new. 
You have merely verified
that your previous knowledge was correct, but you haven't create a new
knowledge. On the other hand, when you think ``it should work, if I do
X and Y'' and it doesn't, then you have learned that your previous knowlegde
was wrong or incomplete, and you are forced to learn something new to
overcome the difficulty. For this reason, I think it is useful to report
not only how to do something, but also to report how not to do something, 
showing the pitfalls of wrong approaches.

That's in my opinion is the goal of a good book. I don't know if have
reached this goal or not (the decision is up to the reader), but at least
I have tried to follow these guidelines.

Moreover, this is not a book on OOP, 
it is a book on OOP \emph{in Python}.

In other words, the point of view of this book is not 
to emphasize general topics of OOP that are exportable to other languages, 
but exactly the opposite: I want to emphasize specific techniques that one
can only use in Python, or that are difficult to translate to other 
languages. Moreover, I will not provide comparisons with other 
languages (except for the section ``Why Python?'' in this introduction and
in few selected other places), 
in order to keep the discussion focused.

This choice comes from the initial motivation for this book, which was 
to fulfill a gap in the (otherwise excellent) Python documentation. 
The problem is that the available documentation still lacks an accessible 
reference of the new Python 2.2+ object-oriented features.
Since myself I have learned Python and OOP from scratch, 
I have decided to write this book in order to fill that gap and
help others.

The emphasis in this book is not in giving 
solutions to specific problems (even if most of the recipes of this book
can easily be tailored to solve real life concrete problems), it is in 
teaching  how does it work, why it does work in some cases and why does 
not work in some other cases. Avoiding too specific problems has an
additional bonus, since it allows me to use \emph{short} examples (the majority 
of the scripts presented here is under 20-30 lines) which I think are 
best suited to teach a new matter [\hyperlink{id2}{1}] . Notice, however, that whereas
the majority of the scripts in this book are short, it is also true
that they are pretty \emph{dense}. The density is due to various reasons:
\newcounter{listcnt1}
\begin{list}{\arabic{listcnt1}.}
{
\usecounter{listcnt1}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
I am defining a lot of helper functions and classes, that are
reused and enhanced during all the book.

\item {} 
I am doing a strong use of inheritance, therefore a script at the
end of the book can inherits from the classes defined through all
the book;

\item {} 
A ten line script involving metaclasses can easily perform the equivalent 
of generating hundreds of lines of code in a language without metaclasses 
such as Java or C++.

\end{list}

To my knowledge, there are no other books covering the same topics with
the same focus (be warned, however, that I haven't read so many Python 
books ;-). The two references that come closest to the present book are
the \texttt{Python Cookbook} by Alex Martelli and David Ascher, and
Alex Martelli's \texttt{Python in a Nutshell}. They are quite recent books and 
therefore it covers (in much less detail) some of the 2.2 features that are 
the central topics to this book. 
However, the Cookbook reserves to OOP only one chapter and has a quite 
different philosophy from the present book, therefore there is 
practically no overlapping. Also \texttt{Python in a Nutshell} covers 
metaclasses in few pages, whereas half of this book is essentially
dedied to them. This means that you can read both ;-)
\begin{figure}[b]\hypertarget{id2}[1]
Readers that prefer the  opposite philosophy of using longer, 
real life-like, examples, have already the excellent ``Dive into 
Python'' book \href{http://diveintopython.org/}{http://diveintopython.org/} at their disposal. This is 
a very good book that I certainly recommend to any (experienced) 
Python programmer; it is also freely available (just like this ;-).
However, the choice of arguments is quite different and there is 
essentially no overlap between my book and ``Dive into Python'' 
(therefore you can read both ;-).
\end{figure}


%___________________________________________________________________________

\hypertarget{for-who-this-book-in-intended}{}
\pdfbookmark[1]{For who this book in intended}{for-who-this-book-in-intended}
\subsection*{For who this book in intended}

I have tried to make this tutorial useful to a large public of Pythonistas, 
i.e. both people with no previous experience of Object Oriented Programming
and people with experience on OOP, but unfamiliar with the most
recent Python 2.2-2.3 features (such as attribute descriptors,
metaclasses, change of the MRO in multiple inheritance, etc). 
However, this is not a book for beginners: the non-experienced reader should 
check (at least) the Internet sites www.python.org/newbies.com and 
www.awaretek.com, that provide a nice collection of resources for Python 
newbies.

These are my recommendations for the reader, according to her/his level:
\newcounter{listcnt2}
\begin{list}{\arabic{listcnt2}.}
{
\usecounter{listcnt2}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
If you are an absolute beginner, with no experience on programming,
this book is \emph{not} for you (yet ;-). Go to 
\href{http://www.python.org/doc/Newbies.html}{http://www.python.org/doc/Newbies.html} and read one of the introductive 
texts listed there, then come back here. I recommend ``How to Think Like 
a Computer Scientist'', available for free on the net (see 
\href{http://www.ibiblio.org/obp/thinkCSpy/}{http://www.ibiblio.org/obp/thinkCSpy/}); I found it useful myself when 
I started learning Python; be warned, however, that it refers to the rather 
old Python version 1.5.2. There are also excellent books 
on the market (see \href{http://www.awaretek.com/plf.html}{http://www.awaretek.com/plf.html}). 
\href{http://www.uselesspython.com/}{http://www.uselesspython.com/} is a good resource to find recensions 
about available Python books. For free books, look at
\href{http://www.tcfb.com/freetechbooks/bookphyton.html}{http://www.tcfb.com/freetechbooks/bookphyton.html} .
This is \emph{not} another Python tutorial.

\item {} 
If you know already (at least) another programming language, but you don't
know Python, then this book is \emph{not} for you (again ;-). Read the FAQ, the
Python Tutorial and play a little with the Standard Library (all this
material can be downloaded for free from  \href{http://www.python.org}{http://www.python.org}), then
come back here.

\item {} 
If you have passed steps 1 and 2, and you are confortable with Python
at the level of simple procedural programming, but have no clue about
objects and classes, \emph{then} this book is for you. Read this book till
the end and your knowledge of OOP will pass from zero to a quite advanced 
level (hopefully). Of course, you will have to play with the code in 
this book and write a lot of code on your own, first ;-)

\item {} 
If you are confortable with Python and you also known OOP from other
languages or from earlier version of Python, then this book is for
you, too: you are ready to read the more advanced chapters.

\item {} 
If you are a Python guru, then you should read the book, too. I expect
you will find the errors and send me feedback, helping me to improve
this tutorial.

\end{list}


%___________________________________________________________________________

\hypertarget{about-the-scripts-in-this-book}{}
\pdfbookmark[1]{About the scripts in this book}{about-the-scripts-in-this-book}
\subsection*{About the scripts in this book}

All the scripts  in this book are free. You are expected to play
with them, to modify them and to improve them.

In order to facilitate the extraction of the scripts from the main text, both
visually for the reader and automatically for Python, I use the
convention of sandwiching the body of the example scripts in blocks like this
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<myfirstscript.py>}\\
\mbox{}\\
\mbox{print~"Here~Starts~the~Python~Way~to~Object~Oriented~Programming~!"}\\
\mbox{}\\
\mbox{{\#}</myfirstscript.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

You may extract the source of this script with the a Python program
called ``test.py'' and provided in the distribution. Simply give the 
following command:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\$}~python~test.py~myfirstscript.py}
\end{flushleft}\end{ttfamily}
\end{quote}

This will create a file called ``myfirstscript.py'', containing the
source of \texttt{myfirstscript.py}; moreover it will execute the script 
and write its output in a file called ``output.txt''. I have tested
all the scripts in this tutorial under Red Hat Linux 7.x and 
Windows 98SE. You should not have any problem in running them,
but if a problem is there, ``test.py'' will probably discover it,
even if, unfortunately, it will not provide the solution :-(.
Notice that test.py requires Python 2.3+ to work, since most of
the examples in this book heavily depends on the new features
introduced in Python 2.2-2.3. Since the installation of Python 
2.3 is simple, quick and free, I think I am requiring to my readers
who haven't upgraded yet a very little effort. This is well worth
the pain since Python 2.3 fixes few bugs of 2.2 (notably in the subject of
attribute descriptors and the \texttt{super} built-in) that makes

You may give more arguments to test.py, as in this example:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\$}~python~test.py~myfirstscript.py~mysecondscript.py}
\end{flushleft}\end{ttfamily}
\end{quote}

The output of both scripts will still be placed in the file ``output.txt''.
Notice that if you give an argument which is not the name of a script in the
book, it will be simply ignored. Morever, if you will not give any argument,
``test.py'' will automatically executes all the tutorial scripts, writing their 
output in ``output.txt'' [\hyperlink{id4}{2}] . You may want to give a look at this file, once 
you have finished the tutorial. It also contains the source code of 
the scripts, for better readability.

Many examples of this tutorial depend on utility functions defined
in a external module called \texttt{oopp} (\texttt{oopp} is an obvious abbreviation 
for the title of the tutorial). The module \texttt{oopp} is automatically generated 
by ``test.py'', which works by extracting from the tutorial 
text blocks of code of the form \texttt{{\#}<oopp.py> something {\#}</oopp.py>} 
and saving them in a file called ``oopp.py''. 
Let me give an example. A very recent enhancement to Python (in 
Python 2.3) has been the addition of a built-in boolean type with
values True and False:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\$}~python}\\
\mbox{Python~2.3a1~({\#}1,~Jan~~6~2003,~10:31:14)}\\
\mbox{[GCC~2.96~20000731~(Red~Hat~Linux~7.2~2.96-108.7.2)]~on~linux2}\\
\mbox{Type~"help",~"copyright",~"credits"~or~"license"~for~more~information.}\\
\mbox{>>>~1+1==2}\\
\mbox{True}\\
\mbox{>>>~1+1==3}\\
\mbox{False}\\
\mbox{>>>~type(True)}\\
\mbox{<type~'bool'>}\\
\mbox{>>>~type(False)}\\
\mbox{<type~'bool'>}
\end{flushleft}\end{ttfamily}
\end{quote}

However, previous version of Python use the integers 1 and 0 for 
True and False respectively.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\$}~python}\\
\mbox{Python~2.2~({\#}1,~Apr~12~2002,~15:29:57)}\\
\mbox{[GCC~2.96~20000731~(Red~Hat~Linux~7.2~2.96-109)]~on~linux2}\\
\mbox{Type~"help",~"copyright",~"credits"~or~"license"~for~more~information.}\\
\mbox{>>>~1+1==2}\\
\mbox{1}\\
\mbox{>>>~1+1==3~}\\
\mbox{0}
\end{flushleft}\end{ttfamily}
\end{quote}

Following the 2.3 convension, in this tutorial I will use the names 
\texttt{True} and \texttt{False} to denotes the numbers 1 and 0 respectively. 
This is automatic in Python 2.2.1+, but not in Python 2.2. Therefore, 
for sake of compatibility, it is convenient to set the values \texttt{True} 
and \texttt{False} in our utility module:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~{\_}{\_}builtin{\_}{\_}}\\
\mbox{try:~}\\
\mbox{~~~~{\_}{\_}builtin{\_}{\_}.True~~~{\#}look~if~True~is~already~defined}\\
\mbox{except~AttributeError:~{\#}~if~not~add~True~and~False~to~the~builtins}\\
\mbox{~~~~{\_}{\_}builtin{\_}{\_}.True~=~1}\\
\mbox{~~~~{\_}{\_}builtin{\_}{\_}.False~=~0}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of usage:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<mysecondscript.py>}\\
\mbox{}\\
\mbox{import~oopp}\\
\mbox{print~"True~=",True,}\\
\mbox{print~"False~=",False}\\
\mbox{}\\
\mbox{{\#}</mysecondscript.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output is ``True = 1 False = 0'' under Python 2.2 and 
``True = True False = False'' under Python 2.3+.
\begin{figure}[b]\hypertarget{id4}[2]
``test.py'', invoked without arguments, does not create '.py' files, 
since I don't want to kludge the distribution with dozens of ten-line
scripts. I expect you may want to save only few scripts as standalone
programs, and cut and paste the others.
\end{figure}


%___________________________________________________________________________

\hypertarget{conventions-used-in-this-book}{}
\pdfbookmark[1]{Conventions used in this book}{conventions-used-in-this-book}
\subsection*{Conventions used in this book}

Python expressions are denoted with monospaced fonts when in the text.
Sections marked with an asterisk can be skipped in a first reading.
Typically they have the purpose of clarifying some subtle point and
are not needed for the rest of the book. These sections are intended
for the advanced reader, but could confuse the beginner.
An example is the section about the difference between methods and
functions, or the difference between the inheritance constraint and
the metaclass constraint.


%___________________________________________________________________________

\hypertarget{introduction}{}
\pdfbookmark[0]{Introduction}{introduction}
\section*{Introduction}
\begin{quote}
\begin{flushleft}
\emph{A~language~that~doesn't~affect~the~way~you~think~about~programming,~\\
is~not~worth~knowing.}~--~Alan~Perlis
\end{flushleft}
\end{quote}


%___________________________________________________________________________

\hypertarget{why-oop}{}
\pdfbookmark[1]{Why OOP ?}{why-oop}
\subsection*{Why OOP ?}

I guess some of my readers, like me, have started programming in the mid-80's,
when traditional (i.e. non object-oriented) Basic and Pascal where popular as 
first languages. At the time OOP was not as pervasive in software development 
how it is now, most of the mainstream languages were non-object-oriented and 
C++ was just being released. That was a time when the transition from 
spaghetti-code to structured code was already well accomplished, but 
the transition from structured programming to (the first phase of) 
OOP was at the beginning.

Nowaydays, we live in a similar time of transition . Today, the transition 
to (the first phase of) OOP is well accomplished and essentially all 
mainstream
languages support some elementary form of OOP. To be clear, when I say
mainstream langauges, I have in mind Java and C++: C is a remarkable 
exception to the  rule, since it is mainstream but not object-oriented.

However, both Java an C++ (I mean standard Java and C++, not special
extension like DTS C++, that have quite powerful object oriented features)
are quite poor object-oriented languages: they provides only the most 
elementary aspects of OOP, the features of the \emph{first phase} of OOP.

Hence, today the transition to the \emph{second phase} of OOP is only at the 
beginning, i.e mainstream language are not yet really OO, but they will
become OOP in the near future.

By second phase of OOP I mean the phase in which the primary
objects of concern for the programmer are no more the objects, but the
metaobjects. In elementary OOP one works on objects, which have attributes
and methods (the evolution of old-fashioned data and functions)  defined
by their classes; in the second phase of OOP one works on classes 
which behavior is described by metaclasses. We no more modify objects 
trough classes: nowadays we modify classes and class hierarchies 
through metaclasses and multiple inheritance.

It would be tempting to represent the history of programming in the last
quarter of century with an evolutionary table like that:

\begin{longtable}[c]{|p{0.30\locallinewidth}|p{0.25\locallinewidth}|p{0.27\locallinewidth}|p{0.10\locallinewidth}|}
\hline
\textbf{
{\textasciitilde}1975
} & \textbf{
{\textasciitilde}1985
} & \textbf{
{\textasciitilde}1995
} & \textbf{
{\textasciitilde}2005
} \\ \hline
\endhead
%[visit_tbody]

procedural programming
 & 
OOP1
 & 
OOP2
 & 
?
 \\ \hline

data,functions
 & 
objects,classes
 & 
classes,metaclasses
 & 
?
 \\ \hline
%[depart_tbody]
\end{longtable}

The problem is that table would be simply wrong, since in truth
Smalltalk had metaclasses already 25 years ago! And also Lisp
had \emph{in nuce} everything a long \emph{long} time ago.
The truth is that certains languages where too much ahead of their 
time ;-)

Therefore, today we already have all the ideas 
and the conceptual tools to go beyond the first phase of OOP 
(they where invented 20-30 years ago), nevertheless those ideas are  
not yet universally known, nor implemented in mainstream languages.

Fortunately, there are good languages
where you can access the bonus of the second phase of OOP (Smalltalk, CLOS,
Dylan, ...): unfortunately
most of them are academic and/or little known in the real world
(often for purely commercial reasons, since typically languages are not
chosen accordingly to their merits, helas!). Python is an exception to this
rule, in the sense that it is an eminently practical language (it started
as a scripting language to do Operating System administrative jobs), 
which is relatively known and used in that application niche (even if some
people \emph{wrongly} think that should not be used for 'serious' things).

There are various reasons why most mainstream languages are rather
poor languages, i.e. underfeatured languages (as Java) or powerful, but too
tricky to use, as C++. Some are good reasons (for instance \emph{efficiency}: if
efficiency is the first concern, then poor languages can be much
better suited to the goal: for instance Fortran for number crunching
and C for system programming), some are less good (economical 
monopoly). There is nothing to do against these reasons: if you
need efficiency, or if you are forced to use a proprietary language
because it is the language used by your employer. However, if you
are free from these restrictions, there is another reason why you
could not choose to use a poweful language. The reason is that, 
till now, programmers working in the industrial world mostly had simple 
problems (I mean conceptually simple problems). In order to solve
simple problems one does not need a powerful language, and the effort
spent in learning it is not worth.

However, nowadays the situations has changed. Now, with Internet and graphics
programming everywhere, and object-oriented languages so widespread,
now it is the time when actually people \emph{needs} metaprogramming, the
ability to changing classes and programs. Now everybody is programming
in the large.

In this situation, it is justified to spend some time to learn better
way of programming. And of course, it is convenient to start from
the language with the flattest learning curve of all.


%___________________________________________________________________________

\hypertarget{why-python}{}
\pdfbookmark[1]{Why Python ?}{why-python}
\subsection*{Why Python ?}
\begin{quote}
\begin{flushleft}
\emph{In~many~ways,~it's~a~dull~language,~borrowing~solid~old~concepts~from~~\\
many~other~languages~{\&}~styles:~~boring~syntax,~unsurprising~semantics,~\\
few~automatic~coercions,~etc~etc.~~But~that's~one~of~the~things~I~like~\\
about~it.}~~--Tim~Peters~on~Python,~16~Sep~93
\end{flushleft}
\end{quote}

If you are reading this book, I assume you already  have some experience
with Python. If this is the case, you already know the obvious advantages
of Python such as readability, easy of use and short development time.
Nevertheless, you could only have used Python as a fast and simple
scripting language. If you are in this situation, then your risk to
have an incorrect opinion on the language like ``it is a nice little
language, but too simple to be useful in 'real' applications''. The
truth is that Python is designed to be \emph{simple}, and actually it
is; but by no means it is a ``shallow'' language. Actually, it goes
quite \emph{deep}, but it takes some time to appreciate this fact.

Let me contrast Python with Lisp, for instance. From the beginning,
Lisp was intended to be a language for experts, for people with difficult 
problems to solve. The first
users of Lisp were academicians, professors of CS and scientists.
On the contrary, from the beginning Python 
was intended to be language for everybody (Python predecessor was ABC, 
a language invented to teach CS to children). Python makes great a first 
language for everybody, whereas Lisp would require especially
clever and motivated students (and we all know that there is lack
of them ;-)

From this difference of origins, Python inherits an easy to learn syntax,
whereas Lisp syntax is horrible for the beginner (even if not as
horrible as C++ syntax ;-)
\begin{quote}
\begin{flushleft}
\emph{Macros~are~a~powerful~extension~to~weak~languages.~\\
Powerful~languages~don't~need~macros~by~definition.}~~\\
--~Christian~Tismer~on~c.l.p.~(referring~to~C)
\end{flushleft}
\end{quote}

Despite the differences, Python borrows quite a lot from Lisp and it
is nearly as expressive as it (I say nearly since Python is 
not as powerful as Lisp: by tradition, Lisp has always been on the top of 
hierarchy of programming language with respect to power of abstraction).
It is true that Python lacks some powerful Lisp features: for instance 
Python object model lacks multiple dispatching (for the time being ;-) 
and the language lacks Lisp macros (but this unlikely to change in the 
near future since Pythonistas see the lack of macro as a Good Thing [\hyperlink{id6}{3}]): 
nevertheless, the point is that Python is much \emph{much} easier to learn. 
You have (nearly) all the power, but without the complexity.

One of the reasons, is that Python
try to be as \emph{less} innovative as
possible: it takes the proven good things from others, more innovative
languages, and avoids their pitfalls. If you are an experienced
programmer , it will be even  easier to you to learn Python, since
there is more or less nothing which is really original to Python.
For instance:
\newcounter{listcnt3}
\begin{list}{\arabic{listcnt3}.}
{
\usecounter{listcnt3}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
the object model is took from languages that are good at it, such
as Smalltalk;

\item {} 
multiple inheritance has been modeled from languages good in it. such
as CLOS and Dylan;

\item {} 
regular expression follows the road opened by Perl;

\item {} 
functional features are borrowed from functional languages;

\item {} 
the idea of documentation strings come from Lisp;

\item {} 
list comprehension come from Haskell;

\item {} 
iterators and generators come from Icon;

\item {} 
etc. etc. (many other points here)

\end{list}

I thinks the really distinctive feature of Python with respect to
any other serious language I know, is that Python is \emph{easy}. You have the 
power (I mean power in conceptual sense, not computational power: in
the sense of computational power the best languages are
non-object-oriented ones) 
of the most powerful languages with a very little investement.
In addition to that, Python has a relatively large user base 
(as compared to Smalltalk or Ruby, or the various fragmented Lisp
communities). Of course, 
there is quite a difference between the user base of Python with
respect to the user base of, let say, VisualBasic or Perl. But 
I would never take in consideration VisualBasic for anything serious, 
whereas Perl is too ugly for my taste ;-).  
Finally, Python is \emph{practical}. With this I mean the fact that 
Python has libraries that
allow the user to do nearly everything, since you can access all the C/C++ 
libraries with little or no effort, and all the Java libraries, though the
Python implementation known as Jython. In particular, one has the choice
between many excellent GUI's trough PyQt, wxPython, Tkinter, etc.

Python started as an Object Oriented Programming
Languages from the beginning, nevertheless is was never intended to be
a \emph{pure} OOPL as SmallTalk or, more recently, Ruby. Python is a 
\emph{multiparadigm}
language such a Lisp, that you choose your programming style according
to your problem: spaghetti-code, structured programming, functional
programming, object-oriented programming are all supported. You can
even write bad code in Python, even if it is less simple than in other
languages ;-). Python is a language which has quite evolved in its twelve
years of life (the first public release was released in February 1991)
and many new features have been integrated in the language with time. 
In particular, Python 2.2 (released in 2002) was a major breakthrough 
in the history of the language
for what concerns support to Object Oriented Programming (OOP). 
Before the 2.2 revolution, Python Object
Orientation was good; now it is \emph{excellent}. All the fundamental features
of OOP, including pretty sophisticated ones, as metaclasses and multiple
inheritance, have now a very good support (the only missing thing is
multiple dispatching).
\begin{figure}[b]\hypertarget{id6}[3]
Python lacks macros for an intentional design choice: many people
in the community (including Guido itself) feel that macros are 
``too powerful''. If you give the user the freedom to create her
own language, you must face at least three problems: i) the risk
to split the original language in dozens of different dialects;
ii) in collaborative projects, the individual programmer must 
spend an huge amount of time and effort would be spent in learning
macro systems written by others; iii) not all users are good
language designers: the programmer will have to fight with badly
designed macro systems. Due to these problems, it seems unlikely
that macros will be added to Python in the future.
\end{figure}
\begin{figure}[b]\hypertarget{id7}[4]
For a good comparison between Python and Lisp I remind the reader to
the excellent Peter Norvig's article in
\href{http://www.norvig.com/python-lisp.html}{http://www.norvig.com/python-lisp.html}
\end{figure}


%___________________________________________________________________________

\hypertarget{further-thoughts}{}
\pdfbookmark[1]{Further thoughts}{further-thoughts}
\subsection*{Further thoughts}

Actually, the principal reasons why I begun studying 
Python was the documentation and the newsgroup: Python has an outstanding 
freely available documentation and an incredibly helpful newsgroup that
make extremely easy to learn the language. If I had found a comparable 
free documentation/newsgroup for C++ or Lisp, I would have studied that
languages instead.

Unfortunately, the enormous development at the software level, had no
correspondence with with an appropriate development of documentation.
As a consequence, the many beatiful, powerful and extremely \emph{useful}
new features of Python 2.2+ object orientation are mostly remained
confined to developers and power users: the average Python programmer
has remained a little a part from the rapid development and she
\emph{wrongly} thinks she has no use for the new features. There have
also been \emph{protestations} of the users against developers of the
kind ``please, stop adding thousands of complicated new extensions
to the language for which we have no use'' !

Extending a language is always a delicate thing to do, for a whole
bunch of reasons:
\newcounter{listcnt4}
\begin{list}{\arabic{listcnt4}.}
{
\usecounter{listcnt4}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
once one extension is done, it is there \emph{forever}.

\end{list}

My experience has been the following.

When I first read about metaclasses, in Guido's essay
``Unifying types and classes in Python 2.2'', I thought ``Wow,
classes of classes, cool concept, but how useful is it?  
Are metaclasses really providing some new functionality?  
What can I do with metaclasses that I cannot do without?''

Clearly, in these terms, the question is rather retorical, since in principle
any Turing-complete programming languages contains all the features provided 
by metaclasses. Python metaclasses themselves are implemented in C, that has 
no metaclasses. Therefore, my real question was not ``What can I do 
with metaclasses that I cannot do without?'' but ``How big is the convenience 
provided by metaclasses, with respect to my typical applications?''.

The answer depends on the kind of problem you are considering. For certain
classes of problems it can be \emph{very} large, as I will show in this and in
the next chapters.

I think the biggest advantage of metaclasses is \emph{elegance}. Altough it
is true that most of what you can do with metaclasses, can be done without 
metaclasses, not using metaclasses can result in a much \emph{uglier} solution.

One needs difficult problems in order to appreciate the advantage
of powerful methods.

If all you need is to write few scripts for copying two or three files,
there is no point in learning OOP.On the other hand, if you only
write simple programs where you define only one of two classes, there
is no point in using metaclasses. Metaclasses becomes relevant only
when you have many classes, whole classes of classes with similar
features that you want to modify.

In this sense, metaprogramming is for experts only, i.e. with people
with difficult problems. The point however, is that nowaydays,
many persons have difficult problems.

Finally, let me conclude this preface by recalling the
gist of Python wisdom.
\begin{quote}
\begin{verbatim}>>> 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!\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{first-things-first}{}
\pdfbookmark[0]{FIRST THINGS, FIRST}{first-things-first}
\section*{FIRST THINGS, FIRST}

This is an introductory chapter, with the main purpose of fixing the 
terminology used in the sequel. In particular, I give the definitions 
of objects, classes, attributes and methods. I discuss a few examples 
and I show some of the most elementary Python introspection features.


%___________________________________________________________________________

\hypertarget{what-s-an-object}{}
\pdfbookmark[1]{What's an object?}{what-s-an-object}
\subsection*{What's an object?}
\begin{quote}
\begin{flushleft}
\emph{So~Everything~Is~An~object.~~~\\
I'm~sure~the~Smalltalkers~are~very~happy~:)}~\\
~\\
--~Michael~Hudson~on~comp.lang.python
\end{flushleft}
\end{quote}

``What's an object'' is the obvious question raised by anybody starting 
to learn Object Oriented Programming. The answer is simple: in Python, 
everything in an object!

An operative definition is the following: an \emph{object}
is everything that can be labelled with an \emph{object reference}.

In practical terms, the object reference is implemented as 
the object memory address, that is an integer number which uniquely
specify the object. There is a simple way to retrieve the object reference:
to use the builtin \texttt{id} function. Informations on \texttt{id} can be retrieved 
via the \texttt{help} function [\hyperlink{id7}{4}]:
\begin{quote}
\begin{verbatim}>>> help(id)
Help on built-in function id:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)\end{verbatim}
\end{quote}

The reader is strongly encouraged to try the help function on everything
(including help(help) ;-). This is the best way to learn how Python works,
even \emph{better} than reading the standard documentation, since the on-line
help is often more update.

Suppose for instance we wonder if the number \texttt{1} is an object: 
it is easy enough to ask Python for the answer:
\begin{quote}
\begin{verbatim}>>> id(1)
135383880\end{verbatim}
\end{quote}

Therefore the number 1 is a Python object and it is stored at the memory 
address 135383880, at least in my computer and during the current session.
Notice that the object reference is a dynamic thing; nevertheless it
is guaranteed to be unique and constant for a given object during its 
lifetime (two objects whose lifetimes are disjunct may have the same id() 
value, though).

Here there are other examples of built-in objects:
\begin{quote}
\begin{verbatim}>>> id(1L) # long
1074483312
>>> id(1.0) #float
135682468
>>> id(1j) # complex
135623440
>>> id('1') #string
1074398272
>>> id([1]) #list
1074376588
>>> id((1,)) #tuple
1074348844
>>> id({1:1}) # dict
1074338100\end{verbatim}
\end{quote}

Even functions are objects:
\begin{quote}
\begin{verbatim}>>> def f(x): return x #user-defined function
>>> id(f)
1074292020
>>> g=lambda x: x #another way to define functions
>>> id(g)
1074292468
>>> id(id) #id itself is a built-in function
1074278668\end{verbatim}
\end{quote}

Modules are objects, too:
\begin{quote}
\begin{verbatim}>>> import math 
>>> id(math) #module of the standard library
1074239068
>>> id(math.sqrt) #function of the standard library
1074469420\end{verbatim}
\end{quote}

\texttt{help} itself is an object:
\begin{quote}
\begin{verbatim}>>> id(help)
1074373452\end{verbatim}
\end{quote}

Finally, we may notice that the reserved keywords are not objects:
\begin{quote}
\begin{verbatim}>>> id(print) #error
File "<string>", line 1
  id(print)       ^
SyntaxError: invalid syntax\end{verbatim}
\end{quote}

The operative definition is convenient since it gives a practical way
to check if something is an object and, more importantly, if two
objects are the same or not:
\begin{quote}
% doctest 
\begin{verbatim}>>> s1='spam'
>>> s2='spam'
>>> s1==s2
True
>>> id(s1)==id(s2)
True\end{verbatim}
\end{quote}

A more elegant way of spelling \texttt{id(obj1)==id(obj2)} is to use the
keyword \texttt{is}:
\begin{quote}
\begin{verbatim}>>> s1 is s2
True\end{verbatim}
\end{quote}

However, I should warn the reader that sometimes \texttt{is} can be surprising:
\begin{quote}
\begin{verbatim}>>> id([]) == id([])
True
>>> [] is []
False\end{verbatim}
\end{quote}

This is happening because writing \texttt{id([])} dynamically creates an unique
object (a list) which goes away when you're finished with it.  So when an
expression needs both at the same time (\texttt{[] is []}), two unique objects
are created, but when an expression doesn't need both at the same time
(\texttt{id([]) == id([])}), an object gets created with an ID, is destroyed,
and then a second object is created with the same ID (since the last one
just got reclaimed) and their IDs compare equal. In other words, ``the 
ID is guaranteed to be unique \emph{only} among simultaneously existing objects''.

Another surprise is the following:
\begin{quote}
\begin{verbatim}>>> a=1
>>> b=1
>>> a is b
True
>>> a=556
>>> b=556
>>> a is b
False\end{verbatim}
\end{quote}

The reason is that integers between 0 and 99 are pre-instantiated by the
interpreter, whereas larger integers are recreated each time.

Notice the difference between '==' and 'is':
\begin{quote}
\begin{verbatim}>>> 1L==1
True\end{verbatim}
\end{quote}

but
\begin{quote}
\begin{verbatim}>>> 1L is 1
False \end{verbatim}
\end{quote}

since they are different objects:
\begin{quote}
\begin{verbatim}>>> id(1L) # long 1 
135625536
>>> id(1)  # int 1
135286080\end{verbatim}
\end{quote}

The disadvantage of the operative definition is that it gives little 
understanding of what an object can be used for. To this aim, I must
introduce the concept of \emph{class}.
\begin{figure}[b]\hypertarget{id9}[5]
Actually \texttt{help} is not a function but a callable object. The
difference will be discussed in a following chapter.
\end{figure}


%___________________________________________________________________________

\hypertarget{objects-and-classes}{}
\pdfbookmark[1]{Objects and classes}{objects-and-classes}
\subsection*{Objects and classes}

It is convenient to think of an object as an element of a set.

It you think a bit, this is the most general definition that actually 
grasps what we mean by object in the common language.
For instance, consider this book, ``Object Oriented Programming in Python'':
this book is an object, in the sense that it is a specific representative 
of the \emph{class} of all possible books.
According to this definition, objects are strictly related to classes, and
actually we say that objects are \emph{instances} of classes.

Classes are nested: for
instance this book belongs to the class of books about programming
language, which is a subset of the class of all possible books;
moreover we may further specify this book as a Python book; moreover
we may specify this book as a Python 2.2+ book. There is no limit
to the restrictions we may impose to our classes.
On the other hand. it is convenient to have a ``mother'' class,
such that any object belongs to it. All strongly Object Oriented
Language have such a class [\hyperlink{id9}{5}]; in Python it is called \emph{object}.

The relation between objects and classes in Python can be investigated
trough the built-in function \texttt{type} [\hyperlink{id12}{6}] that gives the class of any 
Python object.

Let me give some example:
\newcounter{listcnt5}
\begin{list}{\arabic{listcnt5}.}
{
\usecounter{listcnt5}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
Integers numbers are instances of the class \texttt{int} or \texttt{long}:

\end{list}
\begin{quote}
\begin{verbatim}>>> type(1)
<type 'int'>
>>> type(1L)
<type 'long'>\end{verbatim}
\end{quote}
\newcounter{listcnt6}
\begin{list}{\arabic{listcnt6}.}
{
\usecounter{listcnt6}
\addtocounter{listcnt6}{1}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
Floating point numbers are instances of the class \texttt{float}:

\end{list}
\begin{quote}
\begin{verbatim}>>> type(1.0)
<type 'float'>\end{verbatim}
\end{quote}
\newcounter{listcnt7}
\begin{list}{\arabic{listcnt7}.}
{
\usecounter{listcnt7}
\addtocounter{listcnt7}{2}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
Complex numbers are instances of the class \texttt{complex}:

\end{list}
\begin{quote}
\begin{verbatim}>>> type(1.0+1.0j)
<type 'complex'>\end{verbatim}
\end{quote}
\newcounter{listcnt8}
\begin{list}{\arabic{listcnt8}.}
{
\usecounter{listcnt8}
\addtocounter{listcnt8}{3}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
Strings are instances of the class \texttt{str}:

\end{list}
\begin{quote}
\begin{verbatim}>>> type('1')
<type 'str'>\end{verbatim}
\end{quote}
\newcounter{listcnt9}
\begin{list}{\arabic{listcnt9}.}
{
\usecounter{listcnt9}
\addtocounter{listcnt9}{4}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
List, tuples and dictionaries are instances of \texttt{list}, \texttt{tuple} and
\texttt{dict} respectively:

\end{list}
\begin{quote}
\begin{verbatim}>>> type('1')
<type 'str'>
>>> type([1])
<type 'list'>
>>> type((1,))
<type 'tuple'>
>>> type({1:1})
<type 'dict'>\end{verbatim}
\end{quote}
\newcounter{listcnt10}
\begin{list}{\arabic{listcnt10}.}
{
\usecounter{listcnt10}
\addtocounter{listcnt10}{5}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
User defined functions are instances of the \texttt{function} built-in type

\end{list}
\begin{quote}
\begin{verbatim}>>> type(f)
<type 'function'>
>>> type(g)
<type 'function'>\end{verbatim}
\end{quote}

All the previous types are subclasses of object:
\begin{quote}
\begin{verbatim}>>> for cl in int,long,float,str,list,tuple,dict: issubclass(cl,object)
True
True
True
True
True
True
True\end{verbatim}
\end{quote}

However, Python is not a 100{\%} pure Object
Oriented Programming language and its object model has still some minor
warts, due to historical accidents.

Paraphrasing George Orwell, we may say that in Python 2.2-2.3, 
all objects are equal, but some objects are more equal than others.
Actually, we may distinguish Python objects in new style objects, 
or rich man objects, and old style objects, or poor man objects. 
New style objects are instances of new style classes whereas old
style objects are instances of old style classes.
The difference is that new style classes are subclasses of object whereas
old style classes are not.

Old style classes are there for sake of compatibility with previous 
releases of Python, but starting from Python 2.2 practically all built-in 
classes are new style classes.

Instance of old style classes are called old style objects. I will give
few examples of old style objects in the future.

In this tutorial with the term
object \emph{tout court} we will mean new style objects, unless the contrary 
is explicitely stated.
\begin{figure}[b]\hypertarget{id12}[6]
one may notice that C++ does not have such a class, but C++
is \emph{not} a strongly object oriented language ;-)
\end{figure}
\begin{figure}[b]\hypertarget{id13}[7]
Actually \texttt{type} is not a function, but a metaclass; nevertheless, 
since this is an advanced concept, discussed in the fourth chapter; 
for the time being it is better to think of \texttt{type} as a built-in 
function analogous to \texttt{id}.
\end{figure}


%___________________________________________________________________________

\hypertarget{objects-have-attributes}{}
\pdfbookmark[1]{Objects have attributes}{objects-have-attributes}
\subsection*{Objects have attributes}

All objects have attributes describing their characteristics, that may
be accessed via the dot notation
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{objectname.objectattribute}
\end{flushleft}\end{ttfamily}
\end{quote}

The dot notation is common to most Object Oriented programming languages,
therefore the reader with a little of experience should find it not surprising 
at all (Python strongly believes in the Principle of Least Surprise). However,
Python objects also have special attributes denoted by the double-double
underscore notation
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{objectname.{\_}{\_}specialattribute{\_}{\_}}
\end{flushleft}\end{ttfamily}
\end{quote}

with the aim of helping the wonderful Python introspection features, that
does not have correspondence in all OOP language.

Consider for example the string literal ``spam''. We may discover its
class by looking at its special attribute \emph{{\_}{\_}class{\_}{\_}}:
\begin{quote}
\begin{verbatim}>>> 'spam'.__class__
<type 'str'>\end{verbatim}
\end{quote}

Using the \texttt{{\_}{\_}class{\_}{\_}} attribute is not always equivalent to using the 
\texttt{type} function, but it works for all built-in types. Consider for instance 
the number \emph{1}: we may extract its class as follows:
\begin{quote}
\begin{verbatim}>>> (1).__class__
<type 'int'>\end{verbatim}
\end{quote}

Notice that the parenthesis are needed to avoid confusion between the integer
1 and the float (1.).

The non-equivalence type/class is the key to distinguish new style objects from
old style, since for old style objects \texttt{type(obj)<>obj.{\_}{\_}class{\_}{\_}}.
We may use this knowledge to make and utility function that discovers
if an object is a ``real'' object (i.e. new style) or a poor man object:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~isnewstyle(obj):}\\
\mbox{~~~~try:~{\#}some~objects~may~lack~a~{\_}{\_}class{\_}{\_}~attribute~}\\
\mbox{~~~~~~~~obj.{\_}{\_}class{\_}{\_}}\\
\mbox{~~~~except~AttributeError:}\\
\mbox{~~~~~~~~return~False}\\
\mbox{~~~~else:~{\#}look~if~there~is~unification~type/class}\\
\mbox{~~~~~~~~return~type(obj)~is~obj.{\_}{\_}class{\_}{\_}}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Let us check this with various examples:
\begin{quote}
\begin{verbatim}>>> from oopp import isnewstyle
>>> isnewstyle(1)
True
>>> isnewstyle(lambda x:x)
True
>>> isnewstyle(id)
True
>>> isnewstyle(type)
True
>>> isnewstyle(isnewstyle)
True
>>> import math
>>> isnewstyle(math)
True
>>> isnewstyle(math.sqrt)
True
>>> isnewstyle('hello')
True\end{verbatim}
\end{quote}

It is not obvious to find something which is not a real object,
between the built-in objects, however it is possible. For instance, 
the \texttt{help} ``function'' is an old style object:
\begin{quote}
\begin{verbatim}>>> isnewstyle(help)
False\end{verbatim}
\end{quote}

since
\begin{quote}
\begin{verbatim}>>> help.__class__
<class site._Helper at 0x8127c94>\end{verbatim}
\end{quote}

is different from
\begin{quote}
\begin{verbatim}>>> type(help)
<type 'instance'>\end{verbatim}
\end{quote}

Regular expression objects are even poorer objects with no \texttt{{\_}{\_}class{\_}{\_}} 
attribute:
\begin{quote}
\begin{verbatim}>>> import re
>>> reobj=re.compile('somestring')
>>> isnewstyle(reobj)
False
>>> type(reobj)
<type '_sre.SRE_Pattern'>
>>> reobj.__class__ #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: __class__\end{verbatim}
\end{quote}

There other special attributes other than \texttt{{\_}{\_}class{\_}{\_}}; a particularly useful
one is \texttt{{\_}{\_}doc{\_}{\_}}, that contains informations on the class it
refers to. Consider for instance the \texttt{str} class: by looking at its
\texttt{{\_}{\_}doc{\_}{\_}} attribute we can get information on the usage of this class:
\begin{quote}
\begin{verbatim}>>> str.__doc__
str(object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.\end{verbatim}
\end{quote}

From that docstring we learn how to convert generic objects in strings;
for instance we may convert numbers, lists, tuples and dictionaries:
\begin{quote}
\begin{verbatim}>>> str(1)
'1'
>>> str([1])
'[1]'
>>> str((1,))
(1,)'
>>> str({1:1})
'{1: 1}'\end{verbatim}
\end{quote}

\texttt{str} is implicitely called each time we use the \texttt{print} statement, since
\texttt{print obj} is actually syntactic sugar for \texttt{print str(obj)}.

Classes and modules have another interesting special attribute, the 
\texttt{{\_}{\_}dict{\_}{\_}} attribute that gives the content of the class/module.
For instance, the contents of the standard \texttt{math} module can be retrieved 
as follows:
\begin{quote}
\begin{verbatim}>>> import math
>>> for key in math.__dict__: print key,
...
fmod atan pow __file__ cosh ldexp hypot sinh __name__ tan ceil asin cos 
e log fabs floor tanh sqrt __doc__ frexp atan2 modf exp acos pi log10 sin\end{verbatim}
\end{quote}

Alternatively, one can use the built-in function \texttt{vars}:
\begin{quote}
\begin{verbatim}>>> vars(math) is math.__dict__
True\end{verbatim}
\end{quote}

This identity is true for any object with a \texttt{{\_}{\_}dict{\_}{\_}} attribute. 
Two others interesting special attributes are \texttt{{\_}{\_}doc{\_}{\_}}
\begin{quote}
\begin{verbatim}>>> print math.__doc__
This module is always available.  It provides access to the
mathematical functions defined by the C standard. \end{verbatim}
\end{quote}

and \texttt{{\_}{\_}file{\_}{\_}}:
\begin{quote}
\begin{verbatim}>>> math.__file__ #gives the file associated with the module
'/usr/lib/python2.2/lib-dynload/mathmodule.so'\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{objects-have-methods}{}
\pdfbookmark[1]{Objects have methods}{objects-have-methods}
\subsection*{Objects have methods}

In addition to attributes, objects also have \emph{methods}, i.e. 
functions attached to their classes [\hyperlink{id13}{7}].
Methods are also invoked with the dot notation, but
they can be distinguished by attributes because they are typically
called with parenthesis (this is a little simplistic, but it is enough for
an introductory chapter). As a simple example, let me show the
invocation of the \texttt{split} method for a string object:
\begin{quote}
\begin{verbatim}>>> s='hello world!'
>>> s.split()
['hello', 'world!']\end{verbatim}
\end{quote}

In this example \texttt{s.split} is called a \emph{bount method}, since it is
applied to the string object \texttt{s}:
\begin{quote}
\begin{verbatim}>>> s.split
<built-in method split of str object at 0x81572b8>\end{verbatim}
\end{quote}

An \emph{unbound method}, instead, is applied to the class: in this case the
unbound version of \texttt{split} is applied to the \texttt{str} class:
\begin{quote}
\begin{verbatim}>>> str.split
<method 'split' of 'str' objects>\end{verbatim}
\end{quote}

A bound method is obtained from its corresponding unbound 
method by providing the object to the unbound method: for instance 
by providing \texttt{s} to \texttt{str.split} we obtain the same effect of \titlereference{s.split()}:
\begin{quote}
\begin{verbatim}>>> str.split(s)
['hello', 'world!']\end{verbatim}
\end{quote}

This operation is called \emph{binding}  in the Python literature: when write
\texttt{str.split(s)} we bind the unbound method \texttt{str.split} to the object \texttt{s}.
It is interesting to recognize that the bound and unbound methods are
\emph{different} objects:
\begin{quote}
\begin{verbatim}>>> id(str.split) # unbound method reference
135414364
>>> id(s.split) # this is a different object!
135611408\end{verbatim}
\end{quote}

The unbound method (and therefore the bound method) has a \texttt{{\_}{\_}doc{\_}{\_}} 
attribute explaining how it works:
\begin{quote}
\begin{verbatim}>>> print str.split.__doc__
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator.\end{verbatim}
\end{quote}
\begin{figure}[b]\hypertarget{id15}[8]
A precise definition will be given in chapter 5 that introduces the
concept of attribute descriptors. There are subtle
differences between functions and methods.
\end{figure}


%___________________________________________________________________________

\hypertarget{summing-objects}{}
\pdfbookmark[1]{Summing objects}{summing-objects}
\subsection*{Summing objects}

In a pure object-oriented world, there are no functions and everything is 
done trough methods. Python is not a pure OOP language, however quite a
lot is done trough methods. For instance, it is quite interesting to analyze 
what happens when an apparently trivial statement such as
\begin{quote}
\begin{verbatim}>>> 1+1
2\end{verbatim}
\end{quote}

is executed in an object-oriented world.

The key to understand, is to notice that the number 1 is an object, specifically
an instance of class \texttt{int}: this means that that 1 inherits all the methods
of the \texttt{int} class. In particular it inherits a special method called 
\texttt{{\_}{\_}add{\_}{\_}}: this means 1+1 is actually syntactic sugar for
\begin{quote}
\begin{verbatim}>>> (1).__add__(1)
2\end{verbatim}
\end{quote}

which in turns is syntactic sugar for
\begin{quote}
\begin{verbatim}>>> int.__add__(1,1)
2\end{verbatim}
\end{quote}

The same is true for subtraction, multiplication, division and other 
binary operations.
\begin{quote}
\begin{verbatim}>>> 'hello'*2
'hellohello'
>>> (2).__mul__('hello')
'hellohello'
>>> str.__mul__('hello',2)
'hellohello'\end{verbatim}
\end{quote}

However, notice that
\begin{quote}
\begin{verbatim}>>> str.__mul__(2,'hello') #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: descriptor '__mul__' requires a 'str' object but received a 'int'\end{verbatim}
\end{quote}

The fact that operators are implemented as methods, is the key to
\emph{operator overloading}: in Python (as well as in other OOP languages)
the user can redefine the operators. This is already done by default
for some operators: for instance the operator \texttt{+} is overloaded
and works both for integers, floats, complex  numbers and for strings.


%___________________________________________________________________________

\hypertarget{inspecting-objects}{}
\pdfbookmark[1]{Inspecting objects}{inspecting-objects}
\subsection*{Inspecting objects}

In Python it is possible to retrieve most of the attributes and methods 
of an object by using the built-in function \texttt{dir()}
(try \texttt{help(dir)} for more information).

Let me consider the simplest case of a generic object:
\begin{quote}
\begin{verbatim}>>> obj=object()
>>> dir(obj)
['__class__', '__delattr__', '__doc__', '__getattribute__', 
 '__hash__', '__init__', '__new__', '__reduce__', '__repr__', 
 '__setattr__', '__str__']\end{verbatim}
\end{quote}

As we see, there are plenty of attributes available
even to a do nothing object; many of them are special attributes
providing introspection capabilities which are not 
common to all programming languages. We have already discussed the
meaning of some of the more obvious special attributes.
The meaning of some of  the others is quite non-obvious, however.
The docstring is invaluable in providing some clue.

Notice that  there are special \emph{hidden} attributes that cannot be retrieved
with \texttt{dir()}. For instance the \texttt{{\_}{\_}name{\_}{\_}} attribute, returning the 
name of the object (defined for classes, modules and functions) 
and the \texttt{{\_}{\_}subclasses{\_}{\_}} method, defined for classes and returning the 
list of immediate subclasses of a class:
\begin{quote}
\begin{verbatim}>>> str.__name__
'str'
>>> str.__subclasses__.__doc__
'__subclasses__() -> list of immediate subclasses'
>>> str.__subclasses__() # no subclasses of 'str' are currently defined
[]\end{verbatim}
\end{quote}

For instance by doing
\begin{quote}
\begin{verbatim}>>> obj.__getattribute__.__doc__
"x.__getattribute__('name') <==> x.name"\end{verbatim}
\end{quote}

we discover that the expression \texttt{x.name} is syntactic sugar for
\begin{quote}

\texttt{x.{\_}{\_}getattribute{\_}{\_}('name')}
\end{quote}

Another equivalent form which is more often used is
\begin{quote}

\texttt{getattr(x,'name')}
\end{quote}

We may use this trick to make a function that retrieves all the
attributes of an object except the special ones:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~special(name):~return~name.startswith('{\_}{\_}')~and~name.endswith('{\_}{\_}')}\\
\mbox{}\\
\mbox{def~attributes(obj,condition=lambda~n,v:~not~special(n)):}\\
\mbox{~~~~"""Returns~a~dictionary~containing~the~accessible~attributes~of~}\\
\mbox{~~~~an~object.~By~default,~returns~the~non-special~attributes~only."""}\\
\mbox{~~~~dic={\{}{\}}}\\
\mbox{~~~~for~attr~in~dir(obj):}\\
\mbox{~~~~~~~~try:~v=getattr(obj,attr)}\\
\mbox{~~~~~~~~except:~continue~{\#}attr~is~not~accessible}\\
\mbox{~~~~~~~~if~condition(attr,v):~dic[attr]=v}\\
\mbox{~~~~return~dic}\\
\mbox{}\\
\mbox{getall~=~lambda~n,v:~True}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that certain attributes may be unaccessible (we will see how
to make attributes unaccessible in a following chapter) 
and in this case they are simply ignored.
For instance you may retrieve the regular (i.e. non special)
attributes of the built-in functions:
\begin{quote}
\begin{verbatim}>>> from oopp import attributes
>>> attributes(f).keys()
['func_closure', 'func_dict', 'func_defaults', 'func_name', 
 'func_code', 'func_doc', 'func_globals']\end{verbatim}
\end{quote}

In the same vein of the \texttt{getattr} function, there is a built-in
\texttt{setattr} function (that actually calls the \texttt{{\_}{\_}setattr{\_}{\_}} built-in
method), that allows the user to change the attributes and methods of
and object. Informations on \texttt{setattr} can be retrieved from the help 
function:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{>>>~help(setattr)}\\
\mbox{Help~on~built-in~function~setattr:}\\
\mbox{setattr(...)}\\
\mbox{setattr(object,~name,~value)}\\
\mbox{Set~a~named~attribute~on~an~object;~setattr(x,~'y',~v)~is~equivalent~to}\\
\mbox{``x.y~=~v''.}
\end{flushleft}\end{ttfamily}
\end{quote}

\texttt{setattr} can be used to add attributes to an object:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~sys}\\
\mbox{}\\
\mbox{def~customize(obj,errfile=None,**kw):}\\
\mbox{~~~~"""Adds~attributes~to~an~object,~if~possible.~If~not,~writes~an~error}\\
\mbox{~~~~message~on~'errfile'.~If~errfile~is~None,~skips~the~exception."""}\\
\mbox{~~~~for~k~in~kw:}\\
\mbox{~~~~~~~~try:~}\\
\mbox{~~~~~~~~~~~~setattr(obj,k,kw[k])}\\
\mbox{~~~~~~~~except:~{\#}~setting~error}\\
\mbox{~~~~~~~~~~~~if~errfile:}\\
\mbox{~~~~~~~~~~~~~~~~print~>>~errfile,"Error:~{\%}s~cannot~be~set"~{\%}~k}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The attributes of built-in objects cannot be set, however:
\begin{quote}
\begin{verbatim}>>> from oopp import customize,sys
>>> customize(object(),errfile=sys.stdout,newattr='hello!') #error
AttributeError: newattr cannot be set\end{verbatim}
\end{quote}

On the other hand, the attributes of modules can be set:
\begin{quote}
\begin{verbatim}>>> import time
>>> customize(time,newattr='hello!')
>>> time.newattr
'hello!'\end{verbatim}
\end{quote}

Notice that this means we may enhances modules at run-time, but adding
new routines, not only new data attributes.

The \texttt{attributes} and \texttt{customize} functions work for any kind of objects; 
in particular, since classes are a special kind of objects, they work 
for classes, too. Here are the attributes of the \texttt{str}, \texttt{list} and 
\texttt{dict} built-in types:
\begin{quote}
\begin{verbatim}>>> from oopp import attributes
>>> attributes(str).keys()
['startswith', 'rjust', 'lstrip', 'swapcase', 'replace','encode',
 'endswith', 'splitlines', 'rfind', 'strip', 'isdigit', 'ljust', 
 'capitalize', 'find', 'count', 'index', 'lower', 'translate','join', 
 'center', 'isalnum','title', 'rindex', 'expandtabs', 'isspace', 
 'decode', 'isalpha', 'split', 'rstrip', 'islower', 'isupper', 
 'istitle', 'upper']
>>> attributes(list).keys()
['append', 'count', 'extend', 'index', 'insert', 'pop', 
 'remove', 'reverse', 'sort']
>>> attributes(dict).keys()
['clear','copy','fromkeys', 'get', 'has_key', 'items','iteritems',
 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 
 'update', 'values']\end{verbatim}
\end{quote}

Classes and modules have a special attribute \texttt{{\_}{\_}dict{\_}{\_}} giving the 
dictionary of their attributes. Since it is often a quite large dictionary, 
it is convenient to define an utility function printing this dictionary in a 
nice form:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~pretty(dic):}\\
\mbox{~~~~"Returns~a~nice~string~representation~for~the~dictionary"}\\
\mbox{~~~~keys=dic.keys();~keys.sort()~{\#}~sorts~the~keys}\\
\mbox{~~~~return~'{\textbackslash}n'.join(['{\%}s~=~{\%}s'~{\%}~(k,dic[k])~for~k~in~keys])}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

I encourage the use of this function in order to retrieve more 
information about the modules of the standard library:
\begin{quote}
\begin{verbatim}>>> from oopp import pretty
>>> import time #look at the 'time' standard library module
>>> print pretty(vars(time))
__doc__ = This module provides various functions to manipulate time values.
There are two standard representations of time.  One is the number
of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
or a floating point number (to represent fractions of seconds).
The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
The actual value can be retrieved by calling gmtime(0).
The other representation is a tuple of 9 integers giving local time.
The tuple items are:
  year (four digits, e.g. 1998)
  month (1-12)
  day (1-31)
  hours (0-23)
  minutes (0-59)
  seconds (0-59)
  weekday (0-6, Monday is 0)
  Julian day (day in the year, 1-366)
  DST (Daylight Savings Time) flag (-1, 0 or 1)
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
Variables:
timezone -- difference in seconds between UTC and local standard time
altzone -- difference in  seconds between UTC and local DST time
daylight -- whether local time should reflect DST
tzname -- tuple of (standard time zone name, DST time zone name)
Functions:
time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
__file__ = /usr/local/lib/python2.3/lib-dynload/time.so
__name__ = time
accept2dyear = 1
altzone = 14400
asctime = <built-in function asctime>
clock = <built-in function clock>
ctime = <built-in function ctime>
daylight = 1
gmtime = <built-in function gmtime>
localtime = <built-in function localtime>
mktime = <built-in function mktime>
newattr = hello!
sleep = <built-in function sleep>
strftime = <built-in function strftime>
strptime = <built-in function strptime>
struct_time = <type 'time.struct_time'>
time = <built-in function time>
timezone = 18000
tzname = ('EST', 'EDT')\end{verbatim}
\end{quote}

The list of the built-in Python types can be found in the \texttt{types} module:
\begin{quote}
\begin{verbatim}>>> import types
>>> t_dict=dict([(k,v) for (k,v) in vars(types).iteritems() 
... if k.endswith('Type')])
>>> for t in t_dict: print t,
...
DictType IntType TypeType FileType CodeType XRangeType EllipsisType 
SliceType BooleanType ListType MethodType TupleType ModuleType FrameType 
StringType LongType BuiltinMethodType BufferType FloatType ClassType 
DictionaryType BuiltinFunctionType UnboundMethodType UnicodeType 
LambdaType DictProxyType ComplexType GeneratorType ObjectType 
FunctionType InstanceType NoneType TracebackType\end{verbatim}
\end{quote}

For a pedagogical account of the most elementary 
Python introspection features,
Patrick O' Brien:
\href{http://www-106.ibm.com/developerworks/linux/library/l-pyint.html}{http://www-106.ibm.com/developerworks/linux/library/l-pyint.html}


%___________________________________________________________________________

\hypertarget{built-in-objects-iterators-and-generators}{}
\pdfbookmark[1]{Built-in objects: iterators and generators}{built-in-objects-iterators-and-generators}
\subsection*{Built-in objects: iterators and generators}

At the end of the last section , I have used the \texttt{iteritems} method 
of the dictionary, which returns an iterator:
\begin{quote}
\begin{verbatim}>>> dict.iteritems.__doc__
'D.iteritems() -> an iterator over the (key, value) items of D'\end{verbatim}
\end{quote}

Iterators (and generators) are new features of Python 2.2 and could not be
familiar to all readers. However, since they are unrelated to OOP, they 
are outside the scope of this book and will not be discussed here in detail. 
Nevertheless, I will give a typical example of use of a generator, since
this construct will be used in future chapters.

At the syntactical level, a generator is a ``function'' with (at least one) 
\texttt{yield} statement (notice that in Python 2.2 the \texttt{yield} statement is
enabled trough the \texttt{from {\_}{\_}future{\_}{\_} import generators} syntax):
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~re}\\
\mbox{}\\
\mbox{def~generateblocks(regexp,text):}\\
\mbox{~~~~"Generator~splitting~text~in~blocks~according~to~regexp"}\\
\mbox{~~~~start=0}\\
\mbox{~~~~for~MO~in~regexp.finditer(text):}\\
\mbox{~~~~~~~~beg,end=MO.span()}\\
\mbox{~~~~~~~~yield~text[start:beg]~{\#}~actual~text}\\
\mbox{~~~~~~~~yield~text[beg:end]~{\#}~separator}\\
\mbox{~~~~~~~~start=end}\\
\mbox{~~~~lastblock=text[start:]~}\\
\mbox{~~~~if~lastblock:~yield~lastblock;~yield~''}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

In order to understand this example, the reader my want to refresh his/her 
understanding of regular expressions; since this is not a subject for 
this book, I simply remind the meaning of \texttt{finditer}:
\begin{quote}
\begin{verbatim}>>> import re
>>> help(re.finditer)
finditer(pattern, string)
    Return an iterator over all non-overlapping matches in the
    string.  For each match, the iterator returns a match object.
    Empty matches are included in the result.\end{verbatim}
\end{quote}

Generators can be thought of as resumable functions that stop at the
\texttt{yield} statement and resume from the point where they left.
\begin{quote}
\begin{verbatim}>>> from oopp import generateblocks
>>> text='Python_Rules!'
>>> g=generateblocks(re.compile('_'),text)
>>> g
<generator object at 0x401b140c>
>>> dir(g)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', 
 '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', 
 '__repr__', '__setattr__', '__str__', 'gi_frame', 'gi_running', 'next']\end{verbatim}
\end{quote}

Generator objects can be used as iterators in a \texttt{for} loop.
In this example the generator takes a text and a regular expression
describing a fixed delimiter; then it splits the text in blocks
according to the delimiter. For instance, if the delimiter is
'{\_}', the text 'Python Rules!' is splitted as 'Python', '{\_}' and 'Rules!':
\begin{quote}
\begin{verbatim}>>> for n, block in enumerate(g): print n, block
...
0 Python
1 
2 Rules!
3\end{verbatim}
\end{quote}

This example also show the usage of the new Python 2.3 built-in \texttt{enumerate}.

Under the hood the \texttt{for} loop is calling the generator via its 
\texttt{next} method, until the \texttt{StopIteration} exception is raised.
For this reason a new call to the \texttt{for} loop will have no effect:
\begin{quote}
\begin{verbatim}>>> for n, block in enumerate(g): print n, block
...\end{verbatim}
\end{quote}

The point is that the generator has already yield its last element:
\begin{quote}
\begin{verbatim}>>> g.next() # error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
StopIteration\end{verbatim}
\end{quote}

\texttt{generateblocks} always returns an even number of blocks; odd blocks
are delimiters whereas even blocks are the intertwining text; there may be 
empty blocks, corresponding to the null string ''.

It must be remarked the difference with the 'str.split' method
\begin{quote}
\begin{verbatim}>>> 'Python_Rules!'.split('_')
['Python', 'Rules!']\end{verbatim}
\end{quote}

and the regular expression split method:
\begin{quote}
\begin{verbatim}>>> re.compile('_').split('Python_Rules!')
['Python', 'Rules!']\end{verbatim}
\end{quote}

both returns lists with an odd number of elements and both miss the separator. 
The regular expression split method can catch the separator, if wanted,
\begin{quote}
\begin{verbatim}>>> re.compile('(_)').split('Python_Rules!')
['Python', '_', 'Rules!']\end{verbatim}
\end{quote}

but still is different from the generator, since it returns a list. The
difference is relevant if we want to split a very large text, since 
the generator avoids to build a very large list and thus it is much more
memory efficient (it is faster, too). Moreover, \texttt{generateblocks}
works differently in the case of multiple groups:
\begin{quote}
\begin{verbatim}>>> delim=re.compile('(_)|(!)') #delimiter is space or exclamation mark
>>> for n, block in enumerate(generateblocks(delim,text)): 
...     print n, block
0 Python
1 _
2 Rules
3 !\end{verbatim}
\end{quote}

whereas
\begin{quote}
\begin{verbatim}>>> delim.split(text)
['Python', '_', None, 'Rules', None, '!', '']\end{verbatim}
\end{quote}

gives various unwanted \texttt{None} (which could be skipped with 
\texttt{[x for x in delim.split(text) if x is not None]}); notice, that
there are no differences (apart from the fact that \texttt{delim.split(text)}
has an odd number of elements) when one uses a single group regular expression:
\begin{quote}
\begin{verbatim}>>> delim=re.compile('(_|!)')
>>> delim.split(text)
['Python', '_', 'Rules', '!', '']\end{verbatim}
\end{quote}

The reader unfamiliar with iterators and generators is encouraged
to look at the standard documentation and other 
references. For instance, there are Alex Martelli's notes on iterators at 
\href{http://www.strakt.com/dev_talks.html}{http://www.strakt.com/dev{\_}talks.html}
and there is a good article on generators by David Mertz
\href{http://www-106.ibm.com/developerworks/linux/library/l-pycon.html}{http://www-106.ibm.com/developerworks/linux/library/l-pycon.html}


%___________________________________________________________________________

\hypertarget{the-convenience-of-functions}{}
\pdfbookmark[0]{THE CONVENIENCE OF FUNCTIONS}{the-convenience-of-functions}
\section*{THE CONVENIENCE OF FUNCTIONS}

Functions are the most basic Python objects. They are also the simplest
objects where one can apply the  metaprogramming techniques that are
the subject of this book. The tricks used in this chapter and the utility
functions defined here will be used over all the book. Therefore this
is an \emph{essential} chapter.

Since it is intended to be a gentle introduction, the tone will be
informal.


%___________________________________________________________________________

\hypertarget{id16}{}
\pdfbookmark[1]{Introduction}{id16}
\subsection*{Introduction}

One could be surprised that a text on OOP begins with a chapter on the
well known old-fashioned functions. In some sense, this is also
against the spirit of an important trend in OOP, which tries to
shift the focus from functions to data. In pure OOP languages,
there are no more functions, only methods. [\hyperlink{id15}{8}]

However, there are good reasons for that:
\newcounter{listcnt11}
\begin{list}{\arabic{listcnt11}.}
{
\usecounter{listcnt11}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
In Python,  functions  \emph{are} objects. And particularly useful ones.

\item {} 
Python functions are pretty powerful and all their secrets are probably
\emph{not} well known to the average Python programmer.

\item {} 
In the solutions of many problems, you don't need the full apparatus
of OOP: good old functions can be enough.

\end{list}

Moreover, I am a believer in the multiparadigm approach to programming, 
in which you choose your tools according to your problem. 
With a  bazooka you can kill a mosquito, yes, but this does not mean 
that you must use the bazooka \emph{always}. 
In certain languages, you have no choice, and you must define
a class (involving a lot of boiler plate code) even for the most trivial
application. Python's philosophy is to keep simple things simple, but
having the capability of doing even difficult things with a reasonable
amount of effort. The message of this chapter will be: ``use functions when 
you don't need classes''. Functions are good because:
\newcounter{listcnt12}
\begin{list}{\arabic{listcnt12}.}
{
\usecounter{listcnt12}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
They are easy to write (no boiler plate);

\item {} 
They are easy to understand;

\item {} 
They can be reused in your code;

\item {} 
Functions are an essential building block in the construction of objects.

\end{list}

Even if I think that OOP is an extremely effective strategy, with
enormous advantages on design, maintanibility and reusability of code,
nevertheless this book is \emph{not} intended to be a panegyric of OOP. There
are cases in which you don't need OOP. I think the critical parameter is
the size of the program. These are the rules I follows usually (to be
taken as indicative):
\newcounter{listcnt13}
\begin{list}{\arabic{listcnt13}.}
{
\usecounter{listcnt13}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
If I have to write a short script of 20-30 lines, that copies two or 
three files and prints some message, I use fast and dirty spaghetti-code; 
there is no use for OOP.

\item {} 
If your script grows to one-hundred lines or more, I structure
it  write a few routines and a main program: but still I can live
without OOP.

\item {} 
If the script goes beyond the two hundred lines, I start
collecting my routines in few classes.

\item {} 
If the script goes beyond the five hundred lines, I split the program
in various files and modules and convert it to a package.

\item {} 
I never write a function longer than 50 lines, since 50 lines is more
or less the size of a page in my editor, and I need to be able to
see the entire function in a page.

\end{list}

Of course your taste could be different and you could prefer to write a 
monolitic program of five thousand lines; however the average size of 
the modules in the Python standard library is of 111 lines.
I think this is a \emph{strong} suggestion towards 
a modular style of programming, which
is \emph{very} well supported in Python.

The point is that OOP is especially useful for \emph{large} programs: if you
only use Python for short system administration scripts you may well
live without OOP. Unfortunaly, as everybody knows, short scripts have
an evil tendency to become medium size scripts, and medium size scripts
have the even more evil tendency to become large scripts and possible
even full featured applications ! For this reason it is very probable
that at a certain moment you will feel the need for OOP.

I remember my first big program, a long time ago: I wrote a program
to draw mathematical functions in AmigaBasic. It was good and nice
until it had size of few hundred lines; but when it passed a thousand
of lines, it became rapidly unmanageable and unmaintenable. There where
three problems:
\newcounter{listcnt14}
\begin{list}{\arabic{listcnt14}.}
{
\usecounter{listcnt14}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
I could not split the program in modules, as I wanted, due to the
limitations of AmigaBasic;

\item {} 
I was missing OOP to keep the logic of the program all together, but
at the time I didn't know that;

\item {} 
I was missing effective debugging techniques.

\item {} 
I was missing effective refactoring tools.

\end{list}

I am sure anybody who has ever written a large program has run in these
limitations: and the biggest help of OOP is in overcoming these limitations.
Obviously, miracles are impossible, and even object oriented programs can
grow to a size where they become unmaintanable: the point is that the
critical limit is much higher than the thousand lines of structured programs.
I haven't yet reached the limit of unmanageability with Python. The fact
that the standard library is 66492 lines long (as result from the total
number of lines in \texttt{/usr/local/lib/python2.2/}), but it is still manageable,
give me an hope ;-)
\begin{quote}
\begin{figure}[b]\hypertarget{id18}[9]
However, one could argue that having functions distinguished from 
methods is the best thing to do, even in a strongly object-oriented 
world. For instance, generic functions can be used to implement 
multimethods. See for instance Lisp, Dylan and MultiJava. This latter 
is forced to introduce the concept of function outside a class, 
foreign to traditional Java, just to implement multimethods.
\end{figure}
\end{quote}


%___________________________________________________________________________

\hypertarget{a-few-useful-functions}{}
\pdfbookmark[1]{A few useful functions}{a-few-useful-functions}
\subsection*{A few useful functions}

It is always a good idea to have a set of useful function collected in
a user defined module. The first function we want to have in our module
is the \texttt{do{\_}nothing} function:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~do{\_}nothing(*args,**kw):~pass}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

This function accept a variable number of arguments and keywords (I
defer the reader to the standard documentation if she is unfamiliar
with these concept; this is \emph{not} another Python tutorial ;-) and
return \texttt{None}. It is very useful for debugging purposes, when in a
complex program you may want concentrate your attention to few crucial
functions and set the non-relevant functions to \texttt{do{\_}nothing} functions.

A second function which is useful in developing programs is a timer
function. Very ofter indeed,  we may want to determine the bottleneck
parts of a program, we are interested in profiling them and in seeing 
if we can improve the speed by improving the algorithm, or by using
a Python ``compiler'' such as Psyco, or if really we need to write a C 
extension. In my experience, I never needed to write a C extension,
since Python is fast enough. Nevertheless, to profile a program is
always a good idea and Python provides a profiler module in the 
stardard library with this aim. Still, it is convenient to have
a set of user defined functions to test the execution speed of
few selected routines (whereas the standard profiler profiles everything).

We see from the standard library documentation that
the current time can be retrieved from the \texttt{time} module: [\hyperlink{id18}{9}]
\begin{quote}
\begin{verbatim}>>> import time
>>> time.asctime()
'Wed Jan 15 12:46:03 2003'\end{verbatim}
\end{quote}

Since we are not interested in the date but only in the time, we need
a function to extract it. This is easily implemented:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~time}\\
\mbox{}\\
\mbox{def~get{\_}time():}\\
\mbox{~~~~"Return~the~time~of~the~system~in~the~format~HH:MM:SS"}\\
\mbox{~~~~return~time.asctime().split()[3]}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{>>>~from~oopp~import~get{\_}time}\\
\mbox{>>>~get{\_}time()}\\
\mbox{'13:03:49'}
\end{flushleft}\end{ttfamily}
\end{quote}

Suppose, for instance, we want to know how much it takes to Python
to write a Gigabyte of data. This can be a quite useful benchmark
to have an idea of the I/O bottlenecks in our system. Since to take in memory
a file of a Gigabyte can be quite problematic, let me compute the
time spent in writing 1024 files of one Megabyte each. To this
aim we need a \texttt{writefile} function
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~writefile(fname,data):}\\
\mbox{~~~~f=file(fname,'w')}\\
\mbox{~~~~f.write(data)}\\
\mbox{~~~~f.close()}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

and timing function. The idea is to wrap the \texttt{writefile} function in
a \texttt{with{\_}clock} function as follows:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~with{\_}clock(func,n=1):}\\
\mbox{~~~~def~{\_}(*args,**kw):~{\#}~this~is~a~closure}\\
\mbox{~~~~~~~~print~"Process~started~on",get{\_}time()}\\
\mbox{~~~~~~~~print~'~..~please~wait~..'}\\
\mbox{~~~~~~~~for~i~in~range(n):~func(*args,**kw)}\\
\mbox{~~~~~~~~print~"Process~ended~on",get{\_}time()}\\
\mbox{~~~~return~{\_}}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The wrapper function \texttt{with{\_}clock} has converted the function \texttt{writefile} 
in a function \texttt{with{\_}clock(writefile)} which has the same arguments
of \texttt{writefile}, but contains additional features: in this case
timing capabilities. Technically speaking, the internal function \texttt{{\_}}
is called a \emph{closure}. Closures are very common in functional languages 
and can be used in Python too, with very little effort [\hyperlink{id21}{10}].

I will use closures very often in the following, and I will use
the convention of denoting with ``{\_}'' the inner 
function in the closure, since there is no reason of giving to it a 
descriptive name (the name 'with{\_}clock' in the outer function 
is descriptive enough). For the same, reason I do not use a 
docstring for ``{\_}''. If Python would allow multistatement lambda
functions, ``{\_}'' would be a good candidate for an anonymous function.

Here is an example of usage:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> data='*'*1024*1024 #one megabyte
>>> with_clock(writefile,n=1024)('datafile',data) #.
Process started on 21:20:01
 .. please wait ..
Process ended on 21:20:57\end{verbatim}
\end{quote}

This example shows that Python has written one Gigabyte of data (splitted in
1024 chunks of one Megabyte each) in less than a minute. However,the 
result depends very much on the filesystem. I always suggest people
to profile their programs, since one \emph{always} find surprises.
For instance, I have checked the performance of my laptop, 
a dual machine Windows 98 SE/ Red Hat Linux 7.3.
The results are collected in the following table:
\begin{quote}

\begin{longtable}[c]{|p{0.21\locallinewidth}|p{0.26\locallinewidth}|p{0.30\locallinewidth}|}
\hline
\textbf{
Linux ext-3
} & \textbf{
FAT under Linux
} & \textbf{
FAT under Windows 98
} \\ \hline
\endhead
%[visit_tbody]

24-25 s
 & 
56-58 s
 & 
86-88 s
 \\ \hline
%[depart_tbody]
\end{longtable}
\end{quote}

We see that Linux is \emph{much} faster: more than three times faster than
Windows, using the same machine! Notice that the FAT filesystem under
Linux (where it is \emph{not} native) is remarkably faster than the FAT
under Windows 98, where it is native !! I think that now my readers
can begin to understand why this book has been written under Linux 
and why I \emph{never} use Windows for programming (actually I use it only 
to see the DVD's ;-).

I leave as an exercise for the reader to check the results on this
script on their machine. Since my laptop is quite old, you will probably
have much better performances (for instance on my linux desktop I can
write a Gigabyte in less than 12 seconds!). However, there are \emph{always}
surprises: my desktop is a dual Windows 2000 machine with three different
filesystems, Linux ext-2, FAT and NTFS. Surprisingly enough, the NT
filesystem is the more inefficient for writing, \emph{ten times slower} 
than Linux!
\begin{quote}

\begin{longtable}[c]{|p{0.21\locallinewidth}|p{0.26\locallinewidth}|p{0.30\locallinewidth}|}
\hline
\textbf{
Linux ext-2
} & \textbf{
FAT under Win2000
} & \textbf{
NTFS under Win2000
} \\ \hline
\endhead
%[visit_tbody]

11-12 s
 & 
95-97 s
 & 
117-120 s
 \\ \hline
%[depart_tbody]
\end{longtable}
\end{quote}
\begin{figure}[b]\hypertarget{id21}[10]
Users of Python 2.3 can give a look to the new \texttt{datetime} module,
if they are looking for a sophisticated clock/calendar.
\end{figure}
\begin{figure}[b]\hypertarget{id22}[11]
There are good references on functional programming in Python; 
I suggest the Python Cookbook and the articles by David Mertz
www.IBM.dW.
\end{figure}


%___________________________________________________________________________

\hypertarget{functions-are-objects}{}
\pdfbookmark[1]{Functions are objects}{functions-are-objects}
\subsection*{Functions are objects}

As we said in the first chapter, objects have attributes accessible with the 
dot notation. This is not surprising at all. However, it could be
surprising to realize that since Python functions are objects, they
can have attributes, too. This could be surprising since this feature is quite 
uncommon: typically or i) the language is
not object-oriented, and therefore functions are not objects, or ii)
the language is strongly object-oriented and does not have functions, only 
methods. Python is a multiparadigm language (which I prefer to the
term ``hybrid'' language), therefore it has functions that are objects,
as in Lisp and other functional languages. 
Consider for instance the \texttt{get{\_}time} function.
That function has at least an useful attribute, its doctring:
\begin{quote}
\begin{verbatim}>>> from oopp import get_time
>>> print get_time.func_doc
Return the time of the system in the format HH:MM:SS\end{verbatim}
\end{quote}

The docstring can also be obtained with the \texttt{help} function:
\begin{quote}
\begin{verbatim}>>> help(get_time)
Help on function get_time in module oopp:
get_time()
    Return the time of the system in the format HH:MM:SS\end{verbatim}
\end{quote}

Therefore \texttt{help} works on user-defined functions, too, not only on
built-in functions. Notice that \texttt{help} also returns the argument list of 
the function. For instance, this is
the help message on the \texttt{round} function that we will use in the
following:
\begin{quote}
\begin{verbatim}>>> help(round)
Help on built-in function round:
round(...)
    round(number[, ndigits]) -> floating point number
    Round a number to a given precision in decimal digits (default 0 
    digits).This always returns a floating point number.  Precision may 
    be negative.\end{verbatim}
\end{quote}

I strongly recommend Python programmers to use docstrings, not
only for clarity sake during the development, but especially because
it is possible to automatically generate nice HTML documentation from 
the docstrings, by using the standard tool ``pydoc''.

One can easily add attributes to a function. For instance:
\begin{quote}
\begin{verbatim}>>> get_time.more_doc='get_time invokes the function time.asctime'
>>> print get_time.more_doc
get_time invokes the function time.asctime\end{verbatim}
\end{quote}

Attributes can be functions, too:
\begin{quote}
\begin{verbatim}>>> def IamAfunction(): print "I am a function attached to a function"
>>> get_time.f=IamAfunction
>>> get_time.f()
I am a function attached to a function\end{verbatim}
\end{quote}

This is a quite impressive potentiality of Python functions, which has
no direct equivalent in most other languages.

One possible application is to fake C ``static'' variables. Suppose
for instance we need a function remembering how may times it is
called: we can simply use
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<double.py>}\\
\mbox{}\\
\mbox{def~double(x):}\\
\mbox{~~~~try:~{\#}look~if~double.counter~is~defined}\\
\mbox{~~~~~~~~double.counter}\\
\mbox{~~~~except~AttributeError:}\\
\mbox{~~~~~~~~double.counter=0~{\#}first~call}\\
\mbox{~~~~double.counter+=1}\\
\mbox{~~~~return~2*x}\\
\mbox{}\\
\mbox{double(double(2))}\\
\mbox{print~"double~has~been~called~{\%}s~times"~{\%}~double.counter}\\
\mbox{}\\
\mbox{{\#}</double.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output \texttt{double has been called 2 times}.
A more elegant approach involves closures. A closure can enhance an
ordinary function, providing to it the capability of remembering 
the results of its previous calls and avoiding the duplication of
computations:
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~withmemory(f):}\\
\mbox{~~~~"""This~closure~invokes~the~callable~object~f~only~if~need~there~is"""}\\
\mbox{~~~~argskw=[];~result=[]}\\
\mbox{~~~~def~{\_}(*args,**kw):~}\\
\mbox{~~~~~~~~akw=args,kw}\\
\mbox{~~~~~~~~try:~{\#}~returns~a~previously~stored~result}\\
\mbox{~~~~~~~~~~~~i=argskw.index(akw)}\\
\mbox{~~~~~~~~except~ValueError:~{\#}~there~is~no~previously~stored~result}\\
\mbox{~~~~~~~~~~~~res=f(*args,**kw)~~{\#}~returns~the~new~result}\\
\mbox{~~~~~~~~~~~~argskw.append(akw)~{\#}~update~argskw}\\
\mbox{~~~~~~~~~~~~result.append(res)~{\#}~update~result}\\
\mbox{~~~~~~~~~~~~return~res}\\
\mbox{~~~~~~~~else:}\\
\mbox{~~~~~~~~~~~~return~result[i]~~}\\
\mbox{~~~~{\_}.argskw=argskw~{\#}makes~the~argskw~list~accessible~outside}\\
\mbox{~~~~{\_}.result=result~{\#}makes~the~result~list~accessible~outside}\\
\mbox{~~~~return~{\_}}\\
\mbox{}\\
\mbox{def~memoize(f):}\\
\mbox{~~~~"""This~closure~remembers~all~f~invocations"""}\\
\mbox{~~~~argskw,result~=~[],[]}\\
\mbox{~~~~def~{\_}(*args,**kw):~}\\
\mbox{~~~~~~~~akw=args,kw}\\
\mbox{~~~~~~~~try:~{\#}~returns~a~previously~stored~result}\\
\mbox{~~~~~~~~~~~~return~result[argskw.index(akw)]}\\
\mbox{~~~~~~~~except~ValueError:~{\#}~there~is~no~previously~stored~result}\\
\mbox{~~~~~~~~~~~~argskw.append(akw)~{\#}~update~argskw}\\
\mbox{~~~~~~~~~~~~result.append(f(*args,**kw))~{\#}~update~result}\\
\mbox{~~~~~~~~~~~~return~result[-1]~{\#}~return~the~new~result}\\
\mbox{~~~~{\_}.argskw=argskw~{\#}makes~the~argskw~list~accessible~outside}\\
\mbox{~~~~{\_}.result=result~{\#}makes~the~result~list~accessible~outside}\\
\mbox{~~~~return~{\_}}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}

Now, if we call the wrapped function \texttt{f}  twice with the same arguments, 
Python can give the result without repeating the (possibly very long) 
computation.
\begin{quote}
\begin{verbatim}>>> def f(x):
...    print 'called f'
...    return x*x
>>> wrapped_f=withmemory(f)
>>> wrapped_f(2) #first call with the argument 2; executes the computation
called f
4
>>> wrapped_f(2) #does not repeat the computation
4
>>> wrapped_f.result
[4]
>>> wrapped_f.argskw
[((2,), {})]\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{profiling-functions}{}
\pdfbookmark[1]{Profiling functions}{profiling-functions}
\subsection*{Profiling functions}

The \texttt{with{\_}clock} function provided before was intended to be
pedagogical; as such it is a quite poor solution to the
problem of profiling a Python routine. A better solution involves
using two others functions in the time library, \texttt{time.time()} 
that gives that time in seconds elapsed from a given date, and 
\texttt{time.clock()} that gives the time spent by the CPU in a given 
computation. Notice that \texttt{time.clock()} has not an infinite
precision (the precision depends on the system) and one 
should expect relatively big errors if the function runs in
a very short time. That's the reason why it is convenient
to execute multiple times short functions and divide the total
time by the number of repetitions. Moreover, one should subtract the
overhead do to the looping. This can be computed with the following
routine:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~loop{\_}overhead(N):}\\
\mbox{~~~~"Computes~the~time~spent~in~empty~loop~of~N~iterations"}\\
\mbox{~~~~t0=time.clock()}\\
\mbox{~~~~for~i~in~xrange(N):~pass}\\
\mbox{~~~~return~time.clock()-t0}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

For instance, on my laptop an empty loop of one million of iterations
is performed in 1.3 seconds. Typically the loop overhead is negligible,
whereas the real problem is the function overhead.

Using the attribute trick discussed above, we may
define a \texttt{with{\_}timer} function that enhances quite a bit 
\texttt{with{\_}clock}:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~with{\_}timer(func,~modulename='{\_}{\_}main{\_}{\_}',~n=1,~logfile=sys.stdout):}\\
\mbox{~~~~"""Wraps~the~function~func~and~executes~it~n~times~(default~n=1).~}\\
\mbox{~~~~The~average~time~spent~in~one~iteration,~express~in~milliseconds,~}\\
\mbox{~~~~is~stored~in~the~attributes~func.time~and~func.CPUtime,~and~saved~}\\
\mbox{~~~~in~a~log~file~which~defaults~to~the~standard~output.}\\
\mbox{~~~~"""}\\
\mbox{~~~~def~{\_}(*args,**kw):~{\#}~anonymous~function}\\
\mbox{~~~~~~~~time1=time.time()}\\
\mbox{~~~~~~~~CPUtime1=time.clock()}\\
\mbox{~~~~~~~~print~'Executing~{\%}s.{\%}s~...'~{\%}~(modulename,func.{\_}{\_}name{\_}{\_}),}\\
\mbox{~~~~~~~~for~i~in~xrange(n):~res=func(*args,**kw)~{\#}~executes~func~n~times}\\
\mbox{~~~~~~~~time2=time.time()}\\
\mbox{~~~~~~~~CPUtime2=time.clock()}\\
\mbox{~~~~~~~~func.time=1000*(time2-time1)/n}\\
\mbox{~~~~~~~~func.CPUtime=1000*(CPUtime2-CPUtime1-loop{\_}overhead(n))/n}\\
\mbox{~~~~~~~~if~func.CPUtime<10:~r=3~{\#}better~rounding}\\
\mbox{~~~~~~~~else:~r=1~{\#}default~rounding}\\
\mbox{~~~~~~~~print~>>~logfile,~'Real~time:~{\%}s~ms'~{\%}~round(func.time,r),}\\
\mbox{~~~~~~~~print~>>~logfile,~'~CPU~time:~{\%}s~ms'~{\%}~round(func.CPUtime,r)}\\
\mbox{~~~~~~~~return~res}\\
\mbox{~~~~return~{\_}}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here it is an example of application:
\begin{quote}
\begin{verbatim}>>> from oopp import with_timer,writefile
>>> data='*'*1024*1024 #one megabyte
>>> with_timer(writefile,n=1024)('datafile',data) #.
Executing writefile ... Real time: 60.0 ms  CPU time: 42.2 ms\end{verbatim}
\end{quote}

The CPU time can be quite different from the real time, 
as you can see in the following example:
\begin{quote}
\begin{verbatim}>>> import time
>>> def sleep(): time.sleep(1)
...
>>> with_timer(sleep)() #.
Executing sleep ... Real time: 999.7 ms  CPU time: 0.0 ms\end{verbatim}
\end{quote}

We see that Python has run for 999.7 ms (i.e. 1 second, up to
approximation errors in the system clock) during which the CPU has 
worked for 0.0 ms (i.e. the CPU took a rest ;-).
The CPU time is the relevant time to use with the purpose of
benchmarking Python speed.

I should notice that the approach pursued in \texttt{with{\_}timer} is still
quite simple. A better approach would be to
plot the time versus the number of iteration, do a linear interpolation
and extract the typical time for iteration from that. This allows
to check visually that the machine is not doing something strange
during the execution time and it is what
I do in my personal benchmark routine; doing something similar is
left as an exercise for the reader ;-).

Another approach is to use the \texttt{timeit.py} module (new in Python 2.3,
but works also with Python 2.2):
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~timeit,{\_}{\_}main{\_}{\_},warnings}\\
\mbox{}\\
\mbox{warnings.filterwarnings('ignore',}\\
\mbox{'import~{\textbackslash}*~only~allowed~at~module~level',SyntaxWarning)}\\
\mbox{}\\
\mbox{def~timeit{\_}(stmt,setup='from~{\_}{\_}main{\_}{\_}~import~*',n=1000):}\\
\mbox{~~~~t=timeit.Timer(stmt,setup)}\\
\mbox{~~~~try:~print~t.repeat(number=n)~{\#}~class~timeit~3~times}\\
\mbox{~~~~except:~t.print{\_}exc()}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

It is often stated that Python is slow and quite ineffective
in application involving hard computations. This is generally speaking
true, but how bad is the situation ? To test the (in)efficiency of
Python on number crunching, let me give a function to compute the
Mandelbrot set, which I have found in the Python Frequently Asked
Question (FAQ 4.15. \emph{Is it possible to write obfuscated one-liners 
in Python?}).
This function is due to Ulf Bartelt and you should ask him to know how
does it work ;-)
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~mandelbrot(row,col):}\\
\mbox{~~~~"Computes~the~Mandelbrot~set~in~one~line"}\\
\mbox{~~~~return~(lambda~Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(}\\
\mbox{~~~~~~~~lambda~x,y:x+y,map(lambda~y,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=}\\
\mbox{~~~~~~~~lambda~yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,~Sx=Sx,Sy=Sy:reduce(}\\
\mbox{~~~~~~~~lambda~x,y:x+y,map(lambda~x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,~i=i,}\\
\mbox{~~~~~~~~Sx=Sx,F=lambda~xc,yc,x,y,k,f=lambda~xc,yc,x,y,k,f:(k<=0)}\\
\mbox{~~~~~~~~or~(x*x+y*y>=4.0)~or~1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):}\\
\mbox{~~~~~~~~f(xc,yc,x,y,k,f):chr(64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),}\\
\mbox{~~~~~~~~range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy))))(}\\
\mbox{~~~~~~~~-2.1,~0.7,~-1.2,~1.2,~30,~col,~row)}\\
\mbox{~~~~{\#}~~~~{\textbackslash}{\_}{\_}{\_}~{\_}{\_}{\_}/~~{\textbackslash}{\_}{\_}{\_}~{\_}{\_}{\_}/~~|~~~|~~~~|{\_}~lines~on~screen}\\
\mbox{~~~~{\#}~~~~~~~~V~~~~~~~~~~V~~~~~~|~~~|{\_}{\_}{\_}{\_}{\_}{\_}~columns~on~screen}\\
\mbox{~~~~{\#}~~~~~~~~|~~~~~~~~~~|~~~~~~|{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}~maximum~of~"iterations"}\\
\mbox{~~~~{\#}~~~~~~~~|~~~~~~~~~~|{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}~range~on~y~axis}\\
\mbox{~~~~{\#}~~~~~~~~|{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}~range~on~x~axis}\\
\mbox{~~~~}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is the benchmark on my laptop:
\begin{quote}
\begin{verbatim}>>> from oopp import mandelbrot,with_timer
>>> row,col=24,75
>>> output=with_timer(mandelbrot,n=1)(row,col)
Executing __main__.mandelbrot ... Real time: 427.9 ms  CPU time: 410.0 ms
>>> for r in range(row): print output[r*col:(r+1)*col]
...
BBBBBBBBBBBBBBCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDCCCCCCCCCCCCCC
BBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGYLFFFEEEEEDDDDDCCCCCCCCC
BBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGIKNJLLGEEEEEEDDDDDDCCCCC
BBBBBBBBBCCCCCDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFFGHJJR^QLIHGFFEEEEEEDDDDDDCC
BBBBBBBBCCCDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGGGHIK_______LHGFFFFFEEEEDDDDDD
BBBBBBBCCDDDDDDDDDDDDDDDDDDDDEEEEEEEFFFGHILIIIJJKMS_____PLJJIHGGGHJFEEDDDDD
BBBBBBCDDDDDDDDDDDDDDDDDDEEEEEFFFFFFGGGHMQ__T________________QLOUP[OGFEDDDD
BBBBBCDDDDDDDDDDDDDDDEEEFFFFFFFFFGGGGHJNM________________________XLHGFFEEDD
BBBBCDDDDDDDDDEEEEEFFGJKHHHHHHHHHHHHIKN[__________________________MJKGFEEDD
BBBBDDDDEEEEEEEEFFFFGHIKPVPMNU_QMJJKKZ_____________________________PIGFEEED
BBBCDEEEEEEEEFFFFFFHHHML___________PQ_______________________________TGFEEEE
BBBDEEEEEEFGGGGHHHJPNQP^___________________________________________IGFFEEEE
BBB_____________________________________________________________OKIHGFFEEEE
BBBDEEEEEEFGGGGHHHJPNQP^___________________________________________IGFFEEEE
BBBCDEEEEEEEEFFFFFFHHHML___________PQ_______________________________TGFEEEE
BBBBDDDDEEEEEEEEFFFFGHIKPVPMNU_QMJJKKZ_____________________________PIGFEEED
BBBBCDDDDDDDDDEEEEEFFGJKHHHHHHHHHHHHIKN[__________________________MJKGFEEDD
BBBBBCDDDDDDDDDDDDDDDEEEFFFFFFFFFGGGGHJNM________________________XLHGFFEEDD
BBBBBBCDDDDDDDDDDDDDDDDDDEEEEEFFFFFFGGGHMQ__T________________QLOUP[OGFEDDDD
BBBBBBBCCDDDDDDDDDDDDDDDDDDDDEEEEEEEFFFGHILIIIJJKMS_____PLJJIHGGGHJFEEDDDDD
BBBBBBBBCCCDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGGGHIK_______LHGFFFFFEEEEDDDDDD
BBBBBBBBBCCCCCDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFFGHJJR^QLIHGFFEEEEEEDDDDDDCC
BBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGIKNJLLGEEEEEEDDDDDDCCCCC
BBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGYLFFFEEEEEDDDDDCCCCCCCCC\end{verbatim}
\end{quote}

I am willing to concede that this code is not typical Python code and
actually it could be an example of \emph{bad} code, but I wanted a nice ASCII 
picture on my book ... :) Also, this prove that Python is not necessarily 
readable and easy to understand ;-)
I leave for the courageous reader to convert the previous algorithm to C and
measure the difference in speed ;-)


%___________________________________________________________________________

\hypertarget{about-python-speed}{}
\pdfbookmark[1]{About Python speed}{about-python-speed}
\subsection*{About Python speed}

The best way to improved the speed is to improve the algorithm; in
this sense Python is an ideal language since it allows you to test
many algorithms in an incredibly short time: in other words, the time you 
would spend fighting with the compiler in other languages, in Python
can be used to improve the algorithm. 
However in some cases, there is little to do: for instance, in many
problems one has to run lots of loops, and Python loops are horribly
inefficients as compared to C loops. In this case the simplest possibility 
is to use Psyco. Psyco is a specialing Python compiler written by Armin
Rigo. It works for 386 based processors and allows Python to run loops at 
C speed. Installing Psyco requires {\$}0.00 and ten minutes of your time:
nine minutes to find the program, download it, and install it; one
minute to understand how to use it.

The following script explains both the usage and the advantages of Psyco:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<psyco1.py>}\\
\mbox{}\\
\mbox{import~oopp,sys}\\
\mbox{try:~}\\
\mbox{~~~~import~psyco}\\
\mbox{except~ImportError:~}\\
\mbox{~~~~print~"Psyco~is~not~installed,~sorry."}\\
\mbox{else:}\\
\mbox{~~~~n=1000000~{\#}~1,000,000~loops}\\
\mbox{}\\
\mbox{~~~~without=oopp.loop{\_}overhead(n)~}\\
\mbox{~~~~print~"Without~Psyco:",without}\\
\mbox{}\\
\mbox{~~~~psyco.bind(oopp.loop{\_}overhead)~{\#}compile~the~empty{\_}loop}\\
\mbox{}\\
\mbox{~~~~with=oopp.loop{\_}overhead(n)~}\\
\mbox{~~~~print~"With~Psyco:",with}\\
\mbox{}\\
\mbox{~~~~print~'Speedup~=~{\%}sx'~{\%}~round(without/with,1)}\\
\mbox{}\\
\mbox{{\#}</psyco1.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output is impressive:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Without~Psyco:~1.3}\\
\mbox{With~Psyco:~0.02}\\
\mbox{Speedup~=~65.0x}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that repeating the test, you will obtain different speedups.
On my laptop, the speedup for an empty loop of 10,000,000 of
iteration is of the order of 70x, which is the same speed of a C loop, 
actually (I checked it). On my desktop, I have even found a speedup of
94x !

However, I must say that Psyco has some limitations. The problem is
the function call overhead. Psyco enhances the overhead and in some
programs it can even \emph{worsen} the performance (this is way you should
\emph{never} use the \texttt{psyco.jit()} function that wraps all the functions of
your program: you should only wrap the bottleneck loops). Generally speaking, 
you should expect a much more modest improvement, a factor of 2 or 3
is what I obtain usually in my programs.

Look at this second example, which essentially measure the function 
call overhead by invoking the \texttt{do{\_}nothing} function:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<psyco2.py>}\\
\mbox{}\\
\mbox{import~oopp}\\
\mbox{try:~}\\
\mbox{~~~~import~psyco}\\
\mbox{except~ImportError:}\\
\mbox{~~~~print~"Psyco~is~not~installed,~sorry."}\\
\mbox{else:}\\
\mbox{~~~~n=10000~{\#}~10,000~loops}\\
\mbox{~}\\
\mbox{~~~~def~do{\_}nothing{\_}loop():}\\
\mbox{~~~~~~~~for~i~in~xrange(n):~oopp.do{\_}nothing()}\\
\mbox{}\\
\mbox{~~~~print~"Without~Psyco:{\textbackslash}n"}\\
\mbox{~~~~oopp.with{\_}timer(do{\_}nothing{\_}loop,n=5)()~{\#}50,000~times}\\
\mbox{}\\
\mbox{~~~~without=do{\_}nothing{\_}loop.CPUtime}\\
\mbox{}\\
\mbox{~~~~psyco.bind(do{\_}nothing{\_}loop)~}\\
\mbox{~~~~print~"With~Psyco:{\textbackslash}n"}\\
\mbox{~~~~oopp.with{\_}timer(do{\_}nothing{\_}loop,n=5)()~{\#}50,000~times}\\
\mbox{}\\
\mbox{~~~~with=do{\_}nothing{\_}loop.CPUtime}\\
\mbox{}\\
\mbox{~~~~print~'Speedup~=~{\%}sx'~{\%}~round(without/with,1)}\\
\mbox{}\\
\mbox{{\#}</psyco2.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output is less incredible:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Without~Psyco:}\\
\mbox{Executing~do{\_}nothing{\_}loop~...~Real~time:~138.2~ms~~CPU~time:~130.0~ms}\\
\mbox{With~Psyco:}\\
\mbox{Executing~do{\_}nothing{\_}loop~...~Real~time:~70.0~ms~~CPU~time:~68.0~ms}\\
\mbox{Speedup~=~1.9x}
\end{flushleft}\end{ttfamily}
\end{quote}

However, this is still impressive, if you think that you can double 
the speed of your program by adding \emph{a line} of code! Moreover this
example is not fair since Psyco cannot improve very much the performance 
for loops invoking functions with a variable number of arguments. On the
other hand, it can do quite a lot for loops invoking functions with 
a fixed number of arguments. I have checked that you can easily reach 
speedups of 20x (!). The only disadvantage is that a program invoking
Psyco takes much more memory, than a normal Python program, but this
is not a problem for most applications in nowadays computers. 
Therefore, often Psyco
can save you the effort of going trough a C extension. In some cases,
however, there is no hope: I leave as an exercise for the reader
to check (at least the version 0.4.1 I am using now) is unable to
improve the performance on the Mandelbrot set example. This proves
that in the case bad code, there is no point in using a compiler:
you have to improve the algorithm first !

By the way, if you really want to go trough a C extension with a minimal
departure from Python, you can use Pyrex by Greg Ewing. A Pyrex program
is essentially a Python program with variable declarations that is
automatically converted to C code. Alternatively, you can inline
C functions is Python with \texttt{weave} of ... 
Finally, if you want to access C/C++ libraries, there tools
like Swig, Booster and others.


%___________________________________________________________________________

\hypertarget{tracing-functions}{}
\pdfbookmark[1]{Tracing functions}{tracing-functions}
\subsection*{Tracing functions}

Typically, a script contains many functions that call themselves each
other when some conditions are satisfied. Also, typically during
debugging things do not work the way we would like and it is not
clear which functions are called, in which order they are called,
and which parameters are passed. The best way to know all these
informations, is to trace the functions in our script, and to write
all the relevant informations in a log file. In order to keep the
distinction between the traced functions and the original one, it
is convenient to collect all the wrapped functions in a separate dictionary. 
The tracing of a single function can be done with a closure 
like this:
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~with{\_}tracer(function,namespace='{\_}{\_}main{\_}{\_}',output=sys.stdout,~indent=[0]):}\\
\mbox{~~~~"""Closure~returning~traced~functions.~It~is~typically~invoked}\\
\mbox{~~~~trough~an~auxiliary~function~fixing~the~parameters~of~with{\_}tracer."""}\\
\mbox{~~~~def~{\_}(*args,**kw):}\\
\mbox{~~~~~~~~name=function.{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~~~~~i='~'*indent[0];~indent[0]+=4~{\#}~increases~indentation}\\
\mbox{~~~~~~~~output.write("{\%}s[{\%}s]~Calling~'{\%}s'~with~arguments{\textbackslash}n"~{\%}~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(i,namespace,name))}\\
\mbox{~~~~~~~~output.write("{\%}s~{\%}s~...{\textbackslash}n"~{\%}~(i,str(args)+str(kw)))}\\
\mbox{~~~~~~~~res=function(*args,**kw)}\\
\mbox{~~~~~~~~output.write("{\%}s[{\%}s.{\%}s]~called~with~result:~{\%}s{\textbackslash}n"}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~{\%}~(i,namespace,name,str(res)))}\\
\mbox{~~~~~~~~indent[0]-=4~{\#}~restores~indentation}\\
\mbox{~~~~~~~~return~res}\\
\mbox{~~~~return~{\_}~{\#}~the~traced~function}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}

Here is an example of usage:
\begin{quote}
\begin{verbatim}>>> from oopp import with_tracer
>>> def fact(n): # factorial function
...     if n==1: return 1
...     else: return n*fact(n-1)
>>> fact=with_tracer(fact)
>>> fact(3)
[__main__] Calling 'fact' with arguments
 (3,){} ...
    [__main__] Calling 'fact' with arguments
     (2,){} ...
        [__main__] Calling 'fact' with arguments
         (1,){} ...
        [__main__.fact] called with result: 1
    [__main__.fact] called with result: 2
[__main__.fact] called with result: 6
6\end{verbatim}
\end{quote}

The logic behind \texttt{with{\_}tracer} should be clear; the only trick is the
usage of a default list as a way to store a global indentation parameter.
Since \texttt{indent} is mutable, the value of \texttt{indent[0]} changes at any
recursive call of the traced function, resulting in a nested display.

Typically, one wants to trace all the functions in a given module; 
this can be done trough the following function:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{from~types~import~*}\\
\mbox{}\\
\mbox{isfunction=lambda~f:~isinstance(f,(FunctionType,BuiltinFunctionType))}\\
\mbox{}\\
\mbox{def~wrapfunctions(obj,wrapper,err=None,**options):}\\
\mbox{~~~~"Traces~the~callable~objects~in~an~object~with~a~dictionary"}\\
\mbox{~~~~namespace=options.get('namespace',getattr(obj,'{\_}{\_}name{\_}{\_}',''))}\\
\mbox{~~~~output=options.get('output',sys.stdout)}\\
\mbox{~~~~dic=dict([(k,wrapper(v,namespace,output))~}\\
\mbox{~~~~~~~~~~~~~~for~k,v~in~attributes(obj).items()~if~isfunction(v)])}\\
\mbox{~~~~customize(obj,err,**dic)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that 'wrapfunctions' accepts as first argument an object with 
a \texttt{{\_}{\_}dict{\_}{\_}} attribute (such as a module or a class) or with some 
explicit attributes (such as a simple object) and modifies it. One can 
trace a module as in this example:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<tracemodule.py>}\\
\mbox{}\\
\mbox{import~oopp,random}\\
\mbox{}\\
\mbox{oopp.wrapfunctions(random,oopp.with{\_}tracer)~}\\
\mbox{}\\
\mbox{random.random()}\\
\mbox{}\\
\mbox{{\#}</tracemodule.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{[random]~Calling~'random'~with~arguments}\\
\mbox{(){\{}{\}}~...}\\
\mbox{->~'random.random'~called~with~result:~0.175450439202}
\end{flushleft}\end{ttfamily}
\end{quote}

The beauty of the present approach is its generality: 'wrap' can be
used to add any kind of capabilities to a pre-existing module.
For instance, we could time the functions in a module, with the
purpose of looking at the bottlenecks. To this aim, it is enough
to use a 'timer' nested closure:

An example of calling is  \texttt{wrapfunction(obj,timer,iterations=1)}.

We may also compose our closures; for instance one could define a 
\texttt{with{\_}timer{\_}and{\_}tracer} closure:
\begin{quote}
\begin{verbatim}>>> with_timer_and_tracer=lambda f: with_timer(with_tracer(f))\end{verbatim}
\end{quote}

It should be noticed that Python comes with a standard profiler
(in my system it is located in \texttt{/usr/local/lib/python2.2/profile.py})
that allows to profile a script or a module (try 
python /usr/local/lib/python2.2/profile.py oopp.py)

or
\begin{quote}
\begin{verbatim}>>> import profile; help(profile)\end{verbatim}
\end{quote}

and see the on-line documentation.


%___________________________________________________________________________

\hypertarget{tracing-objects}{}
\pdfbookmark[1]{Tracing objects}{tracing-objects}
\subsection*{Tracing objects}

In this section, I will give a more sophisticated example, in which 
one can easily understand why the Python ability of changing methods and 
attributes during run-time, is so useful.
As a preparation to the real example, let me
first introduce an utility routine that allows the user
to add tracing capabilities to a given object.
Needless to say, this feature can be invaluable during debugging, or in trying
to understand the behaviour of a program written by others.

This routine is a little complex and needs some explanation.
\newcounter{listcnt15}
\begin{list}{\arabic{listcnt15}.}
{
\usecounter{listcnt15}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
The routine looks in the attributes of the object and try to access them.

\item {} 
If the access is possible, the routines looks for methods (methods
are recognized trough the \texttt{inspect.isroutine} function in the
standard library) and ignores regular attributes;

\item {} 
The routine try to override the original methods with improved ones,
that possess tracing capabilities;

\item {} 
the traced method is obtained with the wrapping trick discussed before.

\end{list}

I give  now the real life example that I have anticipated before.
Improvements and elaborations of this example can be useful to the
professional programmer, too. Suppose you have an XML text you want
to parse. Python provides excellent support for this kind of operation
and various standard modules. One of the most common is the \texttt{expat}
module (see the standard library documentation for more).

If you are just starting using the module, it is certainly useful
to have a way of tracing its behaviour; this is especially true if
you you find some unexpected error during the parsing of a document
(and this may happens even if you are an experience programmer ;-).

The tracing routine just defined can be used to trace the parser, as
it is exemplified in the following short script:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<expat.py>}\\
\mbox{}\\
\mbox{import~oopp,~xml.parsers.expat,~sys}\\
\mbox{}\\
\mbox{{\#}~text~to~be~parsed}\\
\mbox{text{\_}xml="""{\textbackslash}}\\
\mbox{<?xml~version="1.0"?>}\\
\mbox{<parent~id="dad">}\\
\mbox{<child~name="kid">Text~goes~here</child>}\\
\mbox{</parent>"""}\\
\mbox{}\\
\mbox{{\#}~a~few~do~nothing~functions}\\
\mbox{def~start(*args):~pass}\\
\mbox{def~end(*args):~pass}\\
\mbox{def~handler(*args):~pass}\\
\mbox{}\\
\mbox{{\#}~a~parser~object}\\
\mbox{p~=~xml.parsers.expat.ParserCreate()}\\
\mbox{}\\
\mbox{p.StartElementHandler~=~start}\\
\mbox{p.EndElementHandler~=~end}\\
\mbox{p.CharacterDataHandler~=~handler}\\
\mbox{}\\
\mbox{{\#}adds~tracing~capabilities~to~p}\\
\mbox{oopp.wrapfunctions(p,oopp.with{\_}tracer,~err=sys.stdout)}\\
\mbox{}\\
\mbox{p.Parse(text{\_}xml)}\\
\mbox{}\\
\mbox{{\#}</expat.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output is:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Error:~SetBase~cannot~be~set}\\
\mbox{Error:~Parse~cannot~be~set}\\
\mbox{Error:~ParseFile~cannot~be~set}\\
\mbox{Error:~GetBase~cannot~be~set}\\
\mbox{Error:~SetParamEntityParsing~cannot~be~set}\\
\mbox{Error:~ExternalEntityParserCreate~cannot~be~set}\\
\mbox{Error:~GetInputContext~cannot~be~set}\\
\mbox{[]~Calling~'start'~with~arguments}\\
\mbox{~(u'parent',~{\{}u'id':~u'dad'{\}}){\{}{\}}~...}\\
\mbox{[.start]~called~with~result:~None}\\
\mbox{[]~Calling~'handler'~with~arguments}\\
\mbox{~(u'{\textbackslash}n',){\{}{\}}~...}\\
\mbox{[.handler]~called~with~result:~None}\\
\mbox{[]~Calling~'start'~with~arguments}\\
\mbox{~(u'child',~{\{}u'name':~u'kid'{\}}){\{}{\}}~...}\\
\mbox{[.start]~called~with~result:~None}\\
\mbox{[]~Calling~'handler'~with~arguments}\\
\mbox{~(u'Text~goes~here',){\{}{\}}~...}\\
\mbox{[.handler]~called~with~result:~None}\\
\mbox{[]~Calling~'end'~with~arguments}\\
\mbox{~(u'child',){\{}{\}}~...}\\
\mbox{[.end]~called~with~result:~None}\\
\mbox{[]~Calling~'handler'~with~arguments}\\
\mbox{~(u'{\textbackslash}n',){\{}{\}}~...}\\
\mbox{[.handler]~called~with~result:~None}\\
\mbox{[]~Calling~'end'~with~arguments}\\
\mbox{~(u'parent',){\{}{\}}~...}\\
\mbox{[.end]~called~with~result:~None}
\end{flushleft}\end{ttfamily}
\end{quote}

This is a case where certain methods cannot be managed with 
\texttt{getattr/setattr}, because they are internally coded in C: this
explain the error messages at the beginning. I leave as an exercise 
for the reader to understand the rest ;-)


%___________________________________________________________________________

\hypertarget{inspecting-functions}{}
\pdfbookmark[1]{Inspecting functions}{inspecting-functions}
\subsection*{Inspecting functions}

Python wonderful introspection features are really impressive when applied
to functions. It is possible to extract a big deal of informations
from a Python function, by looking at its associated \emph{code object}.
For instance, let me consider  my, \texttt{do{\_}nothing} function: its associated
code object can be extracted from the \texttt{func{\_}code} attribute:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> co=do_nothing.func_code # extracts the code object
>>> co
<code object do_nothing at 0x402c5d20, file "oopp.py", line 48>
>>> type(co)
<type 'code'>\end{verbatim}
\end{quote}

The code object is far being trivial: the docstring says it all:
\begin{quote}
\begin{verbatim}>>> print type(co).__doc__
code(argcount, nlocals, stacksize, flags, codestring, constants, names,
      varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])
Create a code object.  Not for the faint of heart.\end{verbatim}
\end{quote}

In the case of my \texttt{do{\_}nothing} function, the code object 
possesses the following attributes:
\begin{quote}
\begin{verbatim}>>> print pretty(attributes(co))
co_argcount = 0
co_cellvars = ()
co_code = dS
co_consts = (None,)
co_filename = oopp.py
co_firstlineno = 48
co_flags = 15
co_freevars = ()
co_lnotab =
co_name = do_nothing
co_names = ()
co_nlocals = 2
co_stacksize = 1
co_varnames = ('args', 'kw')\end{verbatim}
\end{quote}

Some of these arguments are pretty technical and implementation dependent;
however, some of these are pretty clear and useful:
\begin{quote}
\begin{itemize}
\item {} 
co{\_}argcount is the total number of arguments

\item {} 
co{\_}filename is the name of the file where the function is defined

\item {} 
co{\_}firstlineno is the line number where the function is defined

\item {} 
co{\_}name is the name of the function

\item {} 
co{\_}varnames are the names

\end{itemize}
\end{quote}

The programmer that it is not a ``faint of heart'' can study
the built-in documentation on code objects; s/he should try
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{for~k,v~in~attributes(co).iteritems():~print~k,':',v.{\_}{\_}doc{\_}{\_},'{\textbackslash}n'}
\end{flushleft}\end{ttfamily}

{\#} does not work now !!
\begin{ttfamily}\begin{flushleft}
\mbox{add=[lambda~x,i=i:~x+i~for~i~in~range(10)]}\\
\mbox{}\\
\mbox{>>>~def~f(y):}\\
\mbox{...~~~~return~lambda~x:~x+y}\\
\mbox{...}\\
\mbox{>>>~f(1).func{\_}closure~{\#}closure~cell~object}\\
\mbox{(<cell~at~0x402b56bc:~int~object~at~0x811d6c8>,)}
\end{flushleft}\end{ttfamily}
\end{quote}

func.defaults, closure, etc.

{\#}how to extract (non-default) arguments as help does.

print (lambda:None).func{\_}code.co{\_}filename

One cannot change the name of a function:
\begin{quote}
\begin{verbatim}>>> def f(): pass
...
>>> f.__name__='ciao' # error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: readonly attribute\end{verbatim}
\end{quote}

However, one can create a copy with a different name:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~copyfunc(f,newname=None):~{\#}~works~under~Python~2.3}\\
\mbox{~~~~if~newname~is~None:~newname=f.func{\_}name~{\#}~same~name}\\
\mbox{~~~~return~FunctionType(f.func{\_}code,~globals(),~newname,~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~f.func{\_}defaults,~f.func{\_}closure)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{>>>~copyfunc(f,newname='f2')}\\
\mbox{<function~f2~at~0x403e233c>}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that the \texttt{copy} module would not do the job:
\begin{quote}
\begin{verbatim}>>> import copy
>>> copy.copy(f) # error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.3/copy.py", line 84, in copy
    y = _reconstruct(x, reductor(), 0)
  File "/usr/local/lib/python2.3/copy_reg.py", line 57, in _reduce
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle function objects\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{the-beauty-of-objects}{}
\pdfbookmark[0]{THE BEAUTY OF OBJECTS}{the-beauty-of-objects}
\section*{THE BEAUTY OF OBJECTS}

In this chapter I will show how to define generic objects in Python, and
how to manipulate them.


%___________________________________________________________________________

\hypertarget{user-defined-objects}{}
\pdfbookmark[1]{User defined objects}{user-defined-objects}
\subsection*{User defined objects}

In Python, one cannot directly modify methods and attributes of built-in 
types, since this would be a potentially frightening source of bugs. 
Imagine for instance of changing the sort method of a list and invoking an 
external module expecting the standard sort: all kind of hideous outcome 
could happen.

Nevertheless, in Python, as in all OOP languages, the user can define 
her own kind of objects, customized to satisfy her needs. In order to
define a new object, the user must define the class of the objects she 
needs. The simplest possible class is a do-nothing class:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Object(object):}\\
\mbox{~~~~"A~convenient~Object~class"}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Elements of the \texttt{Object} class can be created (instantiated) quite
simply:
\begin{quote}
\begin{verbatim}>>> from oopp import Object
>>> obj1=Object()
>>> obj1
<oopp.Object object at 0x81580ec>
>>> obj2=Object()
obj2
<object.Object object at 0x8156704>\end{verbatim}
\end{quote}

Notice that the hexadecimal number 0x81580ec is nothing else that the
unique object reference to \texttt{obj1}
\begin{quote}
\begin{verbatim}>>> hex(id(obj1))
 '0x81580ec'\end{verbatim}
\end{quote}

whereas 0x8156704 is the object reference of \texttt{obj2}:
\begin{quote}
\begin{verbatim}>>> hex(id(obj2))
'0x8156704'\end{verbatim}
\end{quote}

However, at this point \texttt{obj1} and \texttt{obj2} are generic 
doing nothing objects . Nevertheless, they have 
at least an useful attribute, the class docstring:
\begin{quote}
\begin{verbatim}>>> obj1.__doc__ #obj1 docstring
'A convenient Object class'
>>> obj2.__doc__ # obj2 docstring: it's the same
'A convenient Object class'\end{verbatim}
\end{quote}

Notice that the docstring is associate to the class and therefore all
the instances share the same docstring, unless one explicitly assigns
a different docstring to some instance. \texttt{{\_}{\_}doc{\_}{\_}}
is a class attribute (or a static attribute for readers familiar with the
C++/Java terminology) and the expression is actually syntactic sugar for
\begin{quote}
\begin{verbatim}>>> class Object(object): # with explicit assignement to __doc__
...    __doc__ = "A convenient Object class"\end{verbatim}
\end{quote}

Since instances of 'Object' can be modified, I can transform them in
anything I want. For instance, I can create a simple clock:
\begin{quote}
\begin{verbatim}>>> myclock=Object()
>>> myclock
<__main__.Object object at 0x8124614>\end{verbatim}
\end{quote}

A minimal clock should at least print the current time 
on the system. This is given by the \texttt{get{\_}time} function
we defined in the first chapter. We may ``attach'' that function 
to our clock as follows:
\begin{quote}
\begin{verbatim}>>> import oopp
>>> myclock.get_time=oopp.get_time
>>> myclock.get_time # this is a function, not a method
<function get_time at 0x815c40c>\end{verbatim}
\end{quote}

In other words, we have converted the \texttt{oopp.get{\_}time} function to a
\texttt{get{\_}time} function of the object \texttt{myclock}. The procedure works
\begin{quote}
\begin{verbatim}>>> myclock.get_time()
'15:04:57'\end{verbatim}
\end{quote}

but has a disadvantage: if we instantiate another
clock
\begin{quote}
\begin{verbatim}>>> from oopp import Object
>>> otherclock=Object()\end{verbatim}
\end{quote}

the other clock will \texttt{not} have a get{\_}time method:
\begin{quote}
\begin{verbatim}>>> otherclock.get_time() #first attempt; error
AttributeError: 'Object' object has no attribute 'get_time'\end{verbatim}
\end{quote}

Notice instead that the docstring is a \emph{class attribute}, i.e. it
is defined both for the class and \emph{all instances} of the class,
therefore even for \texttt{otherclock}:
\begin{quote}
\begin{verbatim}>>> Object.__doc__
'A convenient Object class' 
>>> otherclock.__doc__
'A convenient Object class'\end{verbatim}
\end{quote}

We would like to convert the \texttt{get{\_}time} function to a 
\texttt{get{\_}time} method for the \emph{entire} class 'Object', i.e. for all its
instances. Naively, one would be tempted to write the following:
\begin{quote}
\begin{verbatim}>>> Object.get_time=oopp.get_time\end{verbatim}
\end{quote}

However this would not work:
\begin{quote}
\begin{verbatim}>>> otherclock.get_time() #second attempt; still error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: oopp.get_time() takes no arguments (1 given)\end{verbatim}
\end{quote}

This error message is something that all Python beginners encounter
(and sometimes even non-beginners ;-). The solution is to introduce
an additional argument:
\begin{quote}
\begin{verbatim}>>> Object.get_time=lambda self : oopp.get_time()
>>> otherclock.get_time # this is method now, not a function
<bound method Object.<lambda> of <__main__.Object object at 0x815881c>>
>>> otherclock.get_time() #third attempt
'15:28:41'\end{verbatim}
\end{quote}

Why this works ? The explanation is the following:
when Python encounters an expression of the form
\texttt{objectname.methodname()} it looks if there is a already a method 
\emph{attached} to the object:
\begin{quote}
\newcounter{listcnt16}
\begin{list}{\alph{listcnt16}.}
{
\usecounter{listcnt16}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
if yes it invokes it with no arguments 
(this is why our first example worked);

\item {} 
if not it looks at the class of the object; if there is a method 
bound to the class it invokes that method \emph{by passing the 
object as first argument}.

\end{list}
\end{quote}

When we invoked \texttt{otherclock.get{\_}time()} in our second attempt, Python
found that the function \texttt{get{\_}time} was defined at the class level, 
and sent it the \texttt{otherclock} object as first argument: however \texttt{get{\_}time} 
was bind to \texttt{func{\_}get{\_}time}, which is function with \emph{no} arguments: whence
the error message. The third attempt worked since, thanks to the 
lambda function trick, the \texttt{get{\_}time} function has been converted to
a function accepting a first argument.

Therefore that's the rule: in Python, one can define methods 
at the class level, provided one explitely introduces a first argument
containing the object on which the method is invoked.

This first argument is traditionally called \texttt{self}; the name 'self' is not
enforced, one could use any other valid Python identifier, however the
convention is so widespread that practically everybody uses it;
pychecker will even raise a warning in the case you don't follow the
convention.

I have just shown one the most interesting features of Python, its
\emph{dynamicity}: you can create the class first and add methods to it later.
That logic cannot be followed in typical compiled language as C++. On the
other hand, one can also define methods in a static, more traditional way:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<clock1.py>}\\
\mbox{}\\
\mbox{"Shows~how~to~define~methods~inside~the~class~(statically)"}\\
\mbox{}\\
\mbox{import~oopp}\\
\mbox{}\\
\mbox{class~Clock(object):}\\
\mbox{~~~~'Clock~class;~version~0.1'}\\
\mbox{~~~~def~get{\_}time(self):~{\#}~method~defined~inside~the~class}\\
\mbox{~~~~~~~~return~oopp.get{\_}time()}\\
\mbox{}\\
\mbox{myclock=Clock()~{\#}creates~a~Clock~instance}\\
\mbox{print~myclock.get{\_}time()~{\#}~print~the~current~time}\\
\mbox{}\\
\mbox{{\#}</clock1.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

In this case we have defined the \texttt{get{\_}time} method inside the class as a
normal function with an explicit first argument called self; this is
entirely equivalent to the use of a lambda function.

The syntax \texttt{myclock.get{\_}time()} is actually syntactic sugar for
\texttt{Clock.get{\_}time(myclock)}.

In this second form, it is clear the \texttt{get{\_}time} is really ``attached'' to the
class, not to the instance.


%___________________________________________________________________________

\hypertarget{objects-have-static-methods-and-classmethods}{}
\pdfbookmark[1]{Objects have static methods and classmethods}{objects-have-static-methods-and-classmethods}
\subsection*{Objects have static methods and classmethods}
\begin{quote}
\begin{flushleft}
\emph{There~should~be~one--and~preferably~only~one--obvious~way~to~do~it}~\\
--~Tim~Peters,~\emph{The~Zen~of~Python}.
\end{flushleft}
\end{quote}

For any rule, there is an exception, and despite the Python's motto    
there are many ways to define methods in classes. The way I presented
before was the obvious one before the Python 2.2 revolution; however, 
nowadays there is another possibility that, even if less obvious, has the 
advantage of some elegance (and it is also slightly more efficient too, even if
efficiency if never a primary concern for a Python programmer).
We see that the first argument in the \texttt{get{\_}time} method is useless,
since the time is computed from the \texttt{time.asctime()} function which
does not require any information about the object that is calling
it. This waste is ugly, and since according to the Zen of Python
\begin{quote}

\emph{Beautiful is better than ugly.}
\end{quote}

we should look for another way. The solution is to use a \emph{static method}:
when a static method is invoked, the calling object is \emph{not} implicitly passed
as first argument. Therefore we may use a normal function with no additional
first argument to define the \texttt{get{\_}time} method:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Clock(object):}\\
\mbox{~~~~'Clock~with~a~staticmethod'}\\
\mbox{~~~~get{\_}time=staticmethod(get{\_}time)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here is how it works:
\begin{quote}
\begin{verbatim}>>> from oopp import Clock
>>> Clock().get_time() # get_time is bound both to instances
'10:34:23'
>>> Clock.get_time() # and to the class
'10:34:26'\end{verbatim}
\end{quote}

The staticmethod idiom converts the lambda function to a
static method of the class 'Clock'. Notice that one can avoid the
lambda expression and use the (arguably more Pythonic) idiom
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{def~get{\_}time()}\\
\mbox{~~~~return~oopp.get{\_}time()}\\
\mbox{get{\_}time=staticmethod(oopp.get{\_}time)}
\end{flushleft}\end{ttfamily}
\end{quote}

as the documentation suggests:
\begin{quote}
\begin{verbatim}>>> print  staticmethod.__doc__
staticmethod(function) -> method
Convert a function to be a static method.
A static method does not receive an implicit first argument.
To declare a static method, use this idiom:
   class C:
       def f(arg1, arg2, ...): ...
       f = staticmethod(f)
It can be called either on the class (e.g. C.f()) or on an instance
(e.g. C().f()).  The instance is ignored except for its class.
Static methods in Python are similar to those found in Java or C++.
For a more advanced concept, see the classmethod builtin.\end{verbatim}
\end{quote}

At the present the notation for static methods is still rather ugly,
but it is expected to improve in future versions of Python (probably
in Python 2.4). Documentation for static methods can
be found in Guido's essay and in the PEP.. : however this is intended for
developers.

As the docstring says, static methods are also ``attached'' to the
class and may be called with the syntax \texttt{Clock.get{\_}time()}.

A similar remark applies for the so called \emph{classmethods}:
\begin{quote}
\begin{verbatim}>>> print classmethod.__doc__
classmethod(function) -> method
Convert a function to be a class method.
A class method receives the class as implicit first argument,
just like an instance method receives the instance.
To declare a class method, use this idiom:
class C:
    def f(cls, arg1, arg2, ...): ...
    f = classmethod(f)
It can be called either on the class (e.g. C.f()) or on an instance
(e.g. C().f()).  The instance is ignored except for its class.
If a class method is called for a derived class, the derived class
object is passed as the implied first argument.
Class methods are different than C++ or Java static methods.
If you want those, see the staticmethod builtin.\end{verbatim}
\end{quote}

{\#}When a regular method is invoked, a reference to the calling object is 
{\#}implicitely passed as first argument; instead, when a static method is 
{\#}invoked, no reference to the calling object is passed.

As the docstring says, classmethods are convenient when one wants to pass 
to a method the calling \emph{class}, not the calling object. Here there is an 
example:
\begin{quote}
\begin{verbatim}>>> class Clock(object): pass
>>> Clock.name=classmethod(lambda cls: cls.__name__)
>>> Clock.name() # called by the class
'Clock'
>>> Clock().name() # called by an instance
'Clock'\end{verbatim}
\end{quote}

Notice that classmethods (and staticmethods too) 
can only be attached to classes, not to objects:
\begin{quote}
\begin{verbatim}>>> class Clock(object): pass
>>> c=Clock()
>>> c.name=classmethod(lambda cls: cls.__name__) 
>>> c.name() #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'classmethod' object is not callable\end{verbatim}
\end{quote}

gives a TypeError. The reason is that classmethods and staticmethods 
are implemented 
trough \emph{attribute descriptors}. This concept will be discussed in detail in a 
forthcoming in chapter 6.

Notice that classmethods are not proving any fundamental feature, since
one could very well use a normal method and retrieve the class with 
\texttt{self.{\_}{\_}class{\_}{\_}} as we did in the first chapter. 
Therefore, we could live without (actually, I think they are a non-essential
complication to the language).
Nevertheless, now that we have them, we can use them, since
they come handy in various circumstances, as we will see in the following.


%___________________________________________________________________________

\hypertarget{objects-have-their-privacy}{}
\pdfbookmark[1]{Objects have their privacy}{objects-have-their-privacy}
\subsection*{Objects have their privacy}

In some situations, it is convenient to give to the developer
some information that should be hided to the final user. To this
aim Python uses private names (i.e. names starting with a single
underscore) and private/protected attributes (i.e. attributes starting with
a double underscore).

Consider for instance the following script:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<privacy.py>}\\
\mbox{}\\
\mbox{import~time}\\
\mbox{}\\
\mbox{class~Clock(object):}\\
\mbox{~~~~{\_}{\_}secret="This~Clock~is~quite~stupid."}\\
\mbox{}\\
\mbox{myclock=Clock()}\\
\mbox{try:~print~myclock.{\_}{\_}secret}\\
\mbox{except~Exception,e:~print~"AttributeError:",e}\\
\mbox{}\\
\mbox{{\#}</privacy.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output of this script is
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{AttributeError:~'Clock'~object~has~no~attribute~'{\_}{\_}secret'}
\end{flushleft}\end{ttfamily}
\end{quote}

Therefore, even if the Clock object \emph{does} have a \texttt{{\_}{\_}secret} attribute, 
the user cannot access it ! In this way she cannot discover that
actually ``This Clock is quite stupid.''

In other programming languages, attributes like \texttt{{\_}{\_}secret} are
called ``private'' attributes. However, in Python private attributes
are not really private and their secrets can be accessed with very
little effort.

First of all, we may notice that \texttt{myclock} really contains a secret
by using the builtin function \texttt{dir()}:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{dir(myclock)}\\
\mbox{['{\_}Clock{\_}{\_}secret',~'{\_}{\_}class{\_}{\_}',~'{\_}{\_}delattr{\_}{\_}',~'{\_}{\_}dict{\_}{\_}',~'{\_}{\_}doc{\_}{\_}',~}\\
\mbox{~'{\_}{\_}getattribute{\_}{\_}',~'{\_}{\_}hash{\_}{\_}',~'{\_}{\_}init{\_}{\_}',~'{\_}{\_}module{\_}{\_}',~'{\_}{\_}new{\_}{\_}',~}\\
\mbox{~'{\_}{\_}reduce{\_}{\_}',~'{\_}{\_}repr{\_}{\_}',~'{\_}{\_}setattr{\_}{\_}',~'{\_}{\_}str{\_}{\_}',~'{\_}{\_}weakref{\_}{\_}']}
\end{flushleft}\end{ttfamily}
\end{quote}

We see that the first attribute of myclock is '{\_}Clock{\_}{\_}secret``, 
which we may access directly:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{print~myclock.{\_}Clock{\_}{\_}secret}\\
\mbox{This~clock~is~quite~stupid.}
\end{flushleft}\end{ttfamily}
\end{quote}

We see here the secret of private variables in Python: the \emph{name mangling}.
When Python sees a name starting with two underscores (and not ending
with two underscores, otherwise it would be interpreted as a special
attribute), internally it manage it as \texttt{{\_}Classname{\_}{\_}privatename}.
Notice that if 'Classname' begins with underscores, the leading underscores
are stripped in such a way to guarantee that the private name starts with
only \emph{one} underscore. For instance, the '{\_}{\_}secret' private attribute 
of classes such as 'Clock', '{\_}Clock', '{\_}{\_}Clock', '{\_}{\_}{\_}Clock', etc. is
mangled to '{\_}Clock{\_}{\_}secret'.

Private names in Python are \emph{not} intended to keep secrets: they
have other uses.
\newcounter{listcnt17}
\begin{list}{\arabic{listcnt17}.}
{
\usecounter{listcnt17}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
On one hand, private names are a suggestion to the developer. 
When the Python programmer sees a name starting with one or two 
underscores in a program written by others, she understands
that name should not be of concern for the final user, but it 
only concerns the internal implementation.

\item {} 
On the other hand, private names are quite useful in class
inheritance, since they provides safety with respect to the overriding
operation. This point we will discussed in the next chapter.

\item {} 
Names starting with one (or more) underscores are not imported by the 
statement \texttt{from module import *}

\end{list}

Remark: it makes no sense to define names with double underscores
outside classes, since the name mangling doesn't work in this case.
Let me show an example:
\begin{quote}
\begin{verbatim}>>> class Clock(object): __secret="This Clock is quite stupid"
>>> def tellsecret(self): return  self.__secret
>>> Clock.tellsecret=tellsecret
>>> Clock().tellsecret() #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in tellsecret
AttributeError: 'Clock' object has no attribute '__secret'\end{verbatim}
\end{quote}

The explanation is that since \texttt{tellsecret()} is defined outside the class,
\texttt{{\_}{\_}secret} is not expanded to \texttt{{\_}Clock{\_}{\_}secret} and therefore cannot be
retrieved, whereas
\begin{quote}
\begin{verbatim}>>> class Clock(object): 
...     __secret="This Clock is quite stupid"
...     def tellsecret(self): return self.__secret
>>> Clock().tellsecret()
This Clock is quite stupid\end{verbatim}
\end{quote}

will work. In other words, private variables are attached to classes,
not objects.


%___________________________________________________________________________

\hypertarget{objects-have-properties}{}
\pdfbookmark[1]{Objects have properties}{objects-have-properties}
\subsection*{Objects have properties}

In the previous section we have shown that private variables are of
little use for keeping secrets: if a developer really wants to restrict 
the access to some methods or attributes, she has to resort to
\emph{properties}.

Let me show an example:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<secret.py>}\\
\mbox{}\\
\mbox{import~oopp}\\
\mbox{}\\
\mbox{class~Clock(object):~}\\
\mbox{~~~~'Clock~class~with~a~secret'}\\
\mbox{~~}\\
\mbox{~~~~you{\_}know{\_}the{\_}pw=False~{\#}default}\\
\mbox{~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~def~give{\_}pw(self,pw):}\\
\mbox{~~~~~~~~"""Check~if~your~know~the~password.~For~security,~one~should~crypt}\\
\mbox{~~~~~~~~the~password."""}\\
\mbox{~~~~~~~~self.you{\_}know{\_}the{\_}pw=(pw=="xyz")}\\
\mbox{~~~~~~}\\
\mbox{~~~~def~get{\_}secret(self):}\\
\mbox{~~~~~~~~if~self.you{\_}know{\_}the{\_}pw:}\\
\mbox{~~~~~~~~~~~~return~"This~clock~doesn't~work."}\\
\mbox{~~~~~~~~else:}\\
\mbox{~~~~~~~~~~~~return~"You~must~give~the~right~password~to~access~'secret'"}\\
\mbox{~~~~~~}\\
\mbox{~~~~secret=property(get{\_}secret)}\\
\mbox{}\\
\mbox{c=Clock()}\\
\mbox{print~c.secret~{\#}~=>~You~must~give~the~right~password~to~access~'secret'}\\
\mbox{c.give{\_}pw('xyz')~{\#}~gives~the~right~password}\\
\mbox{print~c.secret~{\#}~=>~This~clock~doesn't~work.}\\
\mbox{print~Clock.secret~{\#}~=>~<property~object~at~0x814c1b4>}\\
\mbox{}\\
\mbox{{\#}</secret.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

In this script, one wants to restrict the access to the attribute
'secret', which can be accessed only is the user provide the
correct password. Obviously, this example is not very secure,
since I have hard coded the password 'xyz' in the source code,
which is easily accessible. In reality, one should crypt the
password a perform a more sophisticated test than the trivial
check \texttt{(pw=="xyz")}; anyway, the example is only intended to
shown the uses of properties, not to be really secure.

The key action is performed by the descriptor class \texttt{property} that
converts the function \texttt{get{\_}secret} in a property object. Additional
informations on the usage of \texttt{property} can be obtained from the
docstring:
\begin{quote}
\begin{verbatim}>>> print property.__doc__
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
fget is a function to be used for getting an attribute value, and likewise
fset is a function for setting, and fdel a function for del'ing, an
attribute.  Typical use is to define a managed attribute x:
class C(object):
    def getx(self): return self.__x
    def setx(self, value): self.__x = value
    def delx(self): del self.__x
    x = property(getx, setx, delx, "I'm the 'x' property.")\end{verbatim}
\end{quote}

Properties are another example of attribute descriptors.


%___________________________________________________________________________

\hypertarget{objects-have-special-methods}{}
\pdfbookmark[1]{Objects have special methods}{objects-have-special-methods}
\subsection*{Objects have special methods}

From the beginning, we stressed that objects have special attributes that
may turn handy, as for instance the docstring \texttt{{\_}{\_}doc{\_}{\_}} and the class
name attribute \texttt{{\_}{\_}class{\_}{\_}}. They have special methods, too.

With little doubt, the most useful special method is the \texttt{{\_}{\_}init{\_}{\_}}
method, that \emph{initializes} an object right after its creation. \texttt{{\_}{\_}init{\_}{\_}}
is typically used to pass parameters to \emph{object factories}. Let me an
example with geometric figures:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{~~}\\
\mbox{class~GeometricFigure(object):~{\#}an~example~of~object~factory}\\
\mbox{~~~~"""This~class~allows~to~define~geometric~figures~according~to~their}\\
\mbox{~~~~equation~in~the~cartesian~plane.~It~will~be~extended~later."""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,equation,**parameters):}\\
\mbox{~~~~~~~~"Specify~the~cartesian~equation~of~the~object~and~its~parameters"}\\
\mbox{~~~~~~~~self.eq=equation}\\
\mbox{~~~~~~~~self.par=parameters}\\
\mbox{~~~~~~~~for~k,v~in~self.par.items():~{\#}replaces~the~parameters~in~the~equation}\\
\mbox{~~~~~~~~~~~~self.eq=self.eq.replace(k,str(v))}\\
\mbox{~~~~~~~~self.contains=eval('lambda~x,y~:~'+self.eq)~}\\
\mbox{~~~~~~~~{\#}~dynamically~creates~the~function~'contains'}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here it is how it works:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> disk=GeometricFigure('(x-x0)**2+(y-y0)**2 <= r**2', x0=0,y0=0,r=5)
>>> # creates a disk of radius 5 centered in the origing
>>> disk.contains(1,2) #asks if the point (1,2) is inside the disk
True
>>> disk.contains(4,4) #asks if the point (4,4) is inside the disk
False\end{verbatim}
\end{quote}

Let me continue the section on special methods with some some observations on
\texttt{{\_}{\_}repr{\_}{\_}} and \texttt{{\_}{\_}str{\_}{\_}}.Notice that  I
will not discuss all the subtleties; for a thought discussion, see the
thread ``Using {\_}{\_}repr{\_}{\_} or {\_}{\_}str{\_}{\_}'' in c.l.p. (Google is your friend).
The following discussion applies to new style classes, old style classes 
are subtly different; moreover.

When one  writes
\begin{quote}
\begin{verbatim}>>> disk
<oopp.GeometricFigure instance at 0x81b496c>\end{verbatim}
\end{quote}

one obtains the \emph{string representation} of the object. Actually, the previous
line is syntactic sugar for
\begin{quote}
\begin{verbatim}>>> print repr(disk)
<oopp.GeometricFigure instance at 0x81b496c>\end{verbatim}
\end{quote}

or
\begin{quote}
\begin{verbatim}>>> print disk.__repr__()
<oopp.GeometricFigure instance at 0x81b496c>\end{verbatim}
\end{quote}

The \texttt{repr} function extracts the string representation from the
the special method \texttt{{\_}{\_}repr{\_}{\_}}, which can be redefined in order to 
have objects pretty printed. Notice that \texttt{repr} is conceptually
different from the \texttt{str} function that controls the output of the \texttt{print} 
statement. Actually, \texttt{print o} is syntactic sugar for \texttt{print str(o)}
which is sugar for \texttt{print o.{\_}{\_}str{\_}{\_}()}.

If for instance we define
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~PrettyPrinted(object):}\\
\mbox{~~~~formatstring='{\%}s'~{\#}~default}\\
\mbox{~~~~def~{\_}{\_}str{\_}{\_}(self):}\\
\mbox{~~~~~~~~"""Returns~the~name~of~self~in~quotes,~possibly~formatted~via~}\\
\mbox{~~~~~~~~self.formatstring.~If~self~has~no~name,~returns~the~name~}\\
\mbox{~~~~~~~~of~its~class~in~angular~brackets."""~}\\
\mbox{~~~~~~~~try:~{\#}look~if~the~selfect~has~a~name~}\\
\mbox{~~~~~~~~~~~name="'{\%}s'"~{\%}~self.{\_}{\_}name{\_}{\_}~}\\
\mbox{~~~~~~~~except~AttributeError:~{\#}if~not,~use~the~name~of~its~class}\\
\mbox{~~~~~~~~~~~~name='<{\%}s>'~{\%}~type(self).{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~~~~~if~hasattr(self,'formatstring'):}\\
\mbox{~~~~~~~~~~~~return~self.formatstring~{\%}~name}\\
\mbox{~~~~~~~~else:~}\\
\mbox{~~~~~~~~~~~~return~name}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

then we have
\begin{quote}
\begin{verbatim}>>> from oopp import PrettyPrinted
>>> o=PrettyPrinted() # o is an instance of PrettyPrinted
>>> print o #invokes o.__str__() which in this case returns o.__class__.name
<PrettyPrinted>\end{verbatim}
\end{quote}

whereas
\begin{quote}
\begin{verbatim}>>> o # i.e. print repr(o)
<oopp.PrettyPrinted object at 0x400a006c>\end{verbatim}
\end{quote}

However, in most cases \texttt{{\_}{\_}repr{\_}{\_}} and \texttt{{\_}{\_}str{\_}{\_}} gives the same
output, since if \texttt{{\_}{\_}str{\_}{\_}} is not explicitely defined it defaults
to \texttt{{\_}{\_}repr{\_}{\_}}. Therefore, whereas modifying \texttt{{\_}{\_}str{\_}{\_}} 
does not change \texttt{{\_}{\_}repr{\_}{\_}}, modifying \texttt{{\_}{\_}repr{\_}{\_}} changes \texttt{{\_}{\_}str{\_}{\_}},
if \texttt{{\_}{\_}str{\_}{\_}} is not explicitely given:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<fairytale1.py>}\\
\mbox{}\\
\mbox{"{\_}{\_}repr{\_}{\_}~can~also~be~a~regular~method,~not~a~classmethod"}\\
\mbox{}\\
\mbox{class~Frog(object):}\\
\mbox{~~~~attributes="poor,~small,~ugly"}\\
\mbox{~~~~def~{\_}{\_}str{\_}{\_}(self):}\\
\mbox{~~~~~~~~return~"I~am~a~"+self.attributes+'~'+self.{\_}{\_}class{\_}{\_}.{\_}{\_}name{\_}{\_}}\\
\mbox{}\\
\mbox{class~Prince(object):}\\
\mbox{~~~~attributes='rich,~tall,~beautiful'}\\
\mbox{~~~~def~{\_}{\_}str{\_}{\_}(self):}\\
\mbox{~~~~~~~~return~"I~am~a~"+self.attributes+'~'+self.{\_}{\_}class{\_}{\_}.{\_}{\_}name{\_}{\_}}\\
\mbox{}\\
\mbox{jack=Frog();~print~repr(jack),jack}\\
\mbox{charles=Prince();~print~repr(charles),charles~~}\\
\mbox{}\\
\mbox{{\#}</fairytale1.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output of this script is:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{<Frog~object~at~0x81866ec>~I~am~a~poor,~small,~ugly~Frog}\\
\mbox{<Prince~object~at~0x818670c>~I~am~a~rich,~tall,~beautiful~Prince}
\end{flushleft}\end{ttfamily}
\end{quote}

for jack and charles respectively.

\texttt{{\_}{\_}str{\_}{\_}}  and \texttt{{\_}{\_}repr{\_}{\_}} are also called by the formatting
operators ``{\%}s'' and ``{\%}r''.

Notice that  i) \texttt{{\_}{\_}str{\_}{\_}} can be most naturally
rewritten as a class method; ii) Python is magic:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<fairytale2.py>}\\
\mbox{~~}\\
\mbox{"""Shows~two~things:~}\\
\mbox{~~~~1)~redefining~{\_}{\_}repr{\_}{\_}~automatically~changes~the~output~of~{\_}{\_}str{\_}{\_}}\\
\mbox{~~~~2)~the~class~of~an~object~can~be~dinamically~changed!~"""}\\
\mbox{}\\
\mbox{class~Frog(object):}\\
\mbox{~~~~attributes="poor,~small,~ugly"}\\
\mbox{~~~~def~{\_}{\_}repr{\_}{\_}(cls):}\\
\mbox{~~~~~~~~return~"I~am~a~"+cls.attributes+'~'+cls.{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~{\_}{\_}repr{\_}{\_}=classmethod({\_}{\_}repr{\_}{\_})}\\
\mbox{}\\
\mbox{class~Prince(object):}\\
\mbox{~~~~attributes='rich,~tall,~beautiful'}\\
\mbox{~~~~def~{\_}{\_}repr{\_}{\_}(cls):}\\
\mbox{~~~~~~~~return~"I~am~a~"+cls.attributes+'~'+cls.{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~{\_}{\_}repr{\_}{\_}=classmethod({\_}{\_}repr{\_}{\_})}\\
\mbox{}\\
\mbox{def~princess{\_}kiss(frog):}\\
\mbox{~~~~~~frog.{\_}{\_}class{\_}{\_}=Prince}\\
\mbox{}\\
\mbox{jack=Frog()}\\
\mbox{princess{\_}kiss(jack)}\\
\mbox{print~jack~{\#}~the~same~as~repr(jack)}\\
\mbox{}\\
\mbox{{\#}</fairytale2.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now the output for jack is ``I am a rich, tall, beautiful Prince'' !
In Python you may dynamically change the class of an object!!

Of course, this is a feature to use with care ;-)

There are many others special methods, such as {\_}{\_}new{\_}{\_}, {\_}{\_}getattr{\_}{\_},
{\_}{\_}setattr{\_}{\_}, etc. They will be discussed in the next chapters, in
conjunction with inheritance.


%___________________________________________________________________________

\hypertarget{objects-can-be-called-added-subtracted}{}
\pdfbookmark[1]{Objects can be called, added, subtracted, ...}{objects-can-be-called-added-subtracted}
\subsection*{Objects can be called, added, subtracted, ...}

Python provides a nice generalization of functions, via the concept
of \emph{callable objects}. A callable object is an object with a \texttt{{\_}{\_}call{\_}{\_}}
special method. They can be used to define ``functions'' that remember
how many times they are invoked:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<call.py>}\\
\mbox{}\\
\mbox{class~MultiplyBy(object):}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,n):}\\
\mbox{~~~~~~~~self.n=n}\\
\mbox{~~~~~~~~self.counter=0}\\
\mbox{~~~~def~{\_}{\_}call{\_}{\_}(self,x):}\\
\mbox{~~~~~~~~self.counter+=1}\\
\mbox{~~~~~~~~return~self.n*x}\\
\mbox{}\\
\mbox{double=MultiplyBy(2)}\\
\mbox{res=double(double(3))~{\#}~res=12}\\
\mbox{print~"double~is~callable:~{\%}s"~{\%}~callable(double)}\\
\mbox{print~"You~have~called~double~{\%}s~times."~{\%}~double.counter}\\
\mbox{}\\
\mbox{{\#}</call.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

With output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{double~is~callable:~~True}\\
\mbox{You~have~called~double~2~times.}
\end{flushleft}\end{ttfamily}
\end{quote}

The script also show that callable objects (including functions) 
can be recognized with the \texttt{callable} built-in function.

Callable object solves elegantly the problem of having ``static'' variables
inside functions (cfr. with the 'double' example in chapter 2).
A class with a \texttt{{\_}{\_}call{\_}{\_}} method can be used to generate an entire
set of customized ``functions''. For this reason, callable objects are 
especially useful in the conjunction with object factories. Let me show 
an application to my factory of geometric figures:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Makeobj(object):}\\
\mbox{~~~~"""A~factory~of~object~factories.~Makeobj(cls)~returns~instances}\\
\mbox{~~~~~of~cls"""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,cls,*args):}\\
\mbox{~~~~~~~~self.cls=cls}\\
\mbox{~~~~~~~~self.args=args}\\
\mbox{~~~~def~{\_}{\_}call{\_}{\_}(self,**pars):}\\
\mbox{~~~~~~~~return~self.cls(*self.args,**pars)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{{\#}<factory.py>}\\
\mbox{}\\
\mbox{from~oopp~import~Makeobj,GeometricFigure}\\
\mbox{}\\
\mbox{makedisk=Makeobj(GeometricFigure,'(x-x0)**2+(y-y0)**2<r**2')}\\
\mbox{makesquare=Makeobj(GeometricFigure,'abs(x-x0)<L~and~abs(y-y0)<L')}\\
\mbox{disk=makedisk(x0=0,y0=0,r=10)~{\#}~make~a~disk~of~radius~10}\\
\mbox{square=makesquare(x0=0,y0=0,L=20)~{\#}~make~a~disk~of~side~10}\\
\mbox{}\\
\mbox{print~disk.contains(9,9)~{\#}~=>~False}\\
\mbox{print~square.contains(9,9)~{\#}~=>~True}\\
\mbox{{\#}etc.}\\
\mbox{}\\
\mbox{{\#}</factory.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

This factory generates callable objects, such as \texttt{makedisk} and
\texttt{makesquare} that returns geometric objects. It gives a nicer interface
to the object factory provided by 'GeometricFigure'.

Notice that the use of the expression \texttt{disk.contains(9,9)} in order to
know if the point of coordinates (9,9) is contained in the disk, it is
rather inelegant: it would be much better to be able to ask if 
\texttt{(9,9) in disk}. This is possibile, indeed: and the secrets is to
define the special method \texttt{{\_}{\_}contains{\_}{\_}}. This is done in the next
example, that I think give a good taste of the beauty of objects
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<funnyformatter.py>}\\
\mbox{}\\
\mbox{from~oopp~import~Makeobj}\\
\mbox{}\\
\mbox{Nrow=50;~Ncol=78}\\
\mbox{~~}\\
\mbox{class~GeometricFigure(object):}\\
\mbox{~~~~"""This~class~allows~to~define~geometric~figures~according~to~their}\\
\mbox{~~~~equation~in~the~cartesian~plane.~Moreover~addition~and~subtraction}\\
\mbox{~~~~of~geometric~figures~are~defined~as~union~and~subtraction~of~sets."""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,equation,**parameters):}\\
\mbox{~~~~~~~~"Initialize~"}\\
\mbox{~~~~~~~~self.eq=equation}\\
\mbox{~~~~~~~~self.par=parameters}\\
\mbox{~~~~~~~~for~(k,v)~in~self.par.items():~{\#}replaces~the~parameters}\\
\mbox{~~~~~~~~~~~~self.eq=self.eq.replace(k,str(v))}\\
\mbox{~~~~~~~~self.contains=eval('lambda~x,y~:~'+self.eq)}\\
\mbox{~~~~def~combine(self,fig,operator):}\\
\mbox{~~~~~~~~"""Combine~self~with~the~geometric~figure~fig,~using~the}\\
\mbox{~~~~~~~~operators~"or"~(addition)~and~"and~not"~(subtraction)"""}\\
\mbox{~~~~~~~~comboeq="("+self.eq+")"+operator+"("+fig.eq+")"}\\
\mbox{~~~~~~~~return~GeometricFigure(comboeq)}\\
\mbox{~~~~def~{\_}{\_}add{\_}{\_}(self,fig):}\\
\mbox{~~~~~~~~"Union~of~sets"}\\
\mbox{~~~~~~~~return~self.combine(fig,'~or~')}\\
\mbox{~~~~def~{\_}{\_}sub{\_}{\_}(self,fig):}\\
\mbox{~~~~~~~~"Subtraction~of~sets"}\\
\mbox{~~~~~~~~return~self.combine(fig,'~and~not')}\\
\mbox{~~~~def~{\_}{\_}contains{\_}{\_}(self,point):~{\#}point~is~a~tuple~(x,y)}\\
\mbox{~~~~~~~~return~self.contains(*point)}\\
\mbox{}\\
\mbox{makedisk=Makeobj(GeometricFigure,'(x-x0)**2/4+(y-y0)**2~<=~r**2')}\\
\mbox{upperdisk=makedisk(x0=38,y0=7,r=5)}\\
\mbox{smalldisk=makedisk(x0=38,y0=30,r=5)}\\
\mbox{bigdisk=makedisk(x0=38,y0=30,r=14)}\\
\mbox{}\\
\mbox{def~format(text,shape):}\\
\mbox{~~~~"Format~the~text~in~the~shape~given~by~figure"}\\
\mbox{~~~~text=text.replace('{\textbackslash}n','~')}\\
\mbox{~~~~out=[];~i=0;~col=0;~row=0;~L=len(text)}\\
\mbox{~~~~while~1:}\\
\mbox{~~~~~~~~if~(col,row)~in~shape:}\\
\mbox{~~~~~~~~~~~~out.append(text[i]);~i+=1}\\
\mbox{~~~~~~~~~~~~if~i==L:~break}\\
\mbox{~~~~~~~~else:}\\
\mbox{~~~~~~~~~~~~out.append("~")}\\
\mbox{~~~~~~~~if~col==Ncol-1:}\\
\mbox{~~~~~~~~~~~~col=0;~out.append('{\textbackslash}n')~{\#}~starts~new~row}\\
\mbox{~~~~~~~~~~~~if~row==Nrow-1:~row=0~~~{\#}~starts~new~page}\\
\mbox{~~~~~~~~~~~~else:~row+=1}\\
\mbox{~~~~~~~~else:~col+=1~}\\
\mbox{~~~~return~''.join(out)}\\
\mbox{}\\
\mbox{composition=bigdisk-smalldisk+upperdisk}\\
\mbox{print~format(text='Python~Rules!'*95,shape=composition)}\\
\mbox{}\\
\mbox{{\#}</funnyformatter.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

I leave as an exercise for the reader to understand how does it work and to
play with other geometric figures (he can also generate them trough the
'Makeobj' factory). I think it is nicer to show its output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~Pyt~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~hon~Rules!Pyt~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~hon~Rules!Python~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~Rules!Python~Rules!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~Python~Rules!Python~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~Rules!Python~Rules!P~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~ython~Rules!Python~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~Rules!Python~Rules!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~Python~Rules!Pyth~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~on~Rules!Pyth~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~on~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~Rul~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~es!Python~Rules!Pytho~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~n~Rules!Python~Rules!Python~R~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~ules!Python~Rules!Python~Rules!Pyth~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~on~Rules!Python~Rules!Python~Rules!Pyth~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~on~Rules!Python~Rules!Python~Rules!Python~R~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~ules!Python~Rules!Python~Rules!Python~Rules!Pyt~~~~~~~~~~~~~~~~}\\
\mbox{~~~~hon~Rules!Python~Rules!Python~Rules!Python~Rules!~~~~~~~~~~~~~~~}\\
\mbox{~~~Python~Rules!Python~Rules!Python~Rules!Python~Rules~~~~~~~~~~~~~~}\\
\mbox{~~!Python~Rules!Python~Rule~~~s!Python~Rules!Python~Rul~~~~~~~~~~~~~}\\
\mbox{~~es!Python~Rules!Pyth~~~~~~~~~~~~~on~Rules!Python~Rule~~~~~~~~~~~~~}\\
\mbox{~s!Python~Rules!Pyth~~~~~~~~~~~~~~~~~on~Rules!Python~Rul~~~~~~~~~~~~}\\
\mbox{~es!Python~Rules!Py~~~~~~~~~~~~~~~~~~~thon~Rules!Python~~~~~~~~~~~~~}\\
\mbox{~Rules!Python~Rules~~~~~~~~~~~~~~~~~~~!Python~Rules!Pyth~~~~~~~~~~~~}\\
\mbox{on~Rules!Python~Ru~~~~~~~~~~~~~~~~~~~~~les!Python~Rules!P~~~~~~~~~~~}\\
\mbox{~ython~Rules!Python~~~~~~~~~~~~~~~~~~~~Rules!Python~Rule~~~~~~~~~~~~}\\
\mbox{~s!Python~Rules!Pyt~~~~~~~~~~~~~~~~~~~hon~Rules!Python~R~~~~~~~~~~~~}\\
\mbox{~ules!Python~Rules!P~~~~~~~~~~~~~~~~~ython~Rules!Python~~~~~~~~~~~~~}\\
\mbox{~~Rules!Python~Rules!P~~~~~~~~~~~~~ython~Rules!Python~R~~~~~~~~~~~~~}\\
\mbox{~~ules!Python~Rules!Python~~~~Rules!Python~Rules!Python~~~~~~~~~~~~~}\\
\mbox{~~~~Rules!Python~Rules!Python~Rules!Python~Rules!Pytho~~~~~~~~~~~~~~}\\
\mbox{~~~~n~Rules!Python~Rules!Python~Rules!Python~Rules!Py~~~~~~~~~~~~~~~}\\
\mbox{~~~~~thon~Rules!Python~Rules!Python~Rules!Python~Rul~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~es!Python~Rules!Python~Rules!Python~Rules!P~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~ython~Rules!Python~Rules!Python~Rules!P~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~ython~Rules!Python~Rules!Python~Rul~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~es!Python~Rules!Python~Rules!~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~Python~Rules!Python~R~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~ule~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~s!}
\end{flushleft}\end{ttfamily}
\end{quote}

Remark.

Unfortunately, ``funnyformatter.py'' does not reuse old code: in spite of the
fact that we already had in our library the 'GeometricFigure' class, with 
an ``{\_}{\_}init{\_}{\_}'' method that is exactly the same of the ``{\_}{\_}init{\_}{\_}'' method in 
``funnyformatter.py'', we did not reuse that code. We simply did a cut
and paste. This means that if we later find a bug in the \texttt{{\_}{\_}init{\_}{\_}} method,
we will have to fix it twice, both in the script and in the library. Also,
if we plan to extend the method later, we will have to extend it twice.
Fortunately, this nasty situation can be avoided: but this requires the
power of inheritance.


%___________________________________________________________________________

\hypertarget{the-power-of-classes}{}
\pdfbookmark[0]{THE POWER OF CLASSES}{the-power-of-classes}
\section*{THE POWER OF CLASSES}

This chapter is devoted to the concept of class inheritance. I will discuss
single inheritance, cooperative methods, multiple inheritance and more.


%___________________________________________________________________________

\hypertarget{the-concept-of-inheritance}{}
\pdfbookmark[1]{The concept of inheritance}{the-concept-of-inheritance}
\subsection*{The concept of inheritance}

Inheritance is perhaps the most important basic feature in OOP, since it
allows the reuse and incremental improvement of old code.
To show this point, let me come back to one of the 
examples I have introduced in the last chapter, 'fairytale1.py' script, 
where I defined the classes 'Frog' and 'Prince' as
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{class~Frog(object):}\\
\mbox{~~~~attributes="poor,~small,~ugly"}\\
\mbox{~~~~def~{\_}{\_}str{\_}{\_}(self):}\\
\mbox{~~~~~~~~return~"I~am~a~"+self.attributes+'~'+self.{\_}{\_}class{\_}{\_}.{\_}{\_}name{\_}{\_}}\\
\mbox{}\\
\mbox{class~Prince(object):}\\
\mbox{~~~~attributes='rich,~tall,~beautiful'}\\
\mbox{~~~~def~{\_}{\_}str{\_}{\_}(self):}\\
\mbox{~~~~~~~~return~"I~am~a~"+self.attributes+'~'+self.{\_}{\_}class{\_}{\_}.{\_}{\_}name{\_}{\_}}
\end{flushleft}\end{ttfamily}
\end{quote}

We see that the way we followed here was very bad since:
\newcounter{listcnt18}
\begin{list}{\arabic{listcnt18}.}
{
\usecounter{listcnt18}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
The \texttt{{\_}{\_}str{\_}{\_}} method is duplicated both in Frog and in Prince: that
means that if we find a bug a later, we have to fix it twice!

\item {} 
The \texttt{{\_}{\_}str{\_}{\_}} was already defined in the PrettyPrinted class (actually 
more elegantly), therefore we have triplicated the work and worsened the 
situation!

\end{list}

This is very much against the all philosophy of OOP:
\begin{quote}

\emph{never cut and paste!}
\end{quote}

We should \emph{reuse} old code, not paste it!

The solution is \emph{class inheritance}. The idea behind inheritance is to 
define new classes as subclasses of a \emph{parent} classes, in such a way that 
the \emph{children} classes possess all the features of the parents. 
That means that we do not need to 
redefine the properties of the parents explicitely.
In this example, we may derive both 'Frog' and 'Prince' from
the 'PrettyPrinted' class, thus providing to both 'Frog' and  'Prince'
the \texttt{PrettyPrinted.{\_}{\_}str{\_}{\_}} method with no effort:
\begin{quote}
\begin{verbatim}>>> from oopp import PrettyPrinted
>>> class Frog(PrettyPrinted): attributes="poor, small, ugly"
...
>>> class Prince(PrettyPrinted):  attributes="rich, tall, beautiful"
...
>>> print repr(Frog()), Frog()
<__main__.Frog object at 0x401cbeac> <Frog>
>>> print Prince()
>>> print repr(Prince()),Prince()
<__main__.Prince object at 0x401cbaac> <Prince>\end{verbatim}
\end{quote}

Let me show explicitly that both 'Frog' and 'Prince' share the 
'PrettyPrinted.{\_}{\_}str{\_}{\_}' method:
\begin{quote}
\begin{verbatim}>>> id(Frog.__str__) # of course, YMMV
1074329476
>>> id(Prince.__str__)
1074329476
>>> id(PrettyPrinted.__str__)
1074329476\end{verbatim}
\end{quote}

The method is always the same, since the object reference is the same
(the precise value of the reference is not guaranteed to be  1074329476,
however!).

This example is good to show the first advantage of inheritance: 
\emph{avoiding duplication of code}.
Another advantage of inheritance, is \emph{extensibility}: one can very easily
improve existing code. For instance, having written the \texttt{Clock} class once, 
I can reuse it in many different ways. for example I can build a \texttt{Timer} 
to be used for benchmarks. It is enough to reuse the function \texttt{with{\_}timer}
introduced in the first chapter (functions are good for reuse of code, too ;):
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Timer(Clock):}\\
\mbox{~~~~"Inherits~the~get{\_}time~staticmethod~from~Clock"}\\
\mbox{~~~~execute=staticmethod(with{\_}timer)}\\
\mbox{~~~~loop{\_}overhead=staticmethod(loop{\_}overhead)}\\
\mbox{}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of application:
\begin{quote}
\begin{verbatim}>>> from oopp import Timer
>>> Timer.get_time()
'16:07:06'\end{verbatim}
\end{quote}

Therefore 'Timer' inherits 'Clock.get{\_}time'; moreover it has the additional 
method \texttt{execute}:
\begin{quote}
\begin{verbatim}>>> def square(x): return x*x
...
>>> Timer.execute(square,n=100000)(1)
executing square ...
  Real time: 0.01 ms  CPU time: 0.008 ms\end{verbatim}
\end{quote}

The advantage of putting the function \texttt{execute} in a class is that
now we may \emph{inherit} from that class and improve out timer \emph{ad
libitum}.


%___________________________________________________________________________

\hypertarget{inheritance-versus-run-time-class-modifications}{}
\pdfbookmark[1]{Inheritance versus run-time class modifications}{inheritance-versus-run-time-class-modifications}
\subsection*{Inheritance versus run-time class modifications}

Naively, one could think of substituting inheritance with run-time 
modification of classes, since this is allowed by Python. However,
this is not such a good idea, in general. Let me give a simple example.
Suppose we want to improve our previous clock, to show the date, too.
We could reach that goal with the following script:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<clock2.py>}\\
\mbox{}\\
\mbox{"Shows~how~to~modify~and~enhances~classes~on~the~fly"}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{}\\
\mbox{clock=Clock()~{\#}creates~a~Clock~instance}\\
\mbox{print~clock.get{\_}time()~{\#}~print~the~current~time}\\
\mbox{}\\
\mbox{get{\_}data=lambda~:~'~'.join(time.asctime().split()[0:3])+~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~'~'+time.asctime().split()[-1]}\\
\mbox{}\\
\mbox{get{\_}data{\_}and{\_}time=lambda~:~"Today~is:~{\%}s~{\textbackslash}nThe~time~is:~{\%}s"~{\%}~(}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~get{\_}data(),get{\_}time())~{\#}~enhances~get{\_}time}\\
\mbox{}\\
\mbox{Clock.get{\_}time=staticmethod(get{\_}data{\_}and{\_}time)}\\
\mbox{}\\
\mbox{print~clock.get{\_}time()~{\#}~print~the~current~time~and~data}\\
\mbox{}\\
\mbox{{\#}</clock2.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output of this script is:
\begin{quote}

12:51:25
Today is: Sat Feb 22 2003 
The time is: 12:51:25
\end{quote}

Notice that:
\newcounter{listcnt19}
\begin{list}{\arabic{listcnt19}.}
{
\usecounter{listcnt19}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
I instantiated the \texttt{clock} object \emph{before} redefining the \texttt{get{\_}time}
method, when it only could print the time and \emph{not} the date.

\item {} 
However, after the redefinition of the class, the behaviour of all its 
instances is changed, \emph{including the behaviour of objects instantiated 
before the change!}. Then \texttt{clock} \emph{can} print the date, too.

\end{list}

This is not so surprising, once you recognize that Guido own a very famous
time-machine ... ;-)

Seriously, the reason is that an object does not contains a reserved copy
of the attributes and methods of its class: it only contains \emph{references}
to them. If we change them in the class, the references to them in the
object will stay the same, but the contents will change.

In this example, I have solved the problem of enhancing the 'Clock' class
without inheritance, but dynamically replaceing its \texttt{get{\_}time} 
(static) method with the \titlereference{get{\_}data{\_}and{\_}time`} (static) method. 
The dynamics modification of methods can be cool, but it should be avoided 
whenever possible, at least for two reasons [\hyperlink{id22}{11}]:
\newcounter{listcnt20}
\begin{list}{\arabic{listcnt20}.}
{
\usecounter{listcnt20}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
having a class and therefore all its instances (including the instances
created before the modification !) changed during the life-time of the
program can be very confusing to the programmer, if not to the interpreter.

\item {} 
the modification is destructive: I cannot have the old \texttt{get{\_}time} method
and the new one at the same time, unless one explicitly gives to it 
a new name (and giving new names increases the pollution of the namespace).

\end{list}

Both these disadvantages can be solved by resorting to the mechanism of
inheritance. For instance, in this example, we can derive a new class 
\texttt{NewClock} from \texttt{Clock} as follows:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<newclock.py>}\\
\mbox{}\\
\mbox{import~oopp,time}\\
\mbox{}\\
\mbox{get{\_}data=lambda~:~'~'.join(time.asctime().split()[0:3])+~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~'~'+time.asctime().split()[-1]}\\
\mbox{}\\
\mbox{get{\_}data{\_}and{\_}time=lambda~:~"Today~is:~{\%}s~{\textbackslash}nThe~time~is:~{\%}s"~{\%}~(}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~get{\_}data(),oopp.get{\_}time())~{\#}~enhances~get{\_}time}\\
\mbox{}\\
\mbox{class~NewClock(oopp.Clock):}\\
\mbox{~~~~~~~"""NewClock~is~a~class~that~inherits~from~Clock,~provides~get{\_}data}\\
\mbox{~~~~~~~~and~overrides~get{\_}time."""}\\
\mbox{~~~~~~~get{\_}data=staticmethod(get{\_}data)}\\
\mbox{~~~~~~~get{\_}time=staticmethod(get{\_}data{\_}and{\_}time)}\\
\mbox{}\\
\mbox{clock=oopp.Clock();~print~'clock~output=',clock.get{\_}time()~}\\
\mbox{newclock=NewClock();~print~'newclock~output=',newclock.get{\_}time()}\\
\mbox{}\\
\mbox{{\#}</newclock.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output of this script is:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{clock~output=~16:29:17}\\
\mbox{newclock~output=~Today~is:~Sat~Feb~22~2003~}\\
\mbox{The~time~is:~16:29:17}
\end{flushleft}\end{ttfamily}
\end{quote}

We see that the two problems previously discussed are solved since:
\newcounter{listcnt21}
\begin{list}{\roman{listcnt21})}
{
\usecounter{listcnt21}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
there is no cut and paste: the old method \texttt{Clock.get{\_}time()} is used
in the definition of the new method \texttt{NewClock.get{\_}time()};

\item {} 
the old method is still accessible as \texttt{Clock.get{\_}time()}; there is
no need to invent a new name like \texttt{get{\_}time{\_}old()}.

\end{list}

We say that the method \texttt{get{\_}time} in \texttt{NewClock} \emph{overrides} the method
\texttt{get{\_}time} in Clock.

This simple example shows the power of inheritance in code
reuse, but there is more than that.

Inheritance is everywhere in Python, since
all  classes inherit from object. This means that all classes
inherit the methods and attributes of the object class, such as \texttt{{\_}{\_}doc{\_}{\_}},
\texttt{{\_}{\_}class{\_}{\_}}, \texttt{{\_}{\_}str{\_}{\_}}, etc.
\begin{quote}
\begin{figure}[b]\hypertarget{id24}[12]
There are cases when run-time modifications of classes is useful
anyway: particularly when one wants to modify the behavior of
classes written by others without changing the source code. I
will show an example in next chapter.
\end{figure}
\end{quote}


%___________________________________________________________________________

\hypertarget{inheriting-from-built-in-types}{}
\pdfbookmark[1]{Inheriting from built-in types}{inheriting-from-built-in-types}
\subsection*{Inheriting from built-in types}

However, one can subclass a built-in type, effectively creating an 
user-defined type with all the feature of a built-in type, and modify it.

Suppose for instance one has a keyword dictionary such as
\begin{quote}
\begin{verbatim}>>> kd={'title': "OOPP", 'author': "M.S.", 'year': 2003}\end{verbatim}
\end{quote}

it would be nice to be able to access the attributes without
excessive quoting, i.e. using \texttt{kd.author} instead of \texttt{kd["author"]}.
This can be done by subclassing the built-in class \texttt{dict} and
by overriding the \texttt{{\_}{\_}getattr{\_}{\_}} and \texttt{{\_}{\_}setattr{\_}{\_}} special methods:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp/py>}\\
\mbox{}\\
\mbox{class~kwdict(dict):}\\
\mbox{~~~~"Keyword~dictionary~base~class"}\\
\mbox{~~~~def~{\_}{\_}getattr{\_}{\_}(self,attr):~}\\
\mbox{~~~~~~~~return~self[attr]}\\
\mbox{~~~~def~{\_}{\_}setattr{\_}{\_}(self,key,val):~}\\
\mbox{~~~~~~~~self[key]=val}\\
\mbox{~~~~{\_}{\_}str{\_}{\_}~=~pretty~}\\
\mbox{}\\
\mbox{{\#}</oopp/py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of usage:
\begin{quote}
\begin{verbatim}>>> from oopp import kwdict
>>> book=kwdict({'title': "OOPP", 'author': "M.S."})
>>> book.author #it works
'M.S.'
>>> book["author"] # this also works
'M.S.'
>>> book.year=2003 #you may also add new fields on the fly
>>> print book
author = M.S.
title = OOPP
year = 2003\end{verbatim}
\end{quote}

The advantage of subclassing the built-in 'dict', it that you have for free 
all the standard dictionary methods, without having to reimplement them.

However, to subclass built-in it is not always a piece of cake. In
many cases there are complications, indeed. Suppose for instance
one wants to create an enhanced string type, with
the ability of indent and dedent a block of text provided by 
the following functions:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~indent(block,n):}\\
\mbox{~~~~~"Indent~a~block~of~code~by~n~spaces"}\\
\mbox{~~~~~return~'{\textbackslash}n'.join(['~'*n+line~for~line~in~block.splitlines()])}\\
\mbox{}\\
\mbox{def~dedent(block):}\\
\mbox{~~~~"Dedent~a~block~of~code,~if~need~there~is"""}\\
\mbox{~~~~lines=block.splitlines()}\\
\mbox{~~~~for~line~in~lines:}\\
\mbox{~~~~~~~~strippedline=line.lstrip()}\\
\mbox{~~~~~~~~if~strippedline:~break}\\
\mbox{~~~~spaces=len(line)-len(strippedline)}\\
\mbox{~~~~if~not~spaces:~return~block}\\
\mbox{~~~~return~'{\textbackslash}n'.join([line[spaces:]~for~line~in~lines])}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The solution is to inherit from the built-in string type \texttt{str}, and to 
add to the new class the \texttt{indent} and \texttt{dedent} methods:
\begin{quote}
\begin{verbatim}>>> from oopp import indent,dedent
>>> class Str(str):
...    indent=indent
...    dedent=dedent
>>> s=Str('spam\neggs')
>>> type(s)
<class '__main__.Str'>
>>> print s.indent(4)
    spam
    eggs\end{verbatim}
\end{quote}

However, this approach has a disadvantage, since the output of \texttt{indent} is
not a \texttt{Str}, but a normal \texttt{str}, therefore without the additional 
\texttt{indent} and \texttt{dedent} methods:
\begin{quote}
\begin{verbatim}>>> type(s.indent(4))
<type 'str'>
>>> s.indent(4).indent(4) #error
Traceback (most recent call last):
  File "<stdin>", line 9, in ?
AttributeError: 'str' object has no attribute 'indent'
>>> s.indent(4).dedent(4) #error
Traceback (most recent call last):
  File "<stdin>", line 9, in ?
AttributeError: 'str' object has no attribute 'dedent'\end{verbatim}
\end{quote}

We would like \texttt{indent} to return a \texttt{Str} object. To solve this problem
it is enough to rewrite the class as follows:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<Str.py>}\\
\mbox{}\\
\mbox{from~oopp~import~indent,dedent}\\
\mbox{}\\
\mbox{class~Str(str):}\\
\mbox{~~~def~indent(self,n):}\\
\mbox{~~~~~~~return~Str(indent(self,n))}\\
\mbox{~~~def~dedent(self):}\\
\mbox{~~~~~~~return~Str(dedent(self))}\\
\mbox{}\\
\mbox{s=Str('spam{\textbackslash}neggs').indent(4)}\\
\mbox{print~type(s)}\\
\mbox{print~s~{\#}~indented~s}\\
\mbox{s=s.dedent()}\\
\mbox{print~type(s)}\\
\mbox{print~s~{\#}~non-indented~s}\\
\mbox{}\\
\mbox{{\#}</Str.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now, everything works and the output of the previous script is
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{<class~'Str'>}\\
\mbox{~~~~spam}\\
\mbox{~~~~eggs}\\
\mbox{<class~'Str'>}\\
\mbox{spam}\\
\mbox{eggs}
\end{flushleft}\end{ttfamily}
\end{quote}

The solution works because now \texttt{indent()} returns an instance
of \texttt{Str}, which therefore has an \texttt{indent} method. Unfortunately,
this is not the end. Suppose we want to add another food to our list:
\begin{quote}
\begin{verbatim}>>> s2=s+Str("\nham")
>>> s2.indent(4) #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'indent'\end{verbatim}
\end{quote}

The problem is the same, again: the type of \texttt{s2} is \texttt{str}
\begin{quote}
\begin{verbatim}>>> type(s2)
<type 'str'>\end{verbatim}
\end{quote}

and therefore there is no \texttt{indent} method available. There is a
solution to this problem, i.e. to redefine the addition operator
for objects of the class \texttt{Str}. This can be done directly by hand,
but it is \emph{ugly} for the following reasons:
\newcounter{listcnt22}
\begin{list}{\arabic{listcnt22}.}
{
\usecounter{listcnt22}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
If you derive a new class from \texttt{Str} you have to redefine the
addition operator (both the left addition and the right addition [\hyperlink{id24}{12}])
again (ughh!);

\item {} 
There are others operators you must redefine, in particular the
the augumented assignement operator \texttt{+=}, the repetition operator \texttt{*} 
and its augmented version \texttt{*=};

\item {} 
In the case of numeric types, one must redefine, \texttt{+,-,*,/,//, mod,},
possibily \texttt{<<,>>} and others, including  the corresponding 
augumented assignement operators and the left and the right form of
the operators.

\end{list}

This is a mess, especially since due to point 1, one has to redefined
all the operators each time she defines a new subclass. I short, one has
to write a lot of boilerplate for a stupid job that the language
should be able to perform itself automatically. But here are the
good news: Python \emph{can} do all that automatically, in an elegant
and beautiful way, which works for all types, too.

But this requires the magic of metaclasses.
\begin{quote}
\begin{figure}[b]\hypertarget{id26}[13]
The right addition works this way. Python looks at the expression x+y
and if x has an explicit{\_}{\_}add{\_}{\_} method invokes it; on the other hand, 
if x does not define  an {\_}{\_}add{\_}{\_} method, Python considers y+x. 
If y defines a {\_}{\_}radd{\_}{\_} method, it invokes it, otherwise
raises an exception. The same is done for right multiplication, etc.
\end{figure}
\end{quote}


%___________________________________________________________________________

\hypertarget{controlling-the-creation-of-objects}{}
\pdfbookmark[1]{Controlling the creation of objects}{controlling-the-creation-of-objects}
\subsection*{Controlling the creation of objects}

Before introducing multiple inheritance, let me make a short digression on
the mechanism of object creation in Python 2.2+. The important point is
that new style classes have a \texttt{{\_}{\_}new{\_}{\_}} static method that allows
the user to take complete control of object creation. To understand how
\texttt{{\_}{\_}new{\_}{\_}} works, I must explain what happens when an object is instantiated
with a statement like
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{s=Str("spam")~{\#}object~creation}
\end{flushleft}\end{ttfamily}
\end{quote}

What happens under the hood, is that the special static method  \texttt{{\_}{\_}new{\_}{\_}} 
of the class \texttt{Str} (inherited from the built-in \texttt{str} class)
is invoked \texttt{before} the \texttt{Str.{\_}{\_}init{\_}{\_}} method. This means that
the previous line should really be considered syntactic sugar for:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{s=Str.{\_}{\_}new{\_}{\_}(Str,"spam")~{\#}~Str.{\_}{\_}new{\_}{\_}~is~actually~str.{\_}{\_}new{\_}{\_}}\\
\mbox{assert~isinstance(s,Str)}\\
\mbox{Str.{\_}{\_}init{\_}{\_}(s,"spam")~~{\#}~Str.{\_}{\_}init{\_}{\_}~is~actually~str.{\_}{\_}init{\_}{\_}}
\end{flushleft}\end{ttfamily}
\end{quote}

Put it more verbosely, what happens during the object creation is the
following:
\newcounter{listcnt23}
\begin{list}{\arabic{listcnt23}.}
{
\usecounter{listcnt23}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
the static method \texttt{{\_}{\_}new{\_}{\_}}  is invoked with the class of the created 
object as first argument [\hyperlink{id26}{13}];

\item {} 
\texttt{{\_}{\_}new{\_}{\_}} returns an instance of that class.

\item {} 
the instance is then initialized by the \texttt{{\_}{\_}init{\_}{\_}} method.

\end{list}

Notice that both \texttt{{\_}{\_}new{\_}{\_}} and \texttt{{\_}{\_}init{\_}{\_}} are called with the same 
argument list, therefore one must make sure that they have a compatible 
signature.

Let me discuss now why \texttt{{\_}{\_}new{\_}{\_}} must be a static method.
First of all, it cannot be a normal method with a first argument which is an
instance of the calling class, since at the time of \texttt{{\_}{\_}new{\_}{\_}} invocation
that instance (\texttt{myclock} in the example) has still to be created
Since \texttt{{\_}{\_}new{\_}{\_}} needs information about the class calling it, one
could think of implementing \texttt{{\_}{\_}new{\_}{\_}} as a class method. However,
this would implicitly pass the caller class and return an instance
of it. It is more convenient, to have the ability of creating
instances of any class directly from C.{\_}{\_}new{\_}{\_}(B,*args,**kw)

For this reasons, \texttt{{\_}{\_}new{\_}{\_}} must be a static method and pass explicitly
the class which is calling it.

Let me now show an important application of the \texttt{{\_}{\_}new{\_}{\_}} static method:
forbidding object creation. For instance, sometimes it is useful to have 
classes that cannot be instantiated. This kind of classes can be
obtained by inheriting from a \texttt{NonInstantiable} class:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~NonInstantiableError(Exception):~}\\
\mbox{~~~~pass}\\
\mbox{}\\
\mbox{class~NonInstantiable(object):~}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(cls,*args,**kw):}\\
\mbox{~~~~~~~~raise~NonInstantiableError("{\%}s~cannot~be~instantiated"~{\%}~cls)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of usage:
\begin{quote}
\begin{verbatim}>>> from oopp import NonInstantiable,get_time
>>> class Clock(NonInstantiable): 
...    get_time=staticmethod(get_time)
>>> Clock.get_time() # works
'18:48:08'
Clock() #error
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in ?
    Clock()
  File "oopp.py", line 257, in __new__
    raise NonInstantiableError("%s cannot be instantiated" % cls)
NonInstantiableError: <class '__main__.Clock'> cannot be instantiated\end{verbatim}
\end{quote}

However, the approach pursued here has a disadvantage:\texttt{Clock} was already 
defined as a subclass of \texttt{object} and I has to change the source code
to make it a subclass of 'NonInstantiable'. But what happens if 
I cannot change the sources? How can I \emph{reuse} the old code?

The solution is provided by multiple inheritance.

Notice that '{\_}{\_}new{\_}{\_}' is a staticmethod: [\hyperlink{id29}{14}]
\begin{quote}
\begin{quote}
\begin{verbatim}>>> type(NonInstantiable.__dict__['__new__'])
<type 'staticmethod'>\end{verbatim}
\end{quote}
\begin{figure}[b]\hypertarget{id29}[14]
This is how \texttt{type(s)} or \texttt{s.{\_}{\_}class{\_}{\_}} get to know that 
\texttt{s} is an instance of \texttt{Str}, since the class information is 
explicitely passed to the newborn object trough \texttt{{\_}{\_}new{\_}{\_}}.
\end{figure}
\begin{figure}[b]\hypertarget{id30}[15]
However \texttt{object.{\_}{\_}dict{\_}{\_}['{\_}{\_}new{\_}{\_}']} is not a staticmethod
\begin{verbatim}>>> type(object.__dict__['__new__']) # special case
<type 'builtin_function_or_method'>\end{verbatim}
\end{figure}
\end{quote}


%___________________________________________________________________________

\hypertarget{multiple-inheritance}{}
\pdfbookmark[1]{Multiple Inheritance}{multiple-inheritance}
\subsection*{Multiple Inheritance}

Multiple Inheritance (often abbreviated as MI) is often 
considered one of the most advanced topic in Object Oriented Programming. 
It is also one of the most difficult features to implement
in an Object Oriented Programming language. Even, some languages by design
decided to avoid it. This is for instance the case of Java, that avoided
MI having seen its implementation in C++ (which is not for the faint of
heart ;-) and uses a poorest form of it trough interfaces.
For what concerns the scripting languages, of which
the most famous are Perl, Python and Ruby (in this order, even if
the right order would be Python, Ruby and Perl), only Python
implements Multiple Inheritance well (Ruby has a restricted form
of it trough mix-ins, whereas Perl implementation is too difficult
for me to understand what it does ;).

The fact that Multiple Inheritance can be hairy, does not mean that it
is \emph{always} hairy, however. Multiple Inheritance is used with success
in Lisp derived languages (including Dylan).

The aims of this chapter is to discuss the
Python support for MI in the most recent version (2.2 and 2.3), which
has considerably improved with respect to previous versions.
The message is the following: if Python 1.5 had a basic support for
MI inheritance (basic but nevertheless with nice features, dynamic),
Python 2.2 has \emph{greatly} improved that support and with the
change of the Method Resolution Order in Python 2.3, we may say
that support for MI is now \emph{excellent}.

I strongly encourage Python programmers to use MI a lot: this will
allows even a stronger reuse of code than in single inheritance.

Often, inheritance is used when one has a complicate class B, and she wants 
to modify (or enhance) its behavior, by deriving a child class C, which is 
only slightly different from B. In this situation,  B is already a standalone 
class, providing some non-trivial functionality, independently from 
the existence of C.  This kind of design it typical of the so called
\emph{top-down} philosophy, where one builds the 
all structure as a monolithic block, leaving room only for minor improvements.
An alternative approach is the so called \emph{bottom-up} programming, in 
which one builds complicate things starting from very simple building blocks. 
In this logic, it is very appealing the idea of creating classes with the
only purpose of being derived. The 'NonInstantiable' just defined is a
perfect example of this kind of classes, though with multiple inheritance
in mind and often called \emph{mixin} classes.
It can be used to create a new class \texttt{NonInstantiableClock} 
that inherits from \texttt{Clock} and from \texttt{NonInstantiable}.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~NonInstantiableClock(Clock,NonInstantiable):~}\\
\mbox{~~~~pass}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now \texttt{NonInstantiableClock} is both a clock
\begin{quote}
\begin{verbatim}>>> from oopp import NonInstantiableClock
>>> NonInstantiableClock.get_time() # works
'12:57:00' \end{verbatim}
\end{quote}

and a non-instantiable class:
\begin{quote}
\begin{verbatim}>>> NonInstantiableClock() # as expected, give an error
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in ?
    NonInstantiableClock() # error
  File "oopp.py", line 245, in __new__
    raise NonInstantiableError("%s cannot be instantiated" % cls)
NonInstantiableError: <class 'oopp.NonInstantiableClock'> 
cannot be instantiated\end{verbatim}
\end{quote}

Let me give a simple example of a situation where the mixin approach 
comes handy. Suppose that the owner of a 'Pizza-shop' needs a program to 
take care of all the pizzas to-go he sell. Pizzas are distinguished 
according to their size (small, medium or large) and their toppings. 
The problem can be solved by inheriting from a generic pizza factory
like this:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~GenericPizza(object):~{\#}~to~be~customized}\\
\mbox{~~~~toppinglist=[]~{\#}~nothing,~default~}\\
\mbox{~~~~baseprice=1~{\#}~one~dollar,~default}\\
\mbox{~~~~topping{\_}unit{\_}price=0.5~{\#}~half~dollar~for~each~topping,~default}\\
\mbox{~~~~sizefactor={\{}'small':1,~'medium':2,~'large':3{\}}~}\\
\mbox{~~~~{\#}~a~medium~size~pizza~costs~twice~a~small~pizza,~}\\
\mbox{~~~~{\#}~a~large~pizza~costs~three~times}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,size):}\\
\mbox{~~~~~~~~self.size=size}\\
\mbox{~~~~def~price(self):}\\
\mbox{~~~~~~~~return~(self.baseprice+}\\
\mbox{~~~~~~~~~~~~~~~self.toppings{\_}price())*self.sizefactor[self.size]}\\
\mbox{~~~~def~toppings{\_}price(self):}\\
\mbox{~~~~~~~~return~len(self.toppinglist)*self.topping{\_}unit{\_}price}\\
\mbox{~~~~def~{\_}{\_}str{\_}{\_}(self):}\\
\mbox{~~~~~~~~return~'{\%}s~pizza~with~{\%}s,~cost~{\$}~{\%}s'~{\%}~(self.size,}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~','.join(self.toppinglist),}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~self.price())}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here the base class 'GenericPizza' is written with inheritance in mind: one
can derives many pizza classes from it by overriding the \texttt{toppinglist};
for instance one could define
\begin{quote}
\begin{verbatim}>>> from oopp import GenericPizza
>>> class Margherita(GenericPizza): 
...     toppinglist=['tomato']\end{verbatim}
\end{quote}

The problem of this approach is that one must define dozens of 
different pizza subclasses (Marinara, Margherita, Capricciosa, QuattroStagioni, 
Prosciutto, ProsciuttoFunghi, PizzaDellaCasa, etc. etc. [\hyperlink{id30}{15}]). In such a 
situation, it is better to perform the generation of subclasses in a smarter 
way, i.e. via a customizable class factory.
A simpler approach is to use always the same class and to customize
its instances just after creation. Both approaches can be implemented via
the following 'Customizable' mixin class, not meant to be instantiated, 
but rather to be \emph{inherited}:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Customizable(object):}\\
\mbox{~~~~"""Classes~inhering~from~'Customizable'~have~a~'with'~method~acting~as}\\
\mbox{~~~~an~object~modifier~and~'With'~classmethod~acting~as~a~class~factory"""}\\
\mbox{~~~~def~with(self,**kw):}\\
\mbox{~~~~~~~~customize(self,**kw){\#}~customize~the~instance}\\
\mbox{~~~~~~~~return~self~{\#}~returns~the~customized~instance}\\
\mbox{~~~~def~With(cls,**kw):}\\
\mbox{~~~~~~~~class~ChildOf(cls):~pass~{\#}~a~new~class~inheriting~from~cls}\\
\mbox{~~~~~~~~ChildOf.{\_}{\_}name{\_}{\_}=cls.{\_}{\_}name{\_}{\_}~{\#}~by~default,~with~the~same~name}\\
\mbox{~~~~~~~~customize(ChildOf,**kw)~~~~~~~{\#}~of~the~original~class}\\
\mbox{~~~~~~~~return~ChildOf}\\
\mbox{~~~~With=classmethod(With)~}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Descendants of 'Customizable' can be customized by using
'with', that directly acts on the instances, or 'With', that returns
new classes. Notice that one could make 'With' to customize the
original class, without returning a new one; however, in practice,
this would not be safe: I remind that changing a class modifies
automatically all its instances, even instances created \emph{before}
the modification. This could produce bad surprises: it is better to 
returns new classes, that may have the same name of the original one, 
but are actually completely independent from it.

In order to solve the pizza shop problem we may define a 'CustomizablePizza' 
class
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~CustomizablePizza(GenericPizza,Customizable):}\\
\mbox{~~~~pass}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

which can be used in two ways: i) to customize instances just after creation:
\begin{quote}
\begin{verbatim}>>> from oopp import CustomizablePizza
>>> largepizza=CustomizablePizza('large') # CustomizablePizza instance
>>> largemarinara=largepizza.with(toppinglist=['tomato'],baseprice=2)
>>> print largemarinara
large pizza with tomato mozzarella, cost $ 7.0\end{verbatim}
\end{quote}

and ii) to generated customized new classes:
\begin{quote}
\begin{verbatim}>>> Margherita=CustomizablePizza.With(
...    toppinglist=['tomato','mozzarella'], __name__='Margherita')
>>> print Margherita('medium')
medium pizza with tomato,mozzarella, cost $ 4.0\end{verbatim}
\end{quote}

The advantage of the bottom-up approach, is that the 'Customizable' class
can be reused in completely different problems; for instance, it could
be used as a class factory. For instance we could use it to generate a 
'CustomizableClock' class as in this example:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> CustomizableClock=Customizable.With(get_time=staticmethod(Clock.get_time),
... __name__='CustomizableClock') #adds get_time
>>> CustomizableClock.get_time() # now it works
'09:57:50'\end{verbatim}
\end{quote}

Here 'Customizable' ``steal'' the 'get{\_}time' method from 'Clock'.
However that would be a rather perverse usage ;) I wrote it to show
the advantage of classmethods, more than to suggest to the reader that
this is an example of good programming.
\begin{quote}
\begin{figure}[b]\hypertarget{id32}[16]
In Italy, you can easily find ``pizzerie'' with more than 50 different 
kinds of pizzas (once I saw a menu with something like one hundred 
different combinations ;)
\end{figure}
\end{quote}


%___________________________________________________________________________

\hypertarget{cooperative-hierarchies}{}
\pdfbookmark[1]{Cooperative hierarchies}{cooperative-hierarchies}
\subsection*{Cooperative hierarchies}

The examples of multiple inheritance hierarchies given until now were pretty 
easy. The reason is that there was no interaction between the methods of the 
children and of the parents. However, things get more complicated (and
interesting ;) when the methods in the hierarchy call each other. 
Let me consider an example coming from paleoantropology:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<paleoanthropology1.py>}\\
\mbox{}\\
\mbox{class~HomoHabilis(object):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~print~self,'can:'}\\
\mbox{~~~~~~~~print~"~-~make~tools"}\\
\mbox{}\\
\mbox{class~HomoSapiens(HomoHabilis):}\\
\mbox{~~~~def~can(self):~{\#}overrides~HomoHabilis.can}\\
\mbox{~~~~~~~~HomoHabilis.can(self)}\\
\mbox{~~~~~~~~print~"~-~make~abstractions"}\\
\mbox{~~~~~~}\\
\mbox{class~HomoSapiensSapiens(HomoSapiens):}\\
\mbox{~~~~def~can(self):~{\#}overrides~HomoSapiens.can}\\
\mbox{~~~~~~~~HomoSapiens.can(self)}\\
\mbox{~~~~~~~~print~"~-~make~art"}\\
\mbox{}\\
\mbox{modernman=HomoSapiensSapiens()}\\
\mbox{modernman.can()}\\
\mbox{}\\
\mbox{{\#}</paleoanthropology1.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

In this example children methods call parent methods:  
'HomoSapiensSapiens.can' calls 'HomoSapiens.can' that in turns calls
'HomoHabilis.can' and the final output is:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{<{\_}{\_}main{\_}{\_}.HomoSapiensSapiens~object~at~0x814e1fc>~can:}\\
\mbox{~-~make~tools}\\
\mbox{~-~make~abstractions}\\
\mbox{~-~make~art}
\end{flushleft}\end{ttfamily}
\end{quote}

The script works, but it is far from ideal,  if code reuse and refactoring
are considered important requirements. The point is that (very likely, as the
research in paleoanthropology progresses) we may want to extend the 
hierarchy, for instance by adding a class on the top or in the middle. 
In the present form, this would require a non-trivial modification of 
the source code (especially
if one think that the hierarchy could be fleshed out with dozens of others
methods and attributes). However, the aim of OOP is to avoid as 
much as possible source code modifications. This goal can be attained in
practice, if the source code is written to be friendly to extensions and 
improvements as much as possible. I think it is worth to spend some time 
in improving this example, since what can be learn here, 
can be lifted to real life cases.

First of all, let me define a generic \emph{Homo} class, to be used
as first ring of the inheritance chain (actually the first ring is
'object'):
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Homo(PrettyPrinted):~}\\
\mbox{~~~~"""Defines~the~method~'can',~which~is~intended~to~be~overriden~}\\
\mbox{~~~~in~the~children~classes,~and~inherits~'{\_}{\_}str{\_}{\_}'~from~PrettyPrinted,}\\
\mbox{~~~~ensuring~a~nice~printing~representation~for~all~children."""}\\
\mbox{~~~~def~can(self):~}\\
\mbox{~~~~~~~~print~self,'can:'}\\
\mbox{~~~~}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now, let me point out one of the shortcomings of the previous code: in each
subclass, we explicitly call its parent class (also called super class)
by its name. This is inconvenient, both because a change of name in
later stages of the project would require a lot of search and replace
(actually not a lot in this toy example, but you can imagine having
a very big projects with dozens of named method calls) and because it makes 
difficult to insert a new element in the inheritance hierarchy. 
The solution to this problems is the
\texttt{super} built-in, which provides an easy access to the methods
of the superclass.
\texttt{super} objects comes in two flavors: \texttt{super(cls,obj)} objects return 
bound methods whereas \texttt{super(cls)} objects return unbound methods.
In the next code we will use the first form. The hierarchy can more elegantly 
be rewritten as [\hyperlink{id32}{16}] :
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<paleo2.py>}\\
\mbox{}\\
\mbox{from~oopp~import~Homo}\\
\mbox{}\\
\mbox{class~HomoHabilis(Homo):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~super(HomoHabilis,self).can()}\\
\mbox{~~~~~~~~print~"~-~make~tools"}\\
\mbox{}\\
\mbox{class~HomoSapiens(HomoHabilis):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~super(HomoSapiens,self).can()}\\
\mbox{~~~~~~~~print~"~-~make~abstractions"}\\
\mbox{~~~~~~}\\
\mbox{class~HomoSapiensSapiens(HomoSapiens):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~super(HomoSapiensSapiens,self).can()}\\
\mbox{~~~~~~~~print~"~-~make~art"}\\
\mbox{}\\
\mbox{}\\
\mbox{HomoSapiensSapiens().can()}\\
\mbox{}\\
\mbox{{\#}</paleo2.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{<HomoSapiensSapiens>~can:}\\
\mbox{~-~make~tools}\\
\mbox{~-~make~abstractions}\\
\mbox{~-~make~art}
\end{flushleft}\end{ttfamily}
\end{quote}

This is not yet the most elegant form, since even
if \texttt{super} avoids naming the base class explicitely, still it
requires to explicitely name the class where it is defined. This is
rather annoying.
Removing that restriction, i.e. implementing really anonymous 
\texttt{super} calls, is possible but requires a good understand of
private variables in inheritance.


%___________________________________________________________________________

\hypertarget{inheritance-and-privacy}{}
\pdfbookmark[1]{Inheritance and privacy}{inheritance-and-privacy}
\subsection*{Inheritance and privacy}

In order to define anonymous cooperative super calls,  we need classes 
that know themselves, i.e. containing a reference to themselves. This
is not an obvious problem as it could seems, since it cannot be solved
without incurring in the biggest annoyance in inheritance: 
\emph{name clashing}. Name clashing happens when names and attributes defined 
in different ancestors overrides each other in a unwanted order.
Name clashing is especially painful in the case of cooperative 
hierarchies and particularly in in the problem at hand.

A naive solution would be to attach a plain (i.e. non-private) 
attribute '.this' to the class, containing a reference
to itself, that can be invoked by the methods of the class.
Suppose, for instance, that I want to use that attribute in the \texttt{{\_}{\_}init{\_}{\_}}
method of that class. A naive attempt would be to write something like:
\begin{quote}
\begin{verbatim}>>> class B(object):
...     def __init__(self): 
...         print self.this,'.__init__' # .this defined later
>>> B.this=B # B.this can be set only after B has been created
>>> B()
<class '__main__.B'>\end{verbatim}
\end{quote}

Unfortunately, this approach does not work with cooperative hierarchies. 
Consider, for instance, extending 'B' with a cooperative children 
class 'C' as follows:
\begin{quote}
\begin{verbatim}>>> class C(B):
...    def __init__(self):
...       super(self.this,self).__init__() # cooperative call
...       print type(self).this,'.__init__'
>>> C.this=C\end{verbatim}
\end{quote}

\texttt{C.{\_}{\_}init{\_}{\_}} calls \texttt{B.{\_}{\_}init{\_}{\_}} by passing a 'C' instance, therefore
\texttt{C.this} is printed and not \texttt{B.this}:
\begin{quote}
\begin{verbatim}>>> C()
<class '__main__.C'> .__init__
<class '__main__.C'> .__init__
<__main__.C object at 0x4042ca6c>\end{verbatim}
\end{quote}

The problem is that the \texttt{C.this} overrides \texttt{B.this}. The only
way of avoiding the name clashing is to use a private attribute 
\texttt{.{\_}{\_}this}, as in the following script:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<privateh.py>}\\
\mbox{}\\
\mbox{class~B(object):}\\
\mbox{~~~~~def~{\_}{\_}init{\_}{\_}(self):~}\\
\mbox{~~~~~~~~print~self.{\_}{\_}this,'.{\_}{\_}init{\_}{\_}'}\\
\mbox{B.{\_}B{\_}{\_}this=B}\\
\mbox{}\\
\mbox{class~C(B):}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self):}\\
\mbox{~~~~~~~super(self.{\_}{\_}this,self).{\_}{\_}init{\_}{\_}()~{\#}~cooperative~{\_}{\_}init{\_}{\_}~}\\
\mbox{~~~~~~~print~self.{\_}{\_}this,'.{\_}{\_}init{\_}{\_}'}\\
\mbox{C.{\_}C{\_}{\_}this=C}\\
\mbox{}\\
\mbox{C()}\\
\mbox{}\\
\mbox{{\#}~output:}\\
\mbox{{\#}~<class~'{\_}{\_}main{\_}{\_}.B'>~.{\_}{\_}init{\_}{\_}}\\
\mbox{{\#}~<class~'{\_}{\_}main{\_}{\_}.C'>~.{\_}{\_}init{\_}{\_}}\\
\mbox{}\\
\mbox{{\#}</privateh.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The script works since, due to the magic of the mangling mechanism,
in \texttt{B.{\_}{\_}init{\_}{\_}}, \texttt{self.{\_}B{\_}{\_}this} i.e. \texttt{B} is retrieved, whereas in 
\texttt{C.{\_}{\_}init{\_}{\_}} \texttt{self.{\_}C{\_}{\_}this} i.e. \texttt{C} is retrieved.

The elegance of the mechanism can be improved with an helper function
that makes its arguments reflective classes, i.e. classes with a
\texttt{{\_}{\_}this} private attribute:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~reflective(*classes):}\\
\mbox{~~~~"""Reflective~classes~know~themselves,~i.e.~they~possess~a~private}\\
\mbox{~~~~attribute~{\_}{\_}this~containing~a~reference~to~themselves.~If~the~class}\\
\mbox{~~~~name~starts~with~'{\_}',~the~underscores~are~stripped."""}\\
\mbox{~~~~for~c~in~classes:}\\
\mbox{~~~~~~~~name=c.{\_}{\_}name{\_}{\_}~.lstrip('{\_}')~~{\#}~in~2.3}\\
\mbox{~~~~~~~~setattr(c,'{\_}{\%}s{\_}{\_}this'~{\%}~name,c)~}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

It is trivial to rewrite the paleonthropological hierarchy in terms of 
anonymous cooperative super calls by using this trick.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~HomoHabilis(Homo):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~super(self.{\_}{\_}this,self).can()}\\
\mbox{~~~~~~~~print~"~-~make~tools"}\\
\mbox{}\\
\mbox{class~HomoSapiens(HomoHabilis):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~super(self.{\_}{\_}this,self).can()}\\
\mbox{~~~~~~~~print~"~-~make~abstractions"}\\
\mbox{~~~~~~}\\
\mbox{class~HomoSapiensSapiens(HomoSapiens):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~super(self.{\_}{\_}this,self).can()}\\
\mbox{~~~~~~~~print~"~-~make~art"}\\
\mbox{}\\
\mbox{reflective(HomoHabilis,HomoSapiens,HomoSapiensSapiens)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of usage:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> man=HomoSapiensSapiens(); man.can()
<HomoSapiensSapiens> can:
 - make tools
 - make abstractions
 - make art\end{verbatim}
\end{quote}

We may understand why it works by looking at the attributes of man:
\begin{quote}
\begin{verbatim}>>> print pretty(attributes(man))
_HomoHabilis__this = <class 'oopp.HomoHabilis'>
_HomoSapiensSapiens__this = <class 'oopp.HomoSapiensSapiens'>
_HomoSapiens__this = <class 'oopp.HomoSapiens'>
can = <bound method HomoSapiensSapiens.can of 
      <oopp.HomoSapiensSapiens object at 0x404292ec>>
formatstring = %s\end{verbatim}
\end{quote}

It is also interesting to notice that the hierarchy can be entirely
rewritten without using cooperative methods, but using private attributes,
instead. This second approach is simpler, as the following script shows:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<privatehierarchy.py>}\\
\mbox{}\\
\mbox{from~oopp~import~PrettyPrinted,attributes,pretty}\\
\mbox{}\\
\mbox{class~Homo(PrettyPrinted):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~print~self,'can:'}\\
\mbox{~~~~~~~~for~attr,value~in~attributes(self).iteritems():~}\\
\mbox{~~~~~~~~~~~~if~attr.endswith('{\_}{\_}attr'):~print~value}\\
\mbox{class~HomoHabilis(Homo):~}\\
\mbox{~~~~{\_}{\_}attr="~-~make~tools"}\\
\mbox{class~HomoSapiens(HomoHabilis):~}\\
\mbox{~~~~{\_}{\_}attr="~-~make~abstractions"}\\
\mbox{class~HomoSapiensSapiens(HomoSapiens):~}\\
\mbox{~~~~{\_}{\_}attr="~-~make~art"}\\
\mbox{}\\
\mbox{modernman=HomoSapiensSapiens()}\\
\mbox{modernman.can()}\\
\mbox{print~'----------------------------------{\textbackslash}nAttributes~of',modernman}\\
\mbox{print~pretty(attributes(modernman))}\\
\mbox{}\\
\mbox{{\#}</privatehierarchy.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here I have replaced the complicate chain of cooperative methods with 
much simpler private attributes. Only the 'can' method in the 'Homo' 
class survives, and it is modified to print the value of the '{\_}{\_}attr' 
attributes. Moreover, all the classes of the hierarchy have been made
'Customizable', in view of future extensions.

The second script is much shorter and much more elegant than the original
one, however its logic can be a little baffling, at first. The solution
to the mistery is provided by the attribute dictionary of 'moderman',
given by the second part of the output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{<HomoSapiensSapiens>~can:}\\
\mbox{~-~make~abstractions~~}\\
\mbox{~-~make~art~~}\\
\mbox{~-~make~tools~}\\
\mbox{------------------------------------------}\\
\mbox{Attributes~of~<HomoSapiensSapiens>:}\\
\mbox{{\_}HomoHabilis{\_}{\_}attr~=~~-~make~tools}\\
\mbox{{\_}HomoSapiensSapiens{\_}{\_}attr~=~~-~make~art}\\
\mbox{{\_}HomoSapiens{\_}{\_}attr~=~~-~make~abstractions}\\
\mbox{can~=~<bound~method~HomoSapiensSapiens.can~of~}\\
\mbox{~~~~~~<{\_}{\_}main{\_}{\_}.HomoSapiensSapiens~object~at~0x402d892c>>}\\
\mbox{formatstring~=~{\%}s}
\end{flushleft}\end{ttfamily}
\end{quote}

We see that, in addition to the 'can' method inherited from 'Homo',
the 'with' and 'With' method inherited from 'Customizable' and
the 'formatstring' inherited from 'PrettyPrinted', 
\texttt{moderman} has the attributes
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\_}HomoHabilis{\_}{\_}attr:'~-~make~tools'~{\#}~inherited~from~HomoHabilis}\\
\mbox{{\_}HomoSapiens{\_}{\_}attr:'~-~make~abstractions'{\#}~inherited~from~HomoSapiens}\\
\mbox{{\_}HomoSapiensSapiens{\_}{\_}attr:~'~-~make~art'~{\#}~inherited~from~HomoSapiensSapiens}
\end{flushleft}\end{ttfamily}
\end{quote}

which origin is obvious, once one reminds the mangling mechanism associated
with private variables. The important point is that the trick would \emph{not}
have worked for normal attributes. Had I used as variable name
'attr' instead of '{\_}{\_}attr', the name would have been overridden: the only
attribute of 'HomoSapiensSapiens' would have been ' - make art'.

This example explains the advantages of private variables during inheritance:
they cannot be overridden. Using private name guarantees the absence of 
surprises due to inheritance. If a class B has only private variables,
deriving a class C from B cannot cause name clashes.

Private variables have a drawbacks, too. The most obvious disadvantages is
the fact that in order to customize private variables outside their 
defining class, one needs to pass explicitly the name of the class.

For instance we could not change an attribute with the syntax 
\texttt{HomoHabilis.With({\_}{\_}attr=' - work the stone')}, we must write the 
more verbose, error prone and redundant 
\texttt{HomoHabilis.With({\_}HomoHabilis{\_}{\_}attr=' - work the stone')}

A subtler drawback will be discussed in chapter 6.
\begin{quote}
\begin{figure}[b]\hypertarget{id34}[17]
In single inheritance hierarchies, \texttt{super} can be dismissed
in favor of \texttt{{\_}{\_}base{\_}{\_}}: for instance, 
\texttt{super(HomoSapiens,self).can()} is equivalent to
\texttt{HomoSapiens.{\_}{\_}base{\_}{\_}.can(self)}. Nevertheless, in view
of possible extensions to multiple inheritance, using \texttt{super} is a
much preferable choice.
\end{figure}
\end{quote}


%___________________________________________________________________________

\hypertarget{the-sophistication-of-descriptors}{}
\pdfbookmark[0]{THE SOPHISTICATION OF DESCRIPTORS}{the-sophistication-of-descriptors}
\section*{THE SOPHISTICATION OF DESCRIPTORS}

Attribute descriptors are important metaprogramming tools that allows 
the user to customize the behavior of attributes in custom classes.
For instance, attribute descriptors (or descriptors for short) 
can be used as method wrappers, 
to modify or enhance methods (this is the case for the well
known staticmethods and classmethods attribute descriptors); they
can also be used as attribute wrappers, to change or restrict the access to 
attributes (this is the case for properties). Finally, descriptors 
allows the user to play with the resolution order of attributes: 
for instance, the \texttt{super} built-in object used in (multiple) inheritance 
hierarchies, is implemented as an attribute descriptor.

In this chapter, I will show how the user can define its own attribute 
descriptors and I will give some example of useful things you can do with 
them (in particular to add tracing and timing capabilities).


%___________________________________________________________________________

\hypertarget{motivation}{}
\pdfbookmark[1]{Motivation}{motivation}
\subsection*{Motivation}

Attribute descriptors are a recent idea (they where first introduced in 
Python 2.2) nevertheless, under the hood, are everywhere in Python. It is 
a tribute to Guido's ability of hiding Python complications that
the average user can easily miss they existence.
If you need to do simple things, you can very well live without 
the knowledge of descriptors. On the other hand, if you need difficult 
things (such as tracing all the attribute access of your modules) 
attribute descriptors, allow you to perform 
impressive things. 
Let me start by showing why the knowledge of attribute descriptors is 
essential for any user seriously interested  in metaprogramming applications.
Suppose I  want to trace the methods of a clock:
\begin{quote}
\begin{verbatim}>>> import oopp
>>> clock=oopp.Clock()\end{verbatim}
\end{quote}

This is easily done with the \texttt{with{\_}tracer} closure of chapter 2:
\begin{quote}
\begin{verbatim}>>> oopp.wrapfunctions(clock,oopp.with_tracer)
<oopp.Clock object at 0x4044c54c>
>>> clock.get_time()
[] Calling 'get_time' with arguments
(){} ...
-> '.get_time' called with result: 19:55:07
'19:55:07'\end{verbatim}
\end{quote}

However, this approach fails if I try to trace the entire class:
\begin{quote}
\begin{verbatim}>>> oopp.wrapfunctions(oopp.Clock,oopp.with_tracer)
<class 'oopp.Clock'>
>>> oopp.Clock.get_time() # error
Traceback (most recent call last):
  File "<stdin>", line 6, in ?
TypeError: unbound method _() must be called with Clock instance 
as first argument (got nothing instead)\end{verbatim}
\end{quote}

The reason is that \texttt{wrapfunctions} sets the attributes of 'Clock'
by invoking \texttt{customize}, which uses \texttt{setattr}. This converts
'{\_}' (i.e. the traced version of \texttt{get{\_}time}) in a regular method, not in 
a staticmethod!
In order to trace staticmethods, one has to understand the nature 
of attribute descriptors.


%___________________________________________________________________________

\hypertarget{functions-versus-methods}{}
\pdfbookmark[1]{Functions versus methods}{functions-versus-methods}
\subsection*{Functions versus methods}

Attribute descriptors are essential for the implementation 
of one of the most basic Python features: the automatic conversion 
of functions in methods. As I already anticipated in chapter 1, there is 
a sort of magic when one writes \texttt{Clock.get{\_}time=lambda self: get{\_}time()}
and Python automagically converts the right hand side, that is a
function, to a left hand side that is a (unbound) method. In order to 
understand this magic, one needs a better comprehension of the
relation between functions and methods.
Actually, this relationship is quite subtle 
and has no analogous in mainstream programming languages.
For instance, C is not OOP and has only functions, lacking the concept
of method, whereas Java (as other OOP languages) 
has no functions,  only methods.
C++ has functions and methods, but functions are completely
different from methods On the other hand, in Python, 
functions and methods can be transformed both ways.

To show how it works, let me start by defining a simple printing 
function:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~{\_}{\_}main{\_}{\_}~{\#}~gives~access~to~the~{\_}{\_}main{\_}{\_}~namespace~from~the~module}\\
\mbox{}\\
\mbox{def~prn(s):}\\
\mbox{~~~~"""Given~an~evaluable~string,~print~its~value~and~its~object~reference.}\\
\mbox{~~~~Notice~that~the~evaluation~is~done~in~the~{\_}{\_}main{\_}{\_}~dictionary."""}\\
\mbox{~~~~try:~obj=eval(s,{\_}{\_}main{\_}{\_}.{\_}{\_}dict{\_}{\_})}\\
\mbox{~~~~except:~print~'problems~in~evaluating',s}\\
\mbox{~~~~else:~print~s,'=',obj,'at',hex(id(obj))}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now, let me define a class with a method \texttt{m} equals to the identity
function \texttt{f}:
\begin{quote}
\begin{verbatim}>>> def f(x): "Identity function"; return x
...
>>> class C(object):
...    m=f
...    print m #here m is the function f
<function f at 0x401c2b1c>\end{verbatim}
\end{quote}

We see that \emph{inside} its defining class, \texttt{m} coincides with the function 
\texttt{f} (the object reference is the same):
\begin{quote}
\begin{verbatim}>>> f
<function f at 0x401c2b1c>\end{verbatim}
\end{quote}

We may retrieve \texttt{m} from \emph{outside} the class via the class dictionary [\hyperlink{id34}{17}]:
\begin{quote}
\begin{verbatim}>>> C.__dict__['m']
<function prn at 0x401c2b1c>\end{verbatim}
\end{quote}

However, if we invoke \texttt{m} with
the syntax \texttt{C.m}, then it (magically) becomes a (unbound) method:
\begin{quote}
\begin{verbatim}>>> C.m #here m has become a method!
<unbound method C.f>\end{verbatim}
\end{quote}

But why it is so? How comes that in the second syntax the function 
\texttt{f} is transformed in a (unbound) method? To answer that question, we have
to understand how attributes are really invoked in Python, i.e. via
attribute descriptors.


%___________________________________________________________________________

\hypertarget{methods-versus-functions}{}
\pdfbookmark[1]{Methods versus functions}{methods-versus-functions}
\subsection*{Methods versus functions}

First of all, let me point out the differences between methods and
functions. Here, \texttt{C.m} does \emph{not} coincides with \texttt{C.{\_}{\_}dict{\_}{\_}['m']}
i.e. \texttt{f}, since its object reference is different:
\begin{quote}
\begin{verbatim}>>> from oopp import prn,attributes
>>> prn('C.m')
C.m = <unbound method C.prn> at 0x81109b4\end{verbatim}
\end{quote}

The difference is clear since methods and functions have different attributes:
\begin{quote}
\begin{verbatim}>>> attributes(f).keys()
['func_closure', 'func_dict', 'func_defaults', 'func_name', 
'func_code', 'func_doc', 'func_globals']\end{verbatim}
\end{quote}

whereas
\begin{quote}
\begin{verbatim}>>> attributes(C.m).keys()
['im_func', 'im_class', 'im_self']\end{verbatim}
\end{quote}

We discussed few of the functions attributes in the chapter
on functions. The instance method attributes are simpler: \texttt{im{\_}self} 
returns the object to which the method is attached,
\begin{quote}
\begin{verbatim}>>> print C.m.im_self #unbound method, attached to the class
None
>>> C().m.im_self #bound method, attached to C()
<__main__.C object at 0x81bf4ec> \end{verbatim}
\end{quote}

\texttt{im{\_}class} returns the class to which the
method is attached
\begin{quote}
\begin{verbatim}>>> C.m.im_class #class of the unbound method
<class '__main__.C'>
>>> C().m.im_class #class of the bound method,
<class '__main__.C'>\end{verbatim}
\end{quote}

and \texttt{im{\_}func} returns the function equivalent to
the method.
\begin{quote}
\begin{verbatim}>>> C.m.im_func
<function m at 0x8157f44>
>>> C().m.im_func # the same
<function m at 0x8157f44>\end{verbatim}
\end{quote}

As the reference manual states, calling 
\texttt{m(*args,**kw)} is completely equivalent to calling 
\texttt{m.im{\_}func(m.im{\_}self, *args,**kw)}``.

As a general rule, an attribute descriptor is an object with a \texttt{{\_}{\_}get{\_}{\_}} 
special method. The most used descriptors are the good old functions:
they have a \texttt{{\_}{\_}get{\_}{\_}} special  method returning a \emph{method-wrapper object}
\begin{quote}
\begin{verbatim}>>> f.__get__
<method-wrapper object at 0x815cdc4>\end{verbatim}
\end{quote}

method-wrapper objects can be transformed in (both bound and unbound) methods:
\begin{quote}
\begin{verbatim}>>> f.__get__(None,C)
<unbound method C.f>
>>> f.__get__(C(),C)
<bound method C.f of <__main__.C object at 0x815cdc4>>\end{verbatim}
\end{quote}

The general calling syntax for method-wrapper objects is 
\texttt{.{\_}{\_}get{\_}{\_}(obj,cls=None)}, where the first argument is an
instance object or None and the second (optional) argument is the class (or a
generic superclass) of the first one.

Now we see what happens when we use the syntax \texttt{C.m}: Python interprets
this as a shortcut for \texttt{C.{\_}{\_}dict['m'].{\_}{\_}get{\_}{\_}(None,C)} (if \texttt{m} is
in the 'C' dictionary, otherwise it looks for ancestor dictionaries). 
We may check that everything is correct by observing that
\texttt{f.{\_}{\_}get{\_}{\_}(None,C)} has exactly the same object reference than \texttt{C.m},
therefore they are the same object:
\begin{quote}
\begin{verbatim}>>> hex(id(f.__get__(None,C))) # same as hex(id(C.m))
'0x811095c'\end{verbatim}
\end{quote}

The process works equally well for the syntax \texttt{getattr}:
\begin{quote}
\begin{verbatim}>>> print getattr(C,'m'), hex(id(getattr(C,'m')))
<unbound method C.f> 0x811095c\end{verbatim}
\end{quote}

and for bound methods: if
\begin{quote}
\begin{verbatim}>>> c=C()\end{verbatim}
\end{quote}

is an instance of the class C, then the syntax
\begin{quote}
\begin{verbatim}>>> getattr(c,'m') #same as c.m
<bound method C.f of <__main__.C object at 0x815cdc4>>\end{verbatim}
\end{quote}

is a shortcut for
\begin{quote}
\begin{verbatim}>>> type(c).__dict__['m'].__get__(c,C) # or f.__get__(c,C)
<bound method C.f of <__main__.C object at 0x815cdc4>>\end{verbatim}
\end{quote}

(notice that the object reference for \texttt{c.m} and \texttt{f.{\_}{\_}get{\_}{\_}(c,C)} is
the same, they are \emph{exactly} the same object).

Both the unbound method C.m and the bound method c.m refer to the same 
object at hexadecimal address 0x811095c. This object is common to all other
instances of C:
\begin{quote}
\begin{verbatim}>>> c2=C()
>>> print c2.m,hex(id(c2.m)) #always the same method
<bound method C.m of <__main__.C object at 0x815768c>> 0x811095c\end{verbatim}
\end{quote}

One can also omit the second argument:
\begin{quote}
\begin{verbatim}>>> c.m.__get__(c)
<bound method ?.m of <__main__.C object at 0x81597dc>>\end{verbatim}
\end{quote}

Finally, let me point out that methods are attribute descriptors too,
since they have a \texttt{{\_}{\_}get{\_}{\_}} attribute returning a method-wrapper
object:
\begin{quote}
\begin{verbatim}>>> C.m.__get__
<method-wrapper object at 0x815d51c>\end{verbatim}
\end{quote}

Notice that this method wrapper is \emph{not} the same than the \texttt{f.{\_}{\_}get{\_}{\_}}
method wrapper.
\begin{quote}
\begin{figure}[b]\hypertarget{id36}[18]
If \texttt{C.{\_}{\_}dict['m']} is not defined, Python looks if \texttt{m} is defined
in some ancestor of C. For instance if \titlereference{B} is the base of \titlereference{C}, it
looks in \texttt{B.{\_}{\_}dict['m']}, etc., by following the MRO.
\end{figure}
\end{quote}


%___________________________________________________________________________

\hypertarget{static-methods-and-class-methods}{}
\pdfbookmark[1]{Static methods and class methods}{static-methods-and-class-methods}
\subsection*{Static methods and class methods}

Whereas functions and methods are implicit attribute descriptors,
static methods and class methods are examples of explicit
descriptors. They allow to convert regular functions to 
specific descriptor objects. Let me show a trivial example. 
Given the identity function
\begin{quote}
\begin{verbatim}>>> def f(x): return x\end{verbatim}
\end{quote}

we may convert it to a staticmethod object
\begin{quote}
\begin{verbatim}>>> sm=staticmethod(f)
>>> sm
<staticmethod object at 0x4018a0a0>\end{verbatim}
\end{quote}

or to a classmethod object
\begin{quote}
\begin{verbatim}>>> cm=classmethod(f)
>>> cm
<classmethod object at 0x4018a0b0>\end{verbatim}
\end{quote}

In both cases the \texttt{{\_}{\_}get{\_}{\_}} special method returns a method-wrapper object
\begin{quote}
\begin{verbatim}>>> sm.__get__
<method-wrapper object at 0x401751ec>
>>> cm.__get__
<method-wrapper object at 0x4017524c>\end{verbatim}
\end{quote}

However the static method wrapper is quite different from the class
method wrapper. In the first case the wrapper returns a function:
\begin{quote}
\begin{verbatim}>>> sm.__get__(C(),C)
<function f at 0x4027a8b4>
>>> sm.__get__(C())
<function f at 0x4027a8b4>\end{verbatim}
\end{quote}

in the second case it returns a method
\begin{quote}
\begin{verbatim}>>> cm.__get__(C(),C)
<bound method type.f of <class '__main__.C'>>\end{verbatim}
\end{quote}

Let me discuss more in detail the static methods, first.

It is always possible to extract the function from the static method
via the syntaxes \texttt{sm.{\_}{\_}get{\_}{\_}(a)} and \texttt{sm.{\_}{\_}get{\_}{\_}(a,b)} with \emph{ANY} valid
a and b, i.e. the result does not depend on a and b. This is correct,
since static methods are actually function that have nothing to do
with the class and the instances to which they are bound.

This behaviour of the method wrapper makes clear why the relation between 
methods and functions is inversed for static methods with respect to
regular methods:
\begin{quote}
\begin{verbatim}>>> class C(object):
...     s=staticmethod(lambda : None)
...     print s
...
<staticmethod object at 0x8158ec8>\end{verbatim}
\end{quote}

Static methods are non-trivial objects \emph{inside} the class, whereas 
they are regular functions \emph{outside} the class:
\begin{quote}
\begin{verbatim}>>> C.s
<function <lambda> at 0x8158e7c>
>>> C().s
<function <lambda> at 0x8158e7c>\end{verbatim}
\end{quote}

The situation is different for classmethods: inside the class they
are non-trivial objects, just as static methods,
\begin{quote}
\begin{verbatim}>>> class C(object):
...     cm=classmethod(lambda cls: None)
...     print cm
...
<classmethod object at 0x8156100>\end{verbatim}
\end{quote}

but outside the class they are methods bound to the class,
\begin{quote}
\begin{verbatim}>>> c=C()
>>> prn('c.cm') 
<bound method type.<lambda> of <class '__main__.C'>> 
0x811095c\end{verbatim}
\end{quote}

and not to the instance 'c'. The reason is that the \texttt{{\_}{\_}get{\_}{\_}} wrapper method
can be invoked with the syntax  \texttt{{\_}{\_}get{\_}{\_}(a,cls)} which 
is only sensitive to the second argument or with the syntax
\texttt{{\_}{\_}get{\_}{\_}(obj)} which is only sensitive to the type of the first
argument:
\begin{quote}
\begin{verbatim}>>> cm.__get__('whatever',C) # the first argument is ignored
<bound method type.f of <class '__main__.C'>>\end{verbatim}
\end{quote}

sensitive to the type of 'whatever':
\begin{quote}
\begin{verbatim}>>> cm.__get__('whatever') # in Python 2.2 would give a serious error
<bound method type.f of <type 'str'>>\end{verbatim}
\end{quote}

Notice that the class method is actually bound to C's class, i.e.
to 'type'.

Just as regular methods (and differently
from static methods) classmethods have attributes \texttt{im{\_}class}, \texttt{im{\_}func}, 
and \texttt{im{\_}self}. In particular one can retrieve the function wrapped inside
the classmethod with
\begin{quote}
\begin{verbatim}>>> cm.__get__('whatever','whatever').im_func 
<function f at 0x402c2534>\end{verbatim}
\end{quote}

The difference with regular methods is that \texttt{im{\_}class} returns the
class of 'C' whereas \texttt{im{\_}self} returns 'C' itself.
\begin{quote}
\begin{verbatim}>>> C.cm.im_self # a classmethod is attached to the class
<class '__main__.C'>
>>> C.cm.im_class #the class of C
<type 'type'>\end{verbatim}
\end{quote}

Remark: Python 2.2.0 has a bug in classmethods (fixed in newer versions):
when the first argument of {\_}{\_}get{\_}{\_} is None, then one must specify 
the second argument (otherwise segmentation fault :-()


%___________________________________________________________________________

\hypertarget{properties}{}
\pdfbookmark[1]{Properties}{properties}
\subsection*{Properties}

Properties are a more general kind of attribute descriptors than 
staticmethods and classmethods, since their effect can be customized
trough arbitrary get/set/del functions. Let me give an example:
\begin{quote}
\begin{verbatim}>>> def getp(self): return 'property' # get function
...
>>> p=property(getp) # property object
>>> p
<property object at 0x815855c>\end{verbatim}
\end{quote}

\texttt{p} has a \texttt{{\_}{\_}get{\_}{\_}} special method returning a method-wrapper
object, just as it happens for other descriptors:
\begin{quote}
\begin{verbatim}>>> p.__get__
<method-wrapper object at 0x8158a7c>\end{verbatim}
\end{quote}

The difference is that
\begin{quote}
\begin{verbatim}>>> p.__get__(None,type(p))
<property object at 0x4017016c>
>>> p.__get__('whatever')
'property'
>>> p.__get__('whatever','whatever')
'property'\end{verbatim}
\end{quote}

As for static methods, the \texttt{{\_}{\_}get{\_}{\_}} method wrapper is independent from
its arguments, unless the first one is None: in such a case it returns
the property object, in all other circumstances it returns the result
of \texttt{getp}. This explains the behavior
\begin{quote}
\begin{verbatim}>>> class C(object): p=p
>>> C.p
<property object at 0x815855c>
>>> C().p
'property'\end{verbatim}
\end{quote}

Properties are a dangerous feature, since they change the semantics
of the language. This means that apparently trivial operations can have 
any kind of side effects:
\begin{quote}
\begin{verbatim}>>> def get(self):return 'You gave me the order to destroy your hard disk!!'
>>> class C(object): x=property(get) 
>>> C().x
'You gave me the order to destroy your hard disk!!'\end{verbatim}
\end{quote}

Invoking 'C.x' could very well invoke an external program who is going
to do anything! It is up to the programmer to not abuse properties.
The same is true for user defined attribute descriptors.

There are situations in which they are quite handy, however. For
instance, properties can be used to trace the access data attributes.
This can be especially useful during debugging, or for logging
purposes.

Notice that this approach has the problem that now data attributes cannot 
no more be called trough their class, but only though their instances.
Moreover properties do not work well with \texttt{super} in cooperative
methods.


%___________________________________________________________________________

\hypertarget{user-defined-attribute-descriptors}{}
\pdfbookmark[1]{User-defined attribute descriptors}{user-defined-attribute-descriptors}
\subsection*{User-defined attribute descriptors}

As we have seen, there are plenty of predefined attribute descriptors,
such as staticmethods, classmethods and properties (the built-in
\texttt{super} is also an attribute descriptor which, for sake of
convenience, will be discussed in the next section).
In addition to them, the user can also define customized attribute 
descriptors, simply trough classes with a \texttt{{\_}{\_}get{\_}{\_}} special method.
Let me give an example:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<simpledescr.py>}\\
\mbox{}\\
\mbox{class~ChattyAttr(object):}\\
\mbox{~~~~"""Chatty~descriptor~class;~descriptor~objects~are~intended~to~be~}\\
\mbox{~~~~used~as~attributes~in~other~classes"""}\\
\mbox{~~~~def~{\_}{\_}get{\_}{\_}(self,~obj,~cls=None):}\\
\mbox{~~~~~~~~binding=obj~is~not~None}\\
\mbox{~~~~~~~~if~~binding:}\\
\mbox{~~~~~~~~~~~~return~'You~are~binding~{\%}s~to~{\%}s'~{\%}~(self,obj)}\\
\mbox{~~~~~~~~else:}\\
\mbox{~~~~~~~~~~~~return~'Calling~{\%}s~from~{\%}s'~{\%}~(self,cls)}\\
\mbox{}\\
\mbox{class~C(object):}\\
\mbox{~~~~d=ChattyAttr()}\\
\mbox{}\\
\mbox{c=C()}\\
\mbox{}\\
\mbox{print~c.d~{\#}~<=>~type(c).{\_}{\_}dict{\_}{\_}['d'].{\_}{\_}get{\_}{\_}(c,type(c))}\\
\mbox{print~C.d~{\#}~<=>~C.{\_}{\_}dict{\_}{\_}['d'].{\_}{\_}get{\_}{\_}(None,C)}\\
\mbox{}\\
\mbox{{\#}</simpledescr.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{You~are~binding~<ChattyAttr~object~at~0x401bc1cc>~to~}\\
\mbox{<C~object~at~0x401bc2ec>}\\
\mbox{Calling~<ChattyAttr~object~at~0x401bc1cc>~from~<class~'C'>}
\end{flushleft}\end{ttfamily}
\end{quote}

Invoking a method with the syntax \texttt{C.d} or \texttt{c.d} involves calling
\texttt{{\_}{\_}get{\_}{\_}}. The \texttt{{\_}{\_}get{\_}{\_}} signature is fixed: it is
`` {\_}{\_}get{\_}{\_}={\_}{\_}get{\_}{\_}(self,obj,cls=None)``, since the notation
\texttt{self.descr{\_}attr} automatically passes \texttt{self} and \texttt{self.{\_}{\_}class{\_}{\_}} to 
\texttt{{\_}{\_}get{\_}{\_}}.

Custom descriptors can be used to restrict the access to objects in a
more general way than trough properties. For instance, suppose one
wants to raise an error if a given attribute 'a' is accessed, both
from the class and from the instance: a property cannot help here,
since it works only from the instance. The solution is the following
custom descriptor:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~AccessError(object):}\\
\mbox{~~~~"""Descriptor~raising~an~AttributeError~when~the~attribute~is~}\\
\mbox{~~~~accessed"""~{\#}could~be~done~with~a~property}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,errormessage):}\\
\mbox{~~~~~~~~self.msg=errormessage}\\
\mbox{~~~~def~{\_}{\_}get{\_}{\_}(self,obj,cls=None):}\\
\mbox{~~~~~~~~raise~AttributeError(self.msg)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{>>>~from~oopp~import~AccessError}\\
\mbox{>>>~class~C(object):}\\
\mbox{...~~~~a=AccessError("'a'~cannot~be~accessed")}\\
\mbox{>>>~c=C()}\\
\mbox{>>>~c.a~{\#}error}\\
\mbox{Traceback~(most~recent~call~last):}\\
\mbox{~~File~"<stdin>",~line~1,~in~?}\\
\mbox{~~File~"oopp.py",~line~313,~in~{\_}{\_}get{\_}{\_}}\\
\mbox{~~~~raise~AttributeError(self.msg)}\\
\mbox{AttributeError:~'a'~cannot~be~accessed}\\
\mbox{>>>~C.a~{\#}error}\\
\mbox{Traceback~(most~recent~call~last):}\\
\mbox{~~File~"<stdin>",~line~1,~in~?}\\
\mbox{~~File~"oopp.py",~line~313,~in~{\_}{\_}get{\_}{\_}}\\
\mbox{~~~~raise~AttributeError(self.msg)}\\
\mbox{AttributeError:~'a'~cannot~be~accessed}
\end{flushleft}\end{ttfamily}
\end{quote}

It is always possibile to convert plain attributes (i.e. attributes
without a ''{\_}{\_}get{\_}{\_}`` method) to descriptor objects:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~convert2descriptor(object):}\\
\mbox{~~~~"""To~all~practical~means,~this~class~acts~as~a~function~that,~given~an}\\
\mbox{~~~~object,~adds~to~it~a~{\_}{\_}get{\_}{\_}~method~if~it~is~not~already~there.~The~}\\
\mbox{~~~~added~{\_}{\_}get{\_}{\_}~method~is~trivial~and~simply~returns~the~original~object,~}\\
\mbox{~~~~independently~from~obj~and~cls."""}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(cls,a):}\\
\mbox{~~~~~~~~if~hasattr(a,"{\_}{\_}get{\_}{\_}"):~{\#}~do~nothing}\\
\mbox{~~~~~~~~~~~~return~a~{\#}~a~is~already~a~descriptor}\\
\mbox{~~~~~~~~else:~{\#}~creates~a~trivial~attribute~descriptor}\\
\mbox{~~~~~~~~~~~~cls.a=a}\\
\mbox{~~~~~~~~~~~~return~object.{\_}{\_}new{\_}{\_}(cls)}\\
\mbox{~~~~def~{\_}{\_}get{\_}{\_}(self,obj,cls=None):}\\
\mbox{~~~~~~~~"Returns~self.a~independently~from~obj~and~cls"}\\
\mbox{~~~~~~~~return~self.a}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

This example also shows the magic of \texttt{{\_}{\_}new{\_}{\_}}, that allows to use a
class as a function. The output of 'convert2descriptor(a)' can be both 
an instance of 'convert2descriptor' (in this case 'convert2descriptor' acts as 
a normal class, i.e. as an object factory) or 'a' itself 
(if 'a' is already a descriptor): in this case 'convert2descriptor' acts 
as a function.

For instance, a string is converted to a descriptor
\begin{quote}
\begin{verbatim}>>> from oopp import convert2descriptor
>>> a2=convert2descriptor('a')
>>> a2
<oopp.convert2descriptor object at 0x4017506c>
>>> a2.__get__('whatever')
'a'\end{verbatim}
\end{quote}

whereas a function is untouched:
\begin{quote}
\begin{verbatim}>>> def f(): pass
>>> f2=convert2descriptor(f) # does nothing
>>> f2
<function f at 0x4019110c>\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{data-descriptors}{}
\pdfbookmark[1]{Data descriptors}{data-descriptors}
\subsection*{Data descriptors}

It is also possible to specify a \texttt{{\_}{\_}set{\_}{\_}} method (descriptors
with a \texttt{{\_}{\_}set{\_}{\_}} method are typically data descriptors) with
the signature \texttt{{\_}{\_}set{\_}{\_}(self,obj,value)} as in the following
example:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<datadescr.py>}\\
\mbox{}\\
\mbox{class~DataDescriptor(object):}\\
\mbox{~~~~value=None}\\
\mbox{~~~~def~{\_}{\_}get{\_}{\_}(self,~obj,~cls=None):}\\
\mbox{~~~~~~~~if~obj~is~None:~obj=cls}\\
\mbox{~~~~~~~~print~"Getting",obj,"value~=",self.value}\\
\mbox{~~~~~~~~return~self.value}\\
\mbox{~~~~def~{\_}{\_}set{\_}{\_}(self,~obj,~value):}\\
\mbox{~~~~~~~~self.value=value}\\
\mbox{~~~~~~~~print~"Setting",obj,"value~=",value}\\
\mbox{}\\
\mbox{class~C(object):}\\
\mbox{~~~~d=DataDescriptor()}\\
\mbox{}\\
\mbox{c=C()}\\
\mbox{}\\
\mbox{c.d=1~{\#}calls~C.{\_}{\_}dict{\_}{\_}['d'].{\_}{\_}set{\_}{\_}(c,1)}\\
\mbox{c.d~~~{\#}calls~C.{\_}{\_}dict{\_}{\_}['d'].{\_}{\_}get{\_}{\_}(c,C)}\\
\mbox{C.d~~~{\#}calls~C.{\_}{\_}dict{\_}{\_}['d'].{\_}{\_}get{\_}{\_}(None,C)}\\
\mbox{C.d=0~{\#}does~*not*~call~{\_}{\_}set{\_}{\_}}\\
\mbox{print~"C.d~=",C.d}\\
\mbox{}\\
\mbox{{\#}</datadescr.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

With output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Setting~<C~object~at~0x401bc1ec>~value~=~1}\\
\mbox{Getting~<C~object~at~0x401bc42c>~value~=~1}\\
\mbox{Getting~<class~'C'>~value~=~1~~~~~~}\\
\mbox{C.d~=~0}
\end{flushleft}\end{ttfamily}
\end{quote}

With this knowledge, we may now reconsider the clock example given 
in chapter 3. {\#}NO!??
\begin{quote}
\begin{verbatim}>>> import oopp
>>> class Clock(object): pass
>>> myclock=Clock()
...
>>> myclock.get_time=oopp.get_time # this is a function
>>> Clock.get_time=lambda self : oopp.get_time() # this is a method \end{verbatim}
\end{quote}

In this example, \texttt{myclock.get{\_}time}, which is attached to the \texttt{myclock} 
object, is a function, whereas \texttt{Clock.get{\_}time}, which is attached to 
the \texttt{Clock} class is a method. We may also check this by using the \texttt{type} 
function:
\begin{quote}
\begin{verbatim}>>> type(myclock.get_time)
<type 'function'>\end{verbatim}
\end{quote}

whereas
\begin{quote}
\begin{verbatim}>>> type(Clock.get_time) 
<type 'instance method'>\end{verbatim}
\end{quote}

It must be remarked that user-defined attribute descriptors, just as
properties, allow to arbitrarily change the semantics of the language
and should be used with care.


%___________________________________________________________________________

\hypertarget{the-super-attribute-descriptor}{}
\pdfbookmark[1]{The super attribute descriptor}{the-super-attribute-descriptor}
\subsection*{The \texttt{super} attribute descriptor}

super has also a second form, where it is more used as a descriptor.

\texttt{super} objects are attribute descriptors, too, with a \texttt{{\_}{\_}get{\_}{\_}}
method returning a method-wrapper object:
\begin{quote}
\begin{verbatim}>>> super(C,C()).__get__
<method-wrapper object at 0x8161074>\end{verbatim}
\end{quote}

Here I give some example of acceptable call:
\begin{quote}
\begin{verbatim}>>> super(C,C()).__get__('whatever')
<super: <class 'C'>, <C object>>
>>> super(C,C()).__get__('whatever','whatever')
<super: <class 'C'>, <C object>>\end{verbatim}
\end{quote}

Unfortunately, for the time being 
(i.e. for Python 2.3), the \texttt{super} mechanism  has various limitations. 
To show the issues, let me start by  considering the following base class:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~ExampleBaseClass(PrettyPrinted):}\\
\mbox{~~~~"""Contains~a~regular~method~'m',~a~staticmethod~'s',~a~classmethod~}\\
\mbox{~~~~'c',~a~property~'p'~and~a~data~attribute~'d'."""}\\
\mbox{~~~~m=lambda~self:~'regular~method~of~{\%}s'~{\%}~self}\\
\mbox{~~~~s=staticmethod(lambda~:~'staticmethod')}\\
\mbox{~~~~c=classmethod(lambda~cls:~'classmethod~of~{\%}s'~{\%}~cls)}\\
\mbox{~~~~p=property(lambda~self:~'property~of~{\%}s'~{\%}~self)}\\
\mbox{~~~~a=AccessError('Expected~error')}\\
\mbox{~~~~d='data'}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now, let me derive a new class C from ExampleBaseClass:
\begin{quote}
\begin{verbatim}>>> from oopp import ExampleBaseClass
>>> class C(ExampleBaseClass): pass
>>> c=C()\end{verbatim}
\end{quote}

Ideally, we would like to retrieve the methods and attributes of 
ExampleBaseClass from C, by using the \texttt{super} mechanism.
\newcounter{listcnt24}
\begin{list}{\arabic{listcnt24}.}
{
\usecounter{listcnt24}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
We see that \texttt{super} works without problems for regular methods, 
staticmethods and classmethods:

\end{list}
\begin{quote}
\begin{verbatim}>>> super(C,c).m()
'regular method of <C>'
>>> super(C,c).s()
'staticmethod'
>>> super(C,c).c()
"classmethod of <class '__main__.C'>"\end{verbatim}
\end{quote}

It also works for user defined attribute descriptors:
\begin{quote}
\begin{verbatim}>>> super(C,c).a # access error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "oopp.py", line 340, in __get__
    raise AttributeError(self.msg)
AttributeError: Expected error\end{verbatim}
\end{quote}

and for properties (only for Python 2.3+):
\begin{quote}
\begin{verbatim}>>> ExampleBaseClass.p
<property object at 0x81b30fc>\end{verbatim}
\end{quote}

In Python 2.2 one would get an error, instead
\begin{quote}
\begin{verbatim}>>> super(C,c).p #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'super' object has no attribute 'p'\end{verbatim}
\end{quote}

3. Moreover, certain attributes of the superclass, such as its
\texttt{{\_}{\_}name{\_}{\_}}, cannot be retrieved:
\begin{quote}
\begin{verbatim}>>> ExampleBaseClass.__name__
'ExampleBaseClass'
>>> super(C,c).__name__ #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'super' object has no attribute '__name__'\end{verbatim}
\end{quote}
\newcounter{listcnt25}
\begin{list}{\arabic{listcnt25}.}
{
\usecounter{listcnt25}
\addtocounter{listcnt25}{3}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
There is no direct way to retrieve the methods of the super-superclass 
(i.e. the grandmother class, if you wish) or in general the furthest 
ancestors, since \texttt{super} does not chain.

\item {} 
Finally, there are some subtle issues with the \texttt{super(cls)} syntax:

\end{list}
\begin{quote}
\begin{verbatim}>>> super(C).m #(2) error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'super' object has no attribute 'm'\end{verbatim}
\end{quote}

means \texttt{super(C).{\_}{\_}get{\_}{\_}(None,C)}, but only 
\texttt{super(C).{\_}{\_}get{\_}{\_}(c,C).m==super(C,c)} works.
\begin{quote}
\begin{quote}

On the other hand,
\end{quote}
\begin{verbatim}>>> super(C).__init__  #(1) 
<built-in method __init__ of type object at 0x80e6fc0>
>>> super(C).__new__  #(1) 
<built-in method __init__ of type object at 0x80e6fc0>\end{verbatim}
\begin{quote}

seems to work, whereas in reality does not. The reason is that since 
\texttt{super} objects are instances 
of \texttt{object}, they inherit object's methods, and in particular 
\texttt{{\_}{\_}init{\_}{\_}} ; therefore the \texttt{{\_}{\_}init{\_}{\_}} method in (1) is \emph{not} 
the \texttt{ExampleBaseClass.{\_}{\_}init{\_}{\_}} method. The point is that \texttt{super} 
objects are attribute descriptors and not references to the superclass.
\end{quote}
\end{quote}

Probably, in future versions of Python the \texttt{super} mechanism will be 
improved. However, for the time being, one must provide a workaround for 
dealing with these issues. This will be discussed in the next chapter.


%___________________________________________________________________________

\hypertarget{method-wrappers}{}
\pdfbookmark[1]{Method wrappers}{method-wrappers}
\subsection*{Method wrappers}

One of the most typical applications of attribute descriptors is their
usage as \emph{method wrappers}.

Suppose, for instance, one wants to add tracing capabilities to 
the methods of a class for debugging purposes. The problem
can be solved with a custom descriptor class:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~inspect}\\
\mbox{}\\
\mbox{class~wrappedmethod(Customizable):}\\
\mbox{~~~~"""Customizable~method~factory~intended~for~derivation.}\\
\mbox{~~~~The~wrapper~method~is~overridden~in~the~children."""}\\
\mbox{}\\
\mbox{~~~~logfile=sys.stdout~{\#}~default}\\
\mbox{~~~~namespace=''~{\#}~default}\\
\mbox{}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(cls,meth):~{\#}~meth~is~a~descriptor}\\
\mbox{~~~~~~~~if~isinstance(meth,FunctionType):}\\
\mbox{~~~~~~~~~~~~kind=0~{\#}~regular~method}\\
\mbox{~~~~~~~~~~~~func=meth}\\
\mbox{~~~~~~~~elif~isinstance(meth,staticmethod):}\\
\mbox{~~~~~~~~~~~~kind=1~{\#}~static~method}\\
\mbox{~~~~~~~~~~~~func=meth.{\_}{\_}get{\_}{\_}('whatever')}\\
\mbox{~~~~~~~~elif~isinstance(meth,classmethod):}\\
\mbox{~~~~~~~~~~~~kind=2~{\#}~class~method}\\
\mbox{~~~~~~~~~~~~func=meth.{\_}{\_}get{\_}{\_}('whatever','whatever').im{\_}func~}\\
\mbox{~~~~~~~~elif~isinstance(meth,wrappedmethod):~{\#}~already~wrapped}\\
\mbox{~~~~~~~~~~~~return~meth~{\#}~do~nothing}\\
\mbox{~~~~~~~~elif~inspect.ismethoddescriptor(meth):}\\
\mbox{~~~~~~~~~~~~kind=0;~func=meth~{\#}~for~many~builtin~methods~}\\
\mbox{~~~~~~~~else:}\\
\mbox{~~~~~~~~~~~~return~meth~{\#}~do~nothing}\\
\mbox{~~~~~~~~self=super(wrappedmethod,cls).{\_}{\_}new{\_}{\_}(cls)}\\
\mbox{~~~~~~~~self.kind=kind;~self.func=func~{\#}~pre-initialize}\\
\mbox{~~~~~~~~return~self}\\
\mbox{}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,meth):~{\#}~meth~not~used}\\
\mbox{~~~~~~~~self.logfile=self.logfile~{\#}~default~values}\\
\mbox{~~~~~~~~self.namespace=self.namespace~{\#}~copy~the~current}\\
\mbox{}\\
\mbox{~~~~def~{\_}{\_}get{\_}{\_}(self,obj,cls):~{\#}~closure~}\\
\mbox{~~~~~~~~def~{\_}(*args,**kw):}\\
\mbox{~~~~~~~~~~~~if~obj~is~None:~o=()~{\#}~unbound~method~call}\\
\mbox{~~~~~~~~~~~~else:~o=(obj,)~{\#}~bound~method~call}\\
\mbox{~~~~~~~~~~~~allargs=[o,(),(cls,)][self.kind]+args~}\\
\mbox{~~~~~~~~~~~~return~self.wrapper()(*allargs,**kw)}\\
\mbox{~~~~~~~~return~{\_}~{\#}~the~wrapped~function}\\
\mbox{~~~~~~~~{\#}~allargs~is~the~only~nontrivial~line~in~{\_};~it~adds}\\
\mbox{~~~~~~~~{\#}~0~-~obj~if~meth~is~a~regular~method}\\
\mbox{~~~~~~~~{\#}~1~-~nothing~if~meth~is~a~static~method}\\
\mbox{~~~~~~~~{\#}~2~-~cls~if~meth~is~a~class~method}\\
\mbox{}\\
\mbox{~~~~def~wrapper(self):~return~self.func~{\#}~do~nothing,~to~be~overridden}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

This class is intended for derivation: the wrapper method has to be overridden
in the children in order to introduce the wanted feature. If I want to 
implement the capability of tracing methods, I can reuse the \texttt{with{\_}tracer}
closure introduced in chapter 2:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~tracedmethod(wrappedmethod):}\\
\mbox{~~~~def~wrapper(self):}\\
\mbox{~~~~~~~~return~with{\_}tracer(self.func,self.namespace,self.logfile)}\\
\mbox{~~~~~~~~}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Nothing prevents me from introducing timing features by reusing the 
\texttt{with{\_}timer} closure:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~timedmethod(wrappedmethod):}\\
\mbox{~~~~iterations=1~{\#}~additional~default~parameter}\\
\mbox{}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,meth):}\\
\mbox{~~~~~~~~super(timedmethod,self).{\_}{\_}init{\_}{\_}(self,meth)}\\
\mbox{~~~~~~~~self.iterations=self.iterations~{\#}~copy}\\
\mbox{}\\
\mbox{~~~~def~wrapper(self):}\\
\mbox{~~~~~~~~return~with{\_}timer(self.func,self.namespace,}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~self.iterations,self.logfile)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of usage:

The dictionary of wrapped functions is then built from an utility function
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~wrap(obj,wrapped,condition=lambda~k,v:~True,~err=None):}\\
\mbox{~~~~"Retrieves~obj's~dictionary~and~wraps~it"}\\
\mbox{~~~~if~isinstance(obj,dict):~{\#}~obj~is~a~dictionary~}\\
\mbox{~~~~~~~~dic=obj}\\
\mbox{~~~~else:~}\\
\mbox{~~~~~~~~dic=getattr(obj,'{\_}{\_}dict{\_}{\_}',{\{}{\}}).copy()~{\#}~avoids~dictproxy~objects}\\
\mbox{~~~~~~~~if~not~dic:~dic=attributes(obj)~{\#}~for~simple~objects}\\
\mbox{~~~~wrapped.namespace=getattr(obj,'{\_}{\_}name{\_}{\_}','')}\\
\mbox{~~~~for~name,attr~in~dic.iteritems():~{\#}~modify~dic}\\
\mbox{~~~~~~~~if~condition(name,attr):~dic[name]=wrapped(attr)}\\
\mbox{~~~~if~not~isinstance(obj,dict):~{\#}~modify~obj}\\
\mbox{~~~~~~~~customize(obj,err,**dic)~}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<tracingmethods.py>}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{}\\
\mbox{class~C(object):~}\\
\mbox{~~~~"Class~with~traced~methods"}\\
\mbox{}\\
\mbox{~~~~def~f(self):~return~self~}\\
\mbox{~~~~f=tracedmethod(f)}\\
\mbox{}\\
\mbox{~~~~g=staticmethod(lambda:None)}\\
\mbox{~~~~g=tracedmethod(g)}\\
\mbox{}\\
\mbox{~~~~h=classmethod(do{\_}nothing)}\\
\mbox{~~~~h=tracedmethod(h)}\\
\mbox{}\\
\mbox{c=C()}\\
\mbox{}\\
\mbox{{\#}unbound~calls}\\
\mbox{C.f(c)~}\\
\mbox{C.g()}\\
\mbox{C.h()}\\
\mbox{}\\
\mbox{{\#}bound~calls}\\
\mbox{c.f()~~}\\
\mbox{c.g()}\\
\mbox{c.h()}\\
\mbox{}\\
\mbox{{\#}</tracingmethods.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{[C]~Calling~'f'~with~arguments}\\
\mbox{(<C~object~at~0x402042cc>,){\{}{\}}~...}\\
\mbox{->~'C.f'~called~with~result:~<C~object~at~0x402042cc>}\\
\mbox{}\\
\mbox{[C]~Calling~'<lambda>'~with~arguments}\\
\mbox{(){\{}{\}}~...}\\
\mbox{->~'C.<lambda>'~called~with~result:~None}\\
\mbox{}\\
\mbox{[C]~Calling~'do{\_}nothing'~with~arguments}\\
\mbox{(<class~'C'>,){\{}{\}}~...}\\
\mbox{->~'C.do{\_}nothing'~called~with~result:~None}\\
\mbox{}\\
\mbox{[C]~Calling~'f'~with~arguments}\\
\mbox{(<C~object~at~0x402042cc>,){\{}{\}}~...}\\
\mbox{->~'C.f'~called~with~result:~<C~object~at~0x402042cc>}\\
\mbox{}\\
\mbox{[C]~Calling~'<lambda>'~with~arguments}\\
\mbox{(){\{}{\}}~...}\\
\mbox{->~'C.<lambda>'~called~with~result:~None}\\
\mbox{}\\
\mbox{[C]~Calling~'do{\_}nothing'~with~arguments}\\
\mbox{(<class~'C'>,){\{}{\}}~...}\\
\mbox{->~'C.do{\_}nothing'~called~with~result:~None}
\end{flushleft}\end{ttfamily}
\end{quote}

The approach in 'tracingmethods.py' works, but it is far from
being elegant, since I had to explicitly wrap each method in the
class by hand.

Both problems can be avoided.
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> wrap(Clock,tracedmethod)
>>> Clock.get_time()
[Clock] Calling 'get_time' with arguments
(){} ...
-> 'Clock.get_time' called with result: 21:56:52
'21:56:52'\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{the-subtleties-of-multiple-inheritance}{}
\pdfbookmark[0]{THE SUBTLETIES OF MULTIPLE INHERITANCE}{the-subtleties-of-multiple-inheritance}
\section*{THE SUBTLETIES OF MULTIPLE INHERITANCE}

In chapter 4 we introduced the concept of multiple inheritance and discussed
its simplest applications in absence of name collisions. When with methods
with different names are derived from different classes multiple inheritance
is pretty trivial. However, all kind of subtilites comes in presence of name 
clashing, i.e. when we multiply inherits different methods defined in different
classes but with the \emph{same} name.
In order to understand what happens in this situation, it is essential to 
understand the concept of Method Resolution Order (MRO). For reader's
convenience, I collect in this chapter some of the information
reported in \href{http://www.python.org/2.3/mro.html}{http://www.python.org/2.3/mro.html}.


%___________________________________________________________________________

\hypertarget{a-little-bit-of-history-why-python-2-3-has-changed-the-mro}{}
\pdfbookmark[1]{A little bit of history: why Python 2.3 has changed the MRO}{a-little-bit-of-history-why-python-2-3-has-changed-the-mro}
\subsection*{A little bit of history: why Python 2.3 has changed the MRO}

Everything started with a post by Samuele Pedroni to the Python
development mailing list [\hyperlink{id36}{18}].  In his post, Samuele showed that the
Python 2.2 method resolution order is not monotonic and he proposed to
replace it with the C3 method resolution order.  Guido agreed with his
arguments and therefore now Python 2.3 uses C3.  The C3 method itself
has nothing to do with Python, since it was invented by people working
on Dylan and it is described in a paper intended for lispers [\hyperlink{id40}{19}].  The
present paper gives a (hopefully) readable discussion of the C3
algorithm for Pythonistas who want to understand the reasons for the
change.

First of all, let me point out that what I am going to say only applies
to the \emph{new style classes} introduced in Python 2.2:  \emph{classic classes}
maintain their old method resolution order, depth first and then left to
right.  Therefore, there is no breaking of old code for classic classes;
and even if in principle there could be breaking of code for Python 2.2
new style classes, in practice the cases in which the C3 resolution
order differs from the Python 2.2 method resolution order are so rare
that no real breaking of code is expected.  Therefore: don't be scared!

Moreover, unless you make strong use of multiple inheritance and you
have non-trivial hierarchies, you don't need to understand the C3
algorithm, and you can easily skip this paper.  On the other hand, if
you really want to know how multiple inheritance works, then this paper
is for you.  The good news is that things are not as complicated as you
might expect.

Let me begin with some basic definitions.
\newcounter{listcnt26}
\begin{list}{\arabic{listcnt26})}
{
\usecounter{listcnt26}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
Given a class C in a complicated multiple inheritance hierarchy, it
is a non-trivial task to specify the order in which methods are
overridden, i.e. to specify the order of the ancestors of C.

\item {} 
The list of the ancestors of a class C, including the class itself,
ordered from the nearest ancestor to the furthest, is called the
class precedence list or the \emph{linearization} of C.

\item {} 
The \emph{Method Resolution Order} (MRO) is the set of rules that
construct the linearization.  In the Python literature, the idiom
''the MRO of C`` is also used as a synonymous for the linearization of
the class C.

\item {} 
For instance, in the case of single inheritance hierarchy, if C is a
subclass of C1, and C1 is a subclass of C2, then the linearization of
C is simply the list [C, C1 , C2].  However, with multiple
inheritance hierarchies, it is more difficult to construct a
linearization that respects \emph{local precedence ordering} and
\emph{monotonicity}.

\item {} 
I will discuss the local precedence ordering later, but I can give
the definition of monotonicity here.  A MRO is monotonic when the
following is true:  \emph{if C1 precedes C2 in the linearization of C,
then C1 precedes C2 in the linearization of any subclass of C}.
Otherwise, the innocuous operation of deriving a new class could
change the resolution order of methods, potentially introducing very
subtle bugs.  Examples where this happens will be shown later.

\item {} 
Not all classes admit a linearization.  There are cases, in
complicated hierarchies, where it is not possible to derive a class
such that its linearization respects all the desired properties.

\end{list}

Here I give an example of this situation. Consider the hierarchy
\begin{quote}
\begin{verbatim}>>> O = object
>>> class X(O): pass
>>> class Y(O): pass
>>> class A(X,Y): pass
>>> class B(Y,X): pass\end{verbatim}
\end{quote}

which can be represented with the following inheritance graph, where I
have denoted with O the \texttt{object} class, which is the beginning of any
hierarchy for new style classes:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~-----------}\\
\mbox{|~~~~~~~~~~~|}\\
\mbox{|~~~~O~~~~~~|}\\
\mbox{|~~/~~~{\textbackslash}~~~~|}\\
\mbox{~-~X~~~~Y~~/}\\
\mbox{~~~|~~/~|~/}\\
\mbox{~~~|~/~~|/}\\
\mbox{~~~A~~~~B}\\
\mbox{~~~{\textbackslash}~~~/}\\
\mbox{~~~~~?}
\end{flushleft}\end{ttfamily}
\end{quote}

In this case, it is not possible to derive a new class C from A and B,
since X precedes Y in A, but Y precedes X in B, therefore the method
resolution order would be ambiguous in C.

Python 2.3 raises an exception in this situation (TypeError:  MRO
conflict among bases Y, X) forbidding the naive programmer from creating
ambiguous hierarchies.  Python 2.2 instead does not raise an exception,
but chooses an \emph{ad hoc} ordering (CABXYO in this case).


%___________________________________________________________________________

\hypertarget{the-c3-method-resolution-order}{}
\pdfbookmark[1]{The C3 Method Resolution Order}{the-c3-method-resolution-order}
\subsection*{The C3 Method Resolution Order}

Let me introduce a few simple notations which will be useful for the
following discussion.  I will use the shortcut notation
\begin{quote}

C1 C2 ... CN
\end{quote}

to indicate the list of classes [C1, C2, ... , CN].

The \emph{head} of the list is its first element:
\begin{quote}

head = C1
\end{quote}

whereas the \emph{tail} is the rest of the list:
\begin{quote}

tail = C2 ... CN.
\end{quote}

I shall also use the notation
\begin{quote}

C + (C1 C2 ... CN) = C C1 C2 ... CN
\end{quote}

to denote the sum of the lists [C] + [C1, C2, ... ,CN].

Now I can explain how the MRO works in Python 2.3.

Consider a class C in a multiple inheritance hierarchy, with C
inheriting from the base classes B1, B2, ...  , BN.  We want to compute
the linearization L[C] of the class C. In order to do that, we need the
concept of \emph{merging} lists, since the rule says that
\begin{quote}

\emph{the linearization of C is the sum of C plus the merge of a) the
linearizations of the parents and b) the list of the parents.}
\end{quote}

In symbolic notation:
\begin{quote}

L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN)
\end{quote}

How is the merge computed? The rule is the following:
\begin{quote}

\emph{take the head of the first list, i.e L[B1][0]; if this head is not in
the tail of any of the other lists, then add it to the linearization
of C and remove it from the lists in the merge, otherwise look at the
head of the next list and take it, if it is a good head.  Then repeat
the operation until all the class are removed or it is impossible to
find good heads.  In this case, it is impossible to construct the
merge, Python 2.3 will refuse to create the class C and will raise an
exception.}
\end{quote}

This prescription ensures that the merge operation \emph{preserves} the
ordering, if the ordering can be preserved.  On the other hand, if the
order cannot be preserved (as in the example of serious order
disagreement discussed above) then the merge cannot be computed.

The computation of the merge is trivial if:
\newcounter{listcnt27}
\begin{list}{\arabic{listcnt27}.}
{
\usecounter{listcnt27}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
C is the \texttt{object} class, which has no parents; in this case its
linearization coincides with itself,
\begin{quote}

L[object] = object.
\end{quote}

\item {} 
C has only one parent (single inheritance); in this case
\begin{quote}

L[C(B)] = C + merge(L[B],B) = C + L[B]
\end{quote}

\end{list}

However, in the case of multiple inheritance things are more cumbersome
and I don't expect you can understand the rule without a couple of
examples ;-)


%___________________________________________________________________________

\hypertarget{examples}{}
\pdfbookmark[1]{Examples}{examples}
\subsection*{Examples}

First example. Consider the following hierarchy:
\begin{quote}
\begin{verbatim}>>> O = object
>>> class F(O): pass
>>> class E(O): pass
>>> class D(O): pass
>>> class C(D,F): pass
>>> class B(D,E): pass
>>> class A(B,C): pass\end{verbatim}
\end{quote}

In this case the inheritance graph can be drawn as
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~6}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~---}\\
\mbox{Level~3~~~~~~~~~~~~~~~~~|~O~|~~~~~~~~~~~~~~~~~~(more~general)}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~/~~---~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~/~~~~|~~~~{\textbackslash}~~~~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~/~~~~~|~~~~~{\textbackslash}~~~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~/~~~~~~|~~~~~~{\textbackslash}~~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~---~~~~---~~~~---~~~~~~~~~~~~~~~~~~~|}\\
\mbox{Level~2~~~~~~~~3~|~D~|~4|~E~|~~|~F~|~5~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~---~~~~---~~~~---~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~{\textbackslash}~~{\textbackslash}~{\_}~/~~~~~~~|~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~/~{\textbackslash}~{\_}~~~~|~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~/~~~~~~{\textbackslash}~~|~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~---~~~~~~---~~~~~~~~~~~~~~~~~~~~|}\\
\mbox{Level~1~~~~~~~~~~~~1~|~B~|~~~~|~C~|~2~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~---~~~~~~---~~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~/~~~~~~~~~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~/~~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~---}\\
\mbox{Level~0~~~~~~~~~~~~~~~~~0~|~A~|~~~~~~~~~~~~~~~~(more~specialized)}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~---}
\end{flushleft}\end{ttfamily}
\end{quote}

The linearizations of O,D,E and F are trivial:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[O]~=~O}\\
\mbox{L[D]~=~D~O}\\
\mbox{L[E]~=~E~O}\\
\mbox{L[F]~=~F~O}
\end{flushleft}\end{ttfamily}
\end{quote}

The linearization of B can be computed as
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[B]~=~B~+~merge(DO,~EO,~DE)}
\end{flushleft}\end{ttfamily}
\end{quote}

We see that D is a good head, therefore we take it and we are reduced to
compute merge(O,EO,E).  Now O is not a good head, since it is in the
tail of the sequence EO.  In this case the rule says that we have to
skip to the next sequence.  Then we see that E is a good head; we take
it and we are reduced to compute merge(O,O) which gives O. Therefore
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[B]~=~~B~D~E~O}
\end{flushleft}\end{ttfamily}
\end{quote}

Using the same procedure one finds:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[C]~=~C~+~merge(DO,FO,DF)}\\
\mbox{~~~~~=~C~+~D~+~merge(O,FO,F)}\\
\mbox{~~~~~=~C~+~D~+~F~+~merge(O,O)}\\
\mbox{~~~~~=~C~D~F~O}
\end{flushleft}\end{ttfamily}
\end{quote}

Now we can compute:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[A]~=~A~+~merge(BDEO,CDFO,BC)}\\
\mbox{~~~~~=~A~+~B~+~merge(DEO,CDFO,C)}\\
\mbox{~~~~~=~A~+~B~+~C~+~merge(DEO,DFO)}\\
\mbox{~~~~~=~A~+~B~+~C~+~D~+~merge(EO,FO)}\\
\mbox{~~~~~=~A~+~B~+~C~+~D~+~E~+~merge(O,FO)}\\
\mbox{~~~~~=~A~+~B~+~C~+~D~+~E~+~F~+~merge(O,O)}\\
\mbox{~~~~~=~A~B~C~D~E~F~O}
\end{flushleft}\end{ttfamily}
\end{quote}

In this example, the linearization is ordered in a pretty nice way
according to the inheritance level, in the sense that lower levels (i.e.
more specialized classes) have higher precedence (see the inheritance
graph).  However, this is not the general case.

I leave as an exercise for the reader to compute the linearization for
my second example:
\begin{quote}
\begin{verbatim}>>> O = object
>>> class F(O): pass
>>> class E(O): pass
>>> class D(O): pass
>>> class C(D,F): pass
>>> class B(E,D): pass
>>> class A(B,C): pass\end{verbatim}
\end{quote}

The only difference with the previous example is the change B(D,E) --{\textgreater}
B(E,D); however even such a little modification completely changes the
ordering of the hierarchy
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~~6}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~---}\\
\mbox{Level~3~~~~~~~~~~~~~~~~~~|~O~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~/~~---~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~/~~~~|~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~/~~~~~|~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~~/~~~~~~|~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~---~~~~~---~~~~---}\\
\mbox{Level~2~~~~~~~~2~|~E~|~4~|~D~|~~|~F~|~5}\\
\mbox{~~~~~~~~~~~~~~~~~~---~~~~~---~~~~---}\\
\mbox{~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~/~{\textbackslash}~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~/~~~{\textbackslash}~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~/~~~~~{\textbackslash}~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~---~~~~~---}\\
\mbox{Level~1~~~~~~~~~~~~1~|~B~|~~~|~C~|~3}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~---~~~~~---}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~---}\\
\mbox{Level~0~~~~~~~~~~~~~~~~0~|~A~|}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~---}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that the class E, which is in the second level of the hierarchy,
precedes the class C, which is in the first level of the hierarchy, i.e.
E is more specialized than C, even if it is in a higher level.

A lazy programmer can obtain the MRO directly from Python 2.2, since in
this case it coincides with the Python 2.3 linearization.  It is enough
to invoke the .mro() method of class A:
\begin{quote}
\begin{verbatim}>>> A.mro()
(<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>,
<class '__main__.C'>, <class '__main__.D'>, <class '__main__.F'>,
<type 'object'>)\end{verbatim}
\end{quote}

Finally, let me consider the example discussed in the first section,
involving a serious order disagreement.  In this case, it is
straightforward to compute the linearizations of O, X, Y, A and B:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[O]~=~0}\\
\mbox{L[X]~=~X~O}\\
\mbox{L[Y]~=~Y~O}\\
\mbox{L[A]~=~A~X~Y~O}\\
\mbox{L[B]~=~B~Y~X~O}
\end{flushleft}\end{ttfamily}
\end{quote}

However, it is impossible to compute the linearization for a class C
that inherits from A and B:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[C]~=~C~+~merge(AXYO,~BYXO,~AB)}\\
\mbox{~~~~~=~C~+~A~+~merge(XYO,~BYXO,~B)}\\
\mbox{~~~~~=~C~+~A~+~B~+~merge(XYO,~YXO)}
\end{flushleft}\end{ttfamily}
\end{quote}

At this point we cannot merge the lists XYO and YXO, since X is in the
tail of YXO whereas Y is in the tail of XYO:  therefore there are no
good heads and the C3 algorithm stops.  Python 2.3 raises an error and
refuses to create the class C.


%___________________________________________________________________________

\hypertarget{bad-method-resolution-orders}{}
\pdfbookmark[1]{Bad Method Resolution Orders}{bad-method-resolution-orders}
\subsection*{Bad Method Resolution Orders}

A MRO is \emph{bad} when it breaks such fundamental properties as local
precedence ordering and monotonicity.  In this section, I will show
that both the MRO for classic classes and the MRO for new style classes
in Python 2.2 are bad.

It is easier to start with the local precedence ordering.  Consider the
following example:
\begin{quote}
\begin{verbatim}>>> F=type('Food',(),{'remember2buy':'spam'})
>>> E=type('Eggs',(F,),{'remember2buy':'eggs'})
>>> G=type('GoodFood',(F,E),{}) #under Python 2.3 this is an error\end{verbatim}
\end{quote}

with inheritance diagram
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~~~~O}\\
\mbox{~~~~~~~~~~~~~|}\\
\mbox{(buy~spam)~~~F}\\
\mbox{~~~~~~~~~~~~~|~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~|~E~~~(buy~eggs)}\\
\mbox{~~~~~~~~~~~~~|~/}\\
\mbox{~~~~~~~~~~~~~G}\\
\mbox{}\\
\mbox{~~~~~~(buy~eggs~or~spam~?)}
\end{flushleft}\end{ttfamily}
\end{quote}

We see that class G inherits from F and E, with F \emph{before} E:  therefore
we would expect the attribute \emph{G.remember2buy} to be inherited by
\emph{F.rembermer2buy} and not by \emph{E.remember2buy}:  nevertheless Python 2.2
gives
\begin{quote}
\begin{verbatim}>>> G.remember2buy #under Python 2.3 this is an error
'eggs'\end{verbatim}
\end{quote}

This is a breaking of local precedence ordering since the order in the
local precedence list, i.e. the list of the parents of G, is not
preserved in the Python 2.2 linearization of G:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[G,P22]=~G~E~F~object~~~{\#}~F~*follows*~E}
\end{flushleft}\end{ttfamily}
\end{quote}

One could argue that the reason why F follows E in the Python 2.2
linearization is that F is less specialized than E, since F is the
superclass of E; nevertheless the breaking of local precedence ordering
is quite non-intuitive and error prone.  This is particularly true since
it is a different from old style classes:
\begin{quote}
\begin{verbatim}>>> class F: remember2buy='spam'
>>> class E(F): remember2buy='eggs'
>>> class G(F,E): pass
>>> G.remember2buy
'spam'\end{verbatim}
\end{quote}

In this case the MRO is GFEF and the local precedence ordering is
preserved.

As a general rule, hierarchies such as the previous one should be
avoided, since it is unclear if F should override E or viceversa.
Python 2.3 solves the ambiguity by raising an exception in the creation
of class G, effectively stopping the programmer from generating
ambiguous hierarchies.  The reason for that is that the C3 algorithm
fails when the merge
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{merge(FO,EFO,FE)}
\end{flushleft}\end{ttfamily}
\end{quote}

cannot be computed, because F is in the tail of EFO and E is in the tail
of FE.

The real solution is to design a non-ambiguous hierarchy, i.e. to derive
G from E and F (the more specific first) and not from F and E; in this
case the MRO is GEF without any doubt.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~~O}\\
\mbox{~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~~F~(spam)}\\
\mbox{~~~~~~~~~/~|}\\
\mbox{(eggs)~~~E~|}\\
\mbox{~~~~~~~~~{\textbackslash}~|}\\
\mbox{~~~~~~~~~~~G}\\
\mbox{~~~~~~~~~~~~~(eggs,~no~doubt)}
\end{flushleft}\end{ttfamily}
\end{quote}

Python 2.3 forces the programmer to write good hierarchies (or, at
least, less error-prone ones).

On a related note, let me point out that the Python 2.3 algorithm is
smart enough to recognize obvious mistakes, as the duplication of
classes in the list of parents:
\begin{quote}
\begin{verbatim}>>> class A(object): pass
>>> class C(A,A): pass # error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: duplicate base class A\end{verbatim}
\end{quote}

Python 2.2 (both for classic classes and new style classes) in this
situation, would not raise any exception.

Finally, I would like to point out two lessons we have learned from this
example:
\newcounter{listcnt28}
\begin{list}{\arabic{listcnt28}.}
{
\usecounter{listcnt28}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
despite the name, the MRO determines the resolution order of
attributes, not only of methods;

\item {} 
the default food for Pythonistas is spam !  (but you already knew
that ;-)

\end{list}

Having discussed the issue of local precedence ordering, let me now
consider the issue of monotonicity.  My goal is to show that neither the
MRO for classic classes nor that for Python 2.2 new style classes is
monotonic.

To prove that the MRO for classic classes is non-monotonic is rather
trivial, it is enough to look at the diamond diagram:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~C}\\
\mbox{~~/~{\textbackslash}}\\
\mbox{~/~~~{\textbackslash}}\\
\mbox{A~~~~~B}\\
\mbox{~{\textbackslash}~~~/}\\
\mbox{~~{\textbackslash}~/}\\
\mbox{~~~D}
\end{flushleft}\end{ttfamily}
\end{quote}

One easily discerns the inconsistency:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[B,P21]~=~B~C~~~~~~~~{\#}~B~precedes~C~:~B's~methods~win}\\
\mbox{L[D,P21]~=~D~A~C~B~C~~{\#}~B~follows~C~~:~C's~methods~win!}
\end{flushleft}\end{ttfamily}
\end{quote}

On the other hand, there are no problems with the Python 2.2 and 2.3
MROs, they give both
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[D]~=~D~A~B~C}
\end{flushleft}\end{ttfamily}
\end{quote}

Guido points out in his essay [\hyperlink{id41}{20}] that the classic MRO is not so bad in
practice, since one can typically avoids diamonds for classic classes.
But all new style classes inherit from object, therefore diamonds are
unavoidable and inconsistencies shows up in every multiple inheritance
graph.

The MRO of Python 2.2 makes breaking monotonicity difficult, but not
impossible.  The following example, originally provided by Samuele
Pedroni, shows that the MRO of Python 2.2 is non-monotonic:
\begin{quote}
\begin{verbatim}>>> class A(object): pass
>>> class B(object): pass
>>> class C(object): pass
>>> class D(object): pass
>>> class E(object): pass
>>> class K1(A,B,C): pass
>>> class K2(D,B,E): pass
>>> class K3(D,A):   pass
>>> class Z(K1,K2,K3): pass\end{verbatim}
\end{quote}

Here are the linearizations according to the C3 MRO (the reader should
verify these linearizations as an exercise and draw the inheritance
diagram ;-)
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[A]~=~A~O}\\
\mbox{L[B]~=~B~O}\\
\mbox{L[C]~=~C~O}\\
\mbox{L[D]~=~D~O}\\
\mbox{L[E]~=~E~O}\\
\mbox{L[K1]=~K1~A~B~C~O}\\
\mbox{L[K2]=~K2~D~B~E~O}\\
\mbox{L[K3]=~K3~D~A~O}\\
\mbox{L[Z]~=~Z~K1~K2~K3~D~A~B~C~E~O}
\end{flushleft}\end{ttfamily}
\end{quote}

Python 2.2 gives exactly the same linearizations for A, B, C, D, E, K1,
K2 and K3, but a different linearization for Z:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{L[Z,P22]~=~Z~K1~K3~A~K2~D~B~C~E~O}
\end{flushleft}\end{ttfamily}
\end{quote}

It is clear that this linearization is \emph{wrong}, since A comes before D
whereas in the linearization of K3 A comes \emph{after} D. In other words, in
K3 methods derived by D override methods derived by A, but in Z, which
still is a subclass of K3, methods derived by A override methods derived
by D!  This is a violation of monotonicity.  Moreover, the Python 2.2
linearization of Z is also inconsistent with local precedence ordering,
since the local precedence list of the class Z is [K1, K2, K3] (K2
precedes K3), whereas in the linearization of Z K2 \emph{follows} K3.  These
problems explain why the 2.2 rule has been dismissed in favor of the C3
rule.
\begin{figure}[b]\hypertarget{id40}[19]
The thread on python-dev started by Samuele Pedroni:
\href{http://mail.python.org/pipermail/python-dev/2002-October/029035.html}{http://mail.python.org/pipermail/python-dev/2002-October/029035.html}
\end{figure}
\begin{figure}[b]\hypertarget{id41}[20]
The paper \emph{A Monotonic Superclass Linearization for Dylan}:
\href{http://www.webcom.com/haahr/dylan/linearization-oopsla96.html}{http://www.webcom.com/haahr/dylan/linearization-oopsla96.html}
\end{figure}
\begin{figure}[b]\hypertarget{id42}[21]
Guido van Rossum's essay, \emph{Unifying types and classes in Python 2.2}:
\href{http://www.python.org/2.2.2/descrintro.html}{http://www.python.org/2.2.2/descrintro.html}
\end{figure}
\begin{figure}[b]\hypertarget{id43}[22]
The (in)famous book on metaclasses, \emph{Putting Metaclasses to Work}:
Ira R. Forman, Scott Danforth, Addison-Wesley 1999 (out of print,
but probably still available on \href{http://www.amazon.com}{http://www.amazon.com})
\end{figure}


%___________________________________________________________________________

\hypertarget{understanding-the-method-resolution-order}{}
\pdfbookmark[1]{Understanding the Method Resolution Order}{understanding-the-method-resolution-order}
\subsection*{Understanding the Method Resolution Order}

The MRO of any given (new style) Python class is given
by the special attribute \texttt{{\_}{\_}mro{\_}{\_}}. Notice that since
Python is an extremely dynamic language it is possible
to delete and to generate whole classes at run time, therefore the MRO
is a dynamic concept. For instance, let me show how it is possibile to 
remove a class from my 
paleoanthropological hierarchy: for instance I can
replace the last class 'HomoSapiensSapiens' with 'HomoSapiensNeardenthalensis'
(changing a class in the middle of the hierarchy would be more difficult). The
following lines do the job dynamically:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> del HomoSapiensSapiens
>>> class HomoSapiensNeardenthalensis(HomoSapiens):
...     def can(self):
...         super(self.__this,self).can()
...         print " - make something"
>>> reflective(HomoSapiensNeardenthalensis)
>>> HomoSapiensNeardenthalensis().can()
HomoSapiensNeardenthalensis can:
 - make tools
 - make abstractions
 - make something\end{verbatim}
\end{quote}

In this case the MRO of 'HomoSapiensNeardenthalensis', i.e. the list of
all its ancestors, is
\begin{quote}
\begin{verbatim}>>> HomoSapiensNeardenthalensis.__mro__
[<class '__main__.HomoSapiensNeardenthalensis'>,<class 'oopp.HomoSapiens'>, 
 <class 'oopp.HomoHabilis'>, <class 'oopp.Homo'>, 
 <class 'oopp.PrettyPrinted'>, <class 'oopp.object'>]\end{verbatim}
\end{quote}

The \texttt{{\_}{\_}mro{\_}{\_}} attribute gives the \emph{linearization} of the class, i.e. the
ordered list of its ancestors, starting from the class itself and ending
with object. The linearization of a class is essential in order to specify
the resolution order of methods and attributes, i.e. the Method Resolution
Order (MRO). In the case of single inheritance hierarchies, such the
paleonthropological example, the MRO is pretty obvious; on the contrary
it is a quite non-trivial concept in the case of multiple inheritance 
hierarchies.

For instance, let me reconsider my first example of multiple inheritance,
the \texttt{NonInstantiableClock} class, inheriting from 'NonInstantiable' and 
'Clock'. I may represent the hierarchy with the following inheritance graph:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~--~~~object~~~--~}\\
\mbox{~~~~~~~~/~~~~~({\_}{\_}new{\_}{\_})~~~~{\textbackslash}}\\
\mbox{~~~~~~~/~~~~~~~~~~~~~~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~/~~~~~~~~~~~~~~~~~~~~~~{\textbackslash}}\\
\mbox{~~~Clock~~~~~~~~~~~~~~~~NonInstantiable}\\
\mbox{(get{\_}time)~~~~~~~~~~~~~~~~~({\_}{\_}new{\_}{\_})}\\
\mbox{~~~~~{\textbackslash}~~~~~~~~~~~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~{\textbackslash}~~~~~~~~~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~{\textbackslash}~~~~~~~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~{\textbackslash}~~~~~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~{\textbackslash}~~~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~NonInstantiableClock~~~}\\
\mbox{~~~~~~~~~~(get{\_}time,{\_}{\_}new{\_}{\_})}
\end{flushleft}\end{ttfamily}
\end{quote}

The class \texttt{Clock} define a \texttt{get{\_}time} method, whereas the class 
\texttt{NonInstantiable} overrides the \texttt{{\_}{\_}new{\_}{\_}} method of the \texttt{object} class; 
the class \texttt{NonInstantiableClock} inherits \texttt{get{\_}time} from 'Clock' and 
\texttt{{\_}{\_}new{\_}{\_}} from 'NonInstantiable'.

The linearization of 'NonInstantiableClock' is
\begin{quote}
\begin{verbatim}>>> NonInstantiableClock.mro()
[<class '__main__.NonInstantiableClock'>, <class 'oopp.Clock'>, 
 <class 'oopp.NonInstantiable'>, <type 'object'>]\end{verbatim}
\end{quote}

In particular, since 'NonInstantiable' precedes 'object', its \texttt{{\_}{\_}new{\_}{\_}} 
method overrides the \texttt{object} new method. However, with the MRO used before
Python 2.2, the linearization would have been \texttt{NonInstantiableClock, Clock, 
object, NonInstantiable, object} and the \texttt{{\_}{\_}new{\_}{\_}} method of object would 
have (hypothetically, of course, since before Python 2.2 there was not 
\texttt{{\_}{\_}new{\_}{\_}} method! ;-)  overridden the \texttt{{\_}{\_}new{\_}{\_}}
method of \texttt{NonInstantiable}, therefore \texttt{NonInstantiableClock} would 
have lost the property of being non-instantiable!

This simple example shows that the choice of a correct Method Resolution 
Order is far from being obvious in general multiple inheritance hierarchies. 
After a false start in Python 2.2, (with a MRO failing in some subtle cases)
Python 2.3 decided to adopt the so-called C3 MRO, invented by people working 
on Dylan (even if Dylan itself uses the MRO of Common Lisp CLOS). Since this 
is quite a technical matter, I defer the interested reader to appendix 2 
for a full discussion of the C3 algorithm.

Here, I prefer to point out how the built-in
\texttt{super} object works in multiple inheritance situations. To this aim, it 
is convenient to define an utility function that retrieves the ancestors
of a given class with respect to the MRO of one of its subclasses:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~ancestor(C,S=None):}\\
\mbox{~~~~"""Returns~the~ancestors~of~the~first~argument~with~respect~to~the~}\\
\mbox{~~~~MRO~of~the~second~argument.~If~the~second~argument~is~None,~then~}\\
\mbox{~~~~returns~the~MRO~of~the~first~argument."""}\\
\mbox{~~~~if~C~is~object:}\\
\mbox{~~~~~~~~raise~TypeError("There~is~no~superclass~of~object")}\\
\mbox{~~~~elif~S~is~None~or~S~is~C:}\\
\mbox{~~~~~~~~return~list(C.{\_}{\_}mro{\_}{\_})}\\
\mbox{~~~~elif~issubclass(S,C):~{\#}~typical~case}\\
\mbox{~~~~~~~~mro=list(S.{\_}{\_}mro{\_}{\_})}\\
\mbox{~~~~~~~~return~mro[mro.index(C):]~{\#}~compute~the~ancestors~from~the~MRO~of~S}\\
\mbox{~~~~else:}\\
\mbox{~~~~~~~~raise~TypeError("S~must~be~a~subclass~of~C")}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Let me show how the function \texttt{ancestor} works. 
Consider the class \texttt{Clock} in isolation: then 
its direct superclass, i.e. the first ancestor, is \texttt{object},
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> ancestor(Clock)[1]
<type 'object'>\end{verbatim}
\end{quote}

therefore \texttt{super(Clock).{\_}{\_}new{\_}{\_}} retrieves the \texttt{object.{\_}{\_}new{\_}{\_}} method:
\begin{quote}
\begin{verbatim}>>> super(Clock).__new__
<built-in method __new__ of type object at 0x80e6fc0>\end{verbatim}
\end{quote}

Consider now the \texttt{Clock} class together with its subclass 
\texttt{NonInstantiableClock}:
in this case the first ancestor of \texttt{Clock}, \emph{with respect to the MRO of 
'NonInstantiableClock'} is \texttt{NonInstantiable}
\begin{quote}
\begin{verbatim}>>> ancestor(Clock,NonInstantiableClock)[1] 
<class 'oopp.NonInstantiable'>\end{verbatim}
\end{quote}

Therefore \texttt{super(Clock,NonInstantiableClock).{\_}{\_}new{\_}{\_}} retrieves the 
\texttt{NonInstantiable.{\_}{\_}new{\_}{\_}} method:
\begin{quote}
\begin{verbatim}>>> super(Clock,NonInstantiableClock).__new__
<function __new__ at 0x81b293c>
>>> NonInstantiable.__new__
<function __new__ at 0x81b293c>\end{verbatim}
\end{quote}

It must be pointed out that \texttt{super(C,S)} is equivalent but not the same 
than \texttt{ancestor(C,S)[1]}, since it does not return the superclass: 
it returns a super object, instead:
\begin{quote}
\begin{verbatim}>>> super(Clock,NonInstantiableClock)
<super: <class 'Clock'>, <type object>>\end{verbatim}

{\#}{\textless}oopp.py{\textgreater}

{\#}class Super(super):
{\#}    def {\_}{\_}init{\_}{\_}(self,C,S=None):
{\#}        super(Super,self).{\_}{\_}init{\_}{\_}(C,S)
{\#}        self.{\_}{\_}name{\_}{\_}=''Super({\%}s)`` {\%} C.{\_}{\_}name{\_}{\_}

{\#}{\textless}/oopp.py{\textgreater}
\end{quote}

Finally, there is little quirk of super:
\begin{quote}
\begin{verbatim}>>> class C(PrettyPrinted): pass
>>> s=super(C,C())
>>> s.__str__()\end{verbatim}
\end{quote}

but
\begin{quote}
\begin{verbatim}>>> str(s) # idem for print s
"<super: <class 'C'>, <C object>>"\end{verbatim}
\end{quote}

Idem for non-pre-existing methods:
\begin{quote}
\begin{verbatim}>>> class D(list): pass
...
>>> s=super(D,D())
>>> s.__len__()
0
>>> len(s) #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: len() of unsized object\end{verbatim}
\end{quote}

The same problem comes with \texttt{{\_}{\_}getattr{\_}{\_}}:
\begin{quote}
\begin{verbatim}>>> class E(object):
...     def __getattr__(self,name):
...             if name=='__len__': return lambda:0
...
>>> e=E()
>>> e.__len__()
0
>>> len(e) # error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: len() of unsized object\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{counting-instances}{}
\pdfbookmark[1]{Counting instances}{counting-instances}
\subsection*{Counting instances}
\begin{quote}
\begin{flushleft}
\emph{Everything~should~be~built~top-down,~except~the~first~time.}~\\
--~Alan~Perlis
\end{flushleft}
\end{quote}

Multiple inheritance adds a step further to the bottom-up philosophy and
it makes appealing the idea of creating classes with the only 
purpose of being derived. Whereas in the top-down approach one starts
with full featured standalone classes, to be further refined, in the
mix-in approach one starts with bare bone classes, providing very simple 
or even trivial features, with the purpose of providing 
basic reusable components in multiple inheritance hierarchies.
At the very end, the idea is to generate a library of \emph{mixin} classes, to be
composed with other classes. We already saw a couple of examples of
mixin classes: 'NonInstantiable' and 'Customizable'. In this paragraph
I will show three other examples: 'WithCounter','Singleton' and
'AvoidDuplication'.

A common requirement for a class is the ability to count the number of its
instances. This is a quite easy problem: it is enough to increments a counter 
each time an instance of that class is initialized. However, this idea can
be implemented in the wrong way. i.e. naively one could implement
counting capabilities in a class without such capabilities by modifying the
\texttt{{\_}{\_}init{\_}{\_}} method explicitly in the original source code. 
A better alternative is to follow the bottom-up approach and to implement 
the counting feature in a separate mix-in class: then the feature can be 
added to the original class via multiple inheritance, without touching 
the source.
Moreover, the counter class becomes a reusable components that can be
useful for other problems, too. In order to use the mix-in approach, the 
\texttt{{\_}{\_}new{\_}{\_}} method of the counter class must me cooperative, and preferably
via an anonymous super call.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~WithCounter(object):~}\\
\mbox{~~~~"""Mixin~class~counting~the~total~number~of~its~instances~and~storing~}\\
\mbox{~~~~~it~in~the~class~attribute~counter."""}\\
\mbox{}\\
\mbox{~~~~counter=0~{\#}~class~attribute~(or~static~attribute~in~C++/Java~terms)}\\
\mbox{~}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(cls,*args,**kw):}\\
\mbox{~~~~~~~~cls.counter+=1~{\#}~increments~the~class~attribute}\\
\mbox{~~~~~~~~return~super(cls.{\_}{\_}this,cls).{\_}{\_}new{\_}{\_}(cls,*args,**kw)~~}\\
\mbox{~~~~~~~~{\#}anonymous~cooperative~call~to~the~superclass's~method~{\_}{\_}new{\_}{\_}}\\
\mbox{}\\
\mbox{reflective(WithCounter)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Each time an instance of 'WithCounter' is initialized, the counter 'count' is
incremented and when 'WithCounter' is composed trough multiple inheritance, 
its '{\_}{\_}new{\_}{\_}'  method cooperatively invokes the \texttt{{\_}{\_}new{\_}{\_}} method 
of the other components.

For instance, I can use 'WithCounter' to implement a 'Singleton', i.e. 
a class that can have only one instance. This kind of classes can be
obtained as follows:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Singleton(WithCounter):}\\
\mbox{~~~~"If~you~inherit~from~me,~you~can~only~have~one~instance"}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(cls,*args,**kw):}\\
\mbox{~~~~~~~~if~cls.counter==0:~{\#}first~call}\\
\mbox{~~~~~~~~~~~~cls.instance=super(cls.{\_}{\_}this,cls).{\_}{\_}new{\_}{\_}(cls,*args,**kw)}\\
\mbox{~~~~~~~~return~cls.instance}\\
\mbox{}\\
\mbox{reflective(Singleton)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

As an application, I can create a 
class \texttt{SingleClock} that inherits from \texttt{Clock} 
\emph{and} from \texttt{Singleton}. This means that \texttt{SingleClock} is both a 
'Clock' and a 'Singleton', i.e. there can be only a clock:
\begin{quote}
\begin{verbatim}>>> from oopp import Clock,Singleton
>>> class SingleClock(Clock,Singleton): pass
...
>>> clock1=SingleClock()
>>> clock2=SingleClock()
>>> clock1 is clock2
True\end{verbatim}
\end{quote}

Instantiating many clocks is apparently possible (i.e. no error
message is given) but you always obtain the same instance. This makes
sense, since there is only one time on the system and a single
clock is enough.

A variation of the 'Singleton' is a class that generates a new
instance only when a certain condition is satisfied. Suppose for instance
one has a 'Disk' class, to be instantiated with the syntax 
\texttt{Disk(xpos,ypos,radius)}.
It is clear that two disks with the same radius and the same position in
the cartesian plane, are essentially the same disk (assuming there are no
additional attributes such as the color). Therefore it is a vaste of memory
to instantiate two separate objects to describe the same disk. To solve
this problem, one possibility is to store in a list the calling arguments.
When it is time to instanciate a new objects with arguments args = xpos,ypos,
radius, Python should check if a disk with these arguments has already
been instanciated: in this case that disk should be returned, not a new
one. This logic can be elegantly implemented in a mix-in class such as the 
following (compare with the \texttt{withmemory} wrapper in chapter 2):
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~AvoidDuplication(object):}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(cls,*args,**kw):}\\
\mbox{~~~~~~~~return~super(cls.{\_}{\_}this,cls).{\_}{\_}new{\_}{\_}(cls,*args,**kw)~}\\
\mbox{~~~~{\_}{\_}new{\_}{\_}=withmemory({\_}{\_}new{\_}{\_})~{\#}~collects~the~calls~in~{\_}{\_}new{\_}{\_}.result}\\
\mbox{}\\
\mbox{reflective(AvoidDuplication)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that 'AvoidDuplication' is introduced with the only purpose of
giving its functionality to 'Disk': in order to reach this goal, it is enough 
to derive 'Disk' from this class and our previously
introduced 'GeometricFigure' class by writing something like
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> class Disk(GeometricFigure,AvoidDuplication): 
...     def __init__(self,xpos,ypos,radius):
...         return super(Disk,self).__init__('(x-x0)**2+(y-y0)**2 <= r**2', 
...                                          x0=xpos,y0=ypos,r=radius)\end{verbatim}
\end{quote}

Now, if we create a disk
\begin{quote}
\begin{verbatim}>>> c1=Disk(0,0,10) #creates a disk of radius 10\end{verbatim}
\end{quote}

it is easy enough to check that trying to instantiate a new disk with the
\emph{same} arguments return the old disk:
\begin{quote}
\begin{verbatim}>>> c2=Disk(0,0,10) #returns the *same* old disk
>>> c1 is c2
True\end{verbatim}
\end{quote}

Here, everything works, because through the 
cooperative \texttt{super} mechanism, \texttt{Disk.{\_}{\_}init{\_}{\_}} calls 
\texttt{AvoidDuplication.{\_}{\_}init{\_}{\_}} that calls \texttt{GeometricFigure.{\_}{\_}init{\_}{\_}} 
that in turns initialize the disk. Inverting the order of
'AvoidDuplication' and 'GeometricFigure' would case a disaster, since
\texttt{GeometricFigure.{\_}{\_}init{\_}{\_}} would override \texttt{AvoidDuplication.{\_}{\_}init{\_}{\_}}.

Alternatively, one could use the object factory 'Makeobj' implemented in 
chapter 3:
\begin{quote}
\begin{verbatim}>>> class NonDuplicatedFigure(GeometricFigure,AvoidDuplication): pass
>>> makedisk=Makeobj(NonDuplicatedFigure,'(x-x0)**2/4+(y-y0)**2 <= r**2')
>>> disk1=makedisk(x0=38,y0=7,r=5)
>>> disk2=makedisk(x0=38,y0=7,r=5)
>>> disk1 is disk2
True\end{verbatim}
\end{quote}

Remark: it is interesting to notice that the previous approach would not work
for keyword arguments, directly, since dictionary are unhashable.


%___________________________________________________________________________

\hypertarget{the-pizza-shop-example}{}
\pdfbookmark[1]{The pizza-shop example}{the-pizza-shop-example}
\subsection*{The pizza-shop example}

Now it is time to give a non-trivial example of multiple inheritance with
cooperative and non-cooperative classes. The point is that multiple 
inheritance can easily leads to complicated hierarchies: where the
resolution order of methods is far from being obvious and actually
can give bad surprises.

To explain the issue, let me extend the program for the pizza-shop owner of
chapter 4, by following the bottom-up approach and using anonymous
cooperative super calls.
In this approach, one starts from the simplest thing. 
It is clear that the pizza-shop owner has interest in recording all the 
pizzas he sell. 
To this aim, he needs a class providing logging capabilities: 
each time a new instance is created, its features are stored in a log file. In
order to count the total number of instances, 'WithLogger' must derive from
the 'WithCounter' class. In order to have a nicely printed message,
'WithLogger' must derive from 'PrettyPrinted'. Finally, 
since 'WithLogger' must be a general purpose 
class that I will reuse in other problem as a mixin class, it must be 
cooperative. 'WithLogger' can be implemented as follows:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~WithLogger(WithCounter,PrettyPrinted):}\\
\mbox{~~~~"""WithLogger~inherits~from~WithCounter~the~'count'~class~attribute;~}\\
\mbox{~~~~moreover~it~inherits~'{\_}{\_}str{\_}{\_}'~from~PrettyPrinted"""}\\
\mbox{~~~~logfile=sys.stdout~{\#}default}\\
\mbox{~~~~verboselog=False~{\#}default}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,*args,**kw):~}\\
\mbox{~~~~~~~~super(self.{\_}{\_}this,self).{\_}{\_}init{\_}{\_}(*args,**kw)~{\#}~cooperative}\\
\mbox{~~~~~~~~dic=attributes(self)~{\#}~non-special~attributes~dictionary}\\
\mbox{~~~~~~~~print~>>~self.logfile,'*'*77}\\
\mbox{~~~~~~~~print~>>~self.logfile,~time.asctime()}\\
\mbox{~~~~~~~~print~>>~self.logfile,~"{\%}s.~Created~{\%}s"~{\%}~(type(self).counter,self)}\\
\mbox{~~~~~~~~if~self.verboselog:}\\
\mbox{~~~~~~~~~~~~print~>>~self.logfile,"with~accessibile~non-special~attributes:"}\\
\mbox{~~~~~~~~~~~~if~not~dic:~print~>>~self.logfile,"<NOTHING>",}\\
\mbox{~~~~~~~~~~~~else:~print~>>~self.logfile,~pretty(dic)}\\
\mbox{}\\
\mbox{reflective(WithLogger)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here I could well use \texttt{super(self.{\_}{\_}this,self).{\_}{\_}init{\_}{\_}(*args,**kw)} 
instead of \texttt{super(self.{\_}{\_}this,self).{\_}{\_}init{\_}{\_}(*args,**kw)}, nevertheless 
the standard \texttt{super} works in this case and I can use it with better 
performances.
Thanks to the power of multiple inheritance, we may give logging features
to the 'CustomizablePizza' class defined in chapter 4 
with just one line of code:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> class Pizza(WithLogger,CustomizablePizza):
...     "Notice, WithLogger is before CustomizablePizza"
>>> Pizza.With(toppinglist=['tomato'])('small')
****************************************************************************
Sat Feb 22 14:54:44 2003
1. Created <Pizza>
<__main__.Pizza object at 0x816927c>\end{verbatim}
\end{quote}

It is also possible to have a more verbose output:
\begin{quote}
\begin{verbatim}>>> Pizza.With(verboselog=True)
<class '__main__.Pizza'>
>>> Pizza('large')
****************************************************************************
Sat Feb 22 14:59:51 2003
1. Created <Pizza>
with accessibile non-special attributes: 
With = <bound method type.customized of <class '__main__.Pizza'>>
baseprice = 1
count = 2
formatstring = %s
logfile = <open file '<stdout>', mode 'w' at 0x402c2058>
price = <bound method Pizza.price of <__main__.Pizza object at 0x402f6c8c>>
size = large
sizefactor = {'small': 1, 'large': 3, 'medium': 2}
topping_unit_price = 0.5
toppinglist = ['tomato']
toppings_price = <bound method Pizza.toppings_price of 
  <__main__.Pizza object at 0x402f6c8c>>
verboselog = True
with = <bound method Pizza.customized of 
<__main__.Pizza object at 0x402f6c8c>>
<__main__.Pizza object at 0x401ce7ac>\end{verbatim}
\end{quote}

However, there is a problem here, since the output is '{\textless}Pizza{\textgreater}' and
not the nice 'large pizza with tomato, cost {\$} 4.5' that we would
expect from a child of 'CustomizablePizza'. The solution to the
puzzle is given by the MRO:
\begin{quote}
\begin{verbatim}>>> Pizza.mro()
[<class '__main__.Pizza'>, <class 'oopp.WithLogger'>, 
 <class 'oopp.WithCounter'>, <class 'oopp.PrettyPrinted'>,
 <class 'oopp.CustomizablePizza'>, <class 'oopp.GenericPizza'>, 
 <class 'oopp.Customizable'>,  <type 'object'>]\end{verbatim}
\end{quote}

The inheritance graph is rather complicated:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~~object~~7}\\
\mbox{}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~/~~~~~/~~~{\textbackslash}~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~/~~~~~~/~~~~~{\textbackslash}~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~~~/~~~~~~~/~~~~~~~{\textbackslash}~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~~~/~~~~~~~~/~~~~~~~~~{\textbackslash}~~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~~~/~~~~~~~~~/~~~~~~~~~~~{\textbackslash}~~~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~~~/~~~~~~~~~~/~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~/~~~~~~~~~~~/~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~/~~~~~~~~~~~~/~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~/~~~~~~~~~~~~~/~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~~~{\textbackslash}}\\
\mbox{2~~WithCounter~~~PrettyPrinted~3~~~~GenericPizza~5~~Customizable~6}\\
\mbox{~~({\_}{\_}new{\_}{\_})~~~~({\_}{\_}str{\_}{\_},{\_}{\_}init{\_}{\_})~~~~~~({\_}{\_}str{\_}{\_})~~~~~~~/~~~~~~~~~~~~~~}\\
\mbox{~~~~~~{\textbackslash}~~~~~~~~~~~~/~~~~~~~~~~~~~~~~~~~~~~~/~~~~~~~~/~~~~}\\
\mbox{~~~~~~~{\textbackslash}~~~~~~~~~~/~~~~~~~~~~~~~~~~~~~~~~~/~~~~~~/~~~~~~~~~}\\
\mbox{~~~~~~~~{\textbackslash}~~~~~~~~/~~~~~~~~~~~~~~~~~~~~~~~/~~~~/}\\
\mbox{~~~~~~~~~{\textbackslash}~~~~~~/~~~~~~~~~~~~~~~~~~~~~~~/~~/}\\
\mbox{~~~~~~~~~~{\textbackslash}~~~~/~~~~~~~~~~~~CustomizablePizza~~4}\\
\mbox{~~~~~~~~~~~{\textbackslash}~~/~~~~~~~~~~~~~~~~~~~~~~/~~~~~~~}\\
\mbox{~~~1~~~~~WithLogger~~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~({\_}{\_}init{\_}{\_})~~~~~~~~~~~~~~/~~~~~~~~}\\
\mbox{~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~/}\\
\mbox{}\\
\mbox{~~~~~~~~~~~~~~~~~~Pizza~~O}
\end{flushleft}\end{ttfamily}
\end{quote}

As we see, the precedence in the resolution of methods is far from being 
trivial. It is denoted in the graph with numbers
from 0 to 7: first the methods of 'Pizza' (level 0), then the methods of 
'WithLogger' (level 1), then the methods of 'WithCounter' (level 2),  then 
the methods of 'PrettyPrinted' (level 3), then the methods of
'CustomizablePizza' (level 4), then the methods of 'GenericPizza' (level 5),
then the level of 'Customizable' (level 6), finally the 'object' methods 
(level 7).

The reason why the MRO is so, can be understood by studying 
appendix 1.

We see that the \texttt{{\_}{\_}init{\_}{\_}} methods of 'WithLogger' and 
the \texttt{{\_}{\_}new{\_}{\_}} method of 'WithCounter' are cooperative. 
\texttt{WithLogger.{\_}{\_}init{\_}{\_}} 
calls \texttt{WithCounter.{\_}{\_}init{\_}{\_}} that is
inherited from \texttt{CustomizablePizza.{\_}{\_}init{\_}{\_}} which is not cooperative, 
but this is not dangerous since \texttt{CustomizablePizza.{\_}{\_}init{\_}{\_}} does not need 
to call any other \texttt{{\_}{\_}init{\_}{\_}}.

However, \texttt{PrettyPrinted.{\_}{\_}str{\_}{\_}} and \texttt{GenericPizza.{\_}{\_}str{\_}{\_}} are not
cooperative and since 'PrettyPrinted' precedes 'GenericPizza', the
\texttt{GenericPizza.{\_}{\_}str{\_}{\_}} method is overridden, which is bad.

If  \texttt{WithLogger.{\_}{\_}init{\_}{\_}}  and \texttt{WithCounter.{\_}{\_}new{\_}{\_}} were not 
cooperative, they would therefore badly breaking the program.

The message is: when you inherit from both cooperative and non-cooperative
classes, put cooperative classes first. The will be fair and will not
blindly override methods of the non-cooperative classes.

With multiple inheritance you can reuse old code a lot,
however the price to pay, is to have a non-trivial hierarchy. If from
the beginning we knew that 'Pizza' was needing a 'WithLogger', 
a 'WithCounter' and the
ability to be 'Customizable' we could have put everything in an unique
class. The problem is that in real life one never knows ;) 
Fortunately, Python dynamism allows to correct design mistakes

Remark: in all text books about inheritance, the authors always stress
that inheritance should be used as a ''is-a`` relation, not
and ''has-a`` relation. In spite of this fact, I have decided to implement
the concept of having a logger (or a counter) via a mixin class. One 
should not blindly believe text books ;)


%___________________________________________________________________________

\hypertarget{fixing-wrong-hierarchies}{}
\pdfbookmark[1]{Fixing wrong hierarchies}{fixing-wrong-hierarchies}
\subsection*{Fixing wrong hierarchies}

A typical metaprogramming technique, is the run-time modification of classes.
As I said in a previous chapter, this feature can confuse the programmer and 
should not be abused (in particular it should not be used as a replacement 
of inheritance!); nevertheless, there applications where the ability of 
modifying classes at run time is invaluable: for instance, 
it can be used to correct design mistakes.

In this case we would like the \texttt{{\_}{\_}str{\_}{\_} method} of 'PrettyPrinted' to be
overridden by \texttt{GenericPizza.{\_}{\_}str{\_}{\_}}. Naively, this can be solved by
putting 'WithLogger' after 'GenericPizza'. Unfortunately, doing so 
would cause \texttt{GenericPizza.{\_}{\_}init{\_}{\_}} to override \texttt{WithLogger.{\_}{\_}init{\_}{\_}},
therefore by loosing logging capabilitiesr, unless countermeasures
are taken.

A valid countermeasure could be to replace the non-cooperative
\texttt{GenericPizza.{\_}{\_}init{\_}{\_}} with a cooperative one. This can miraculously
done at run time in few lines of code:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~coop{\_}init(self,size):~{\#}~cooperative~{\_}{\_}init{\_}{\_}~for~GenericPizza}\\
\mbox{~~~~self.size=size}\\
\mbox{~~~~super(self.{\_}GenericPizza{\_}{\_}this,self).{\_}{\_}init{\_}{\_}(size)}\\
\mbox{}\\
\mbox{GenericPizza.{\_}{\_}init{\_}{\_}=coop{\_}init~{\#}~replace~the~old~{\_}{\_}init{\_}{\_}}\\
\mbox{}\\
\mbox{reflective(GenericPizza)~{\#}~define~GenericPizza.{\_}{\_}this}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice the usage of the fully qualified private attribute
\texttt{self.{\_}GenericPizza{\_}{\_}this} inside \texttt{coop{\_}init}: since this function 
is defined outside any class, the automatica mangling mechanism cannot 
work and has to be implemented by hand. Notice also that 
\texttt{super(self.{\_}GenericPizza{\_}{\_}this,self)} could be replaced by
\texttt{super(GenericPizza,self)}; however the simpler approach is
less safe against possible future manipulations of the hierarchy. 
Suppose, for example, we want to create a copy of the hierarchy
with the same name but slightly different features (actually,
in chapter 8 we will implement a traced copy of the pizza hierarchy, 
useful for debugging purposes): then, using \texttt{super(GenericPizza,self)}
would raise an error, since self would be an instance of the traced
hierarchy and \texttt{GenericPizza}  the original nontraced class. Using
the form \texttt{super(self.{\_}GenericPizza{\_}{\_}this,self)} and making 
\texttt{self.{\_}GenericPizza{\_}{\_}this} pointing to the traced 'GenericPizza'
class (actually this will happen automatically) the problems goes
away.

Now everything works if 'WithLogger' is put after 'CustomizablePizza'
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> class PizzaWithLog(CustomizablePizza,WithLogger): pass 
>>> PizzaWithLog.With(toppinglist=['tomato'])('large')
****************************************************************************
Sun Apr 13 16:19:12 2003
1. Created large pizza with tomato, cost $ 4.5
<class '__main__.PizzaWithLog'>\end{verbatim}
\end{quote}

The log correctly  says \texttt{Created large pizza with tomato, cost {\$} 4.5} and not
\texttt{Created <Pizza>} as before since now \texttt{GenericPizza.{\_}{\_}str{\_}{\_}}
overrides \texttt{PrettyPrinted.{\_}{\_}str{\_}{\_}}. Moreover, the hierarchy is logically
better organized:
\begin{quote}
\begin{verbatim}>>> PizzaWithLog.mro()
[<class '__main__.PizzaWithLog'>, <class 'oopp.CustomizablePizza'>, 
<class 'oopp.GenericPizza'>,  <class 'oopp.Customizable'>, 
<class 'oopp.WithLogger'>, <class 'oopp.WithCounter'>, 
<class 'oopp.PrettyPrinted'>, <type 'object'>]\end{verbatim}
\end{quote}

I leave as an exercise for the reader to make the \texttt{{\_}{\_}str{\_}{\_}} methods
cooperative ;)

Obviously, in this example it would have been better to correct the
original hierarchy, by leaving 'Beautiful' instantiable from the beginning
(that's why I said the 'Beautiful' is an example of wrong mix-in class): 
nevertheless, sometimes, one has do to with wrong hierarchies written by 
others, and it can be a pain to fix them, both directly by modifying the 
original source code, and indirectly
by inheritance, since one must change all the names, in order to distinghish
the original classes from the fixed ones. In those cases Python
dynamism can save your life. This also allows you enhance original
classes which are not wrong, but that simply don't do something you want
to implement.

Modifying classes at run-time can be trivial, as in the examples I have
shown here, but can also be rather tricky, as in this example
\begin{quote}
\begin{verbatim}>>> from oopp import PrettyPrinted
>>> class PrettyPrintedWouldBe(object): __str__ = PrettyPrinted.__str__
>>> print PrettyPrintedWouldBe() #error
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
TypeError: unbound method __str__() must be called with PrettyPrinted 
instance as first argument (got nothing instead)\end{verbatim}
\end{quote}

As the error message says, the problem here, is that the 
\texttt{PrettyPrinted.{\_}{\_}str{\_}{\_}} unbound method, has not received any argument. 
This is because in this
form \texttt{PrettyPrintedWouldBe.{\_}{\_}str{\_}{\_}} has been defined as an attribute, 
not as a real method. The solution is to write
\begin{quote}
\begin{verbatim}>>> class PrettyPrintedWouldBe(object): 
...     __str__ = PrettyPrinted.__dict__['__str__']
...
>>> print PrettyPrintedWouldBe() # now it works
<PrettyPrintedWouldBe>\end{verbatim}
\end{quote}

This kind of run-time modifications does not work when private variables
are involved:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<changewithprivate.py>}\\
\mbox{}\\
\mbox{class~C(object):}\\
\mbox{~~~~{\_}{\_}x='C.{\_}{\_}init{\_}{\_}'}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self):~}\\
\mbox{~~~~~~~~print~self.{\_}{\_}x~{\#}~okay}\\
\mbox{}\\
\mbox{class~D(object):}\\
\mbox{~~~~{\_}{\_}x='D.{\_}{\_}init{\_}{\_}'}\\
\mbox{~~~~{\_}{\_}init{\_}{\_}=C.{\_}{\_}dict{\_}{\_}['{\_}{\_}init{\_}{\_}']~{\#}~error}\\
\mbox{}\\
\mbox{class~New:}\\
\mbox{~~~~class~C(object):}\\
\mbox{~~~~~~~~{\_}{\_}x='New.C.{\_}{\_}init{\_}{\_}'}\\
\mbox{~~~~~~~~{\_}{\_}init{\_}{\_}=C.{\_}{\_}dict{\_}{\_}['{\_}{\_}init{\_}{\_}']~{\#}~okay}\\
\mbox{}\\
\mbox{C()}\\
\mbox{try:~D()}\\
\mbox{except~AttributeError,e:~print~e}\\
\mbox{}\\
\mbox{{\#}</changewithprivate.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Gives as result
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{C.{\_}{\_}init{\_}{\_}}\\
\mbox{'D'~object~has~no~attribute~'{\_}C{\_}{\_}x'}\\
\mbox{New.C.{\_}{\_}init{\_}{\_}}
\end{flushleft}\end{ttfamily}
\end{quote}

The problem is that when \texttt{C.{\_}{\_}dict{\_}{\_}['{\_}{\_}init{\_}{\_}']} is compiled 
(to byte-code) \texttt{self.{\_}{\_}x} is expanded to \texttt{self.{\_}C{\_}{\_}x}. However,
when one invokes \texttt{D.{\_}{\_}init{\_}{\_}}, a D-object is passed, which has
a \texttt{self.{\_}D{\_}{\_}x} attribute, but not a \texttt{self.{\_}C{\_}{\_}x} attribute (unless
'D' is a subclass of 'C'. Fortunately, Python wisdom
\begin{quote}

\emph{Namespaces are one honking great idea -- let's do more of those!}
\end{quote}

suggests the right solution: to use a new class with the \emph{same name}
of the old one, but in a different namespace, in order to avoid
confusion. The simplest way to generate a new namespace is to
declare a new class (the class 'New' in this example): then 'New.C'
becomes an inner class of 'New'. Since it has the same name of the
original class, private variables are correctly expanded and one
can freely exchange methods from 'C' to 'New.C' (and viceversa, too).


%___________________________________________________________________________

\hypertarget{modifying-hierarchies}{}
\pdfbookmark[1]{Modifying hierarchies}{modifying-hierarchies}
\subsection*{Modifying hierarchies}
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{def~mod(cls):~return~cls}\\
\mbox{}\\
\mbox{class~New:~pass}\\
\mbox{}\\
\mbox{for~c~in~HomoSapiensSapiens.{\_}{\_}mro{\_}{\_}:}\\
\mbox{~~~~setattr(New,c.{\_}{\_}name{\_}{\_},mod(c))}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{inspecting-python-code}{}
\pdfbookmark[1]{Inspecting Python code}{inspecting-python-code}
\subsection*{Inspecting Python code}

how to inspect a class, by retrieving useful informations about its
information.

A first possibility is to use the standard \texttt{help} function.
The problem of this approach is that \texttt{help} gives too much 
information.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{{\#}plaindata=}\\
\mbox{plainmethod=lambda~m:m~{\#}identity~function}\\
\mbox{}\\
\mbox{class~Get(object):}\\
\mbox{~~~~"""Invoked~as~Get(cls)(xxx)~where~xxx~=~staticmethod,~classmethod,}\\
\mbox{~~~~property,~plainmethod,~plaindata,~returns~the~corresponding~}\\
\mbox{~~~~attributes~as~a~keyword~dictionary.~It~works~by~internally~calling~}\\
\mbox{~~~~the~routine~inspect.classify{\_}class{\_}attrs.~Notice~that~data}\\
\mbox{~~~~attributes~with~double~underscores~are~not~retrieved~}\\
\mbox{~~~~(this~is~by~design)."""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,cls):}\\
\mbox{~~~~~~~~self.staticmethods=kwdict()}\\
\mbox{~~~~~~~~self.classmethods=kwdict()}\\
\mbox{~~~~~~~~self.properties=kwdict()}\\
\mbox{~~~~~~~~self.methods=kwdict()}\\
\mbox{~~~~~~~~self.data=kwdict()}\\
\mbox{~~~~~~~~for~name,~kind,~klass,~attr~in~inspect.classify{\_}class{\_}attrs(cls):}\\
\mbox{~~~~~~~~~~~~if~kind=='static~method':}\\
\mbox{~~~~~~~~~~~~~~~~self.staticmethods[name]=attr}\\
\mbox{~~~~~~~~~~~~elif~kind=='class~method':}\\
\mbox{~~~~~~~~~~~~~~~~self.classmethods[name]=attr}\\
\mbox{~~~~~~~~~~~~elif~kind=='property':}\\
\mbox{~~~~~~~~~~~~~~~~self.properties[name]=attr}\\
\mbox{~~~~~~~~~~~~elif~kind=='method':}\\
\mbox{~~~~~~~~~~~~~~~~self.methods[name]=attr}\\
\mbox{~~~~~~~~~~~~elif~kind=='data':}\\
\mbox{~~~~~~~~~~~~~~~if~not~special(name):~self.data[name]=attr}\\
\mbox{~~~~def~{\_}{\_}call{\_}{\_}(self,descr):~{\#}could~be~done~with~a~dict}\\
\mbox{~~~~~~~~if~descr==staticmethod:~return~self.staticmethods~}\\
\mbox{~~~~~~~~elif~descr==classmethod:~return~self.classmethods}\\
\mbox{~~~~~~~~elif~descr==property:~return~self.properties~}\\
\mbox{~~~~~~~~elif~descr==plainmethod:~return~self.methods}\\
\mbox{~~~~~~~~elif~descr==plaindata:~return~self.data}\\
\mbox{~~~~~~~~else:~raise~SystemExit("Invalid~descriptor")}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

With similar tricks one can automatically recognize cooperative methods:
{\#}it is different, (better NOT to use descriptors)
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{{\#}class~Cooperative(Class):}\\
\mbox{{\#}~~~~{\_}{\_}metaclass{\_}{\_}~=~WithWrappingCapabilities}\\
\mbox{{\#}}\\
\mbox{{\#}~~~~def~cooperative(method):}\\
\mbox{{\#}~~~~~~~~~"""Calls~both~the~superclass~method~and~the~class}\\
\mbox{{\#}~~~~~~~~~method~(if~the~class~has~an~explicit~method).~}\\
\mbox{{\#}~~~~~~~~~Works~for~methods~returning~None."""}\\
\mbox{{\#}~~~~~~~~~name,cls=Cooperative.parameters~{\#}~fixed~by~the~meta-metaclass}\\
\mbox{{\#}~~~~~~~~~def~{\_}(*args,**kw):}\\
\mbox{{\#}~~~~~~~~~~~~getattr(super(cls,args[0]),name)(*args[1:],**kw)~}\\
\mbox{{\#}~~~~~~~~~~~~if~method:~method(*args,**kw)~{\#}~call~it}\\
\mbox{{\#}~~~~~~~~~return~{\_}}\\
\mbox{{\#}~~~~}\\
\mbox{{\#}~~~~cooperative=staticmethod(cooperative)}\\
\mbox{}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~wrapH(cls):}\\
\mbox{~~~~for~c~in~cls.{\_}{\_}mro{\_}{\_}[:-2]:}\\
\mbox{~~~~~~~~tracer.namespace=c.{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~~~~~new=vars(c).get('{\_}{\_}new{\_}{\_}',None)}\\
\mbox{~~~~~~~~if~new:~c.{\_}{\_}new{\_}{\_}=tracedmethod(new)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{the-magic-of-metaclasses-part-i}{}
\pdfbookmark[0]{THE MAGIC OF METACLASSES - PART I}{the-magic-of-metaclasses-part-i}
\section*{THE MAGIC OF METACLASSES - PART I}
\begin{quote}
\begin{flushleft}
\emph{Metaclasses~are~deeper~magic~than~99{\%}~of~users~should~ever~\\
worry~about.~~If~you~wonder~whether~you~need~them,~you~don't~\\
(the~people~who~actually~need~them~know~with~certainty~that~\\
they~need~them,~and~don't~need~an~explanation~about~why).}~\\
--Tim~Peters
\end{flushleft}
\end{quote}

Python always had metaclasses, since they are inherent to its object
model. However, before Python 2.2, metaclasses where tricky and their
study could cause the programmer's brain to explode [\hyperlink{id42}{21}]. Nowadays, 
the situation has changed, and the reader should be able to understand 
this chapter without risk for his/her brain (however I do not give any 
warranty ;)

Put it shortly, metaclasses give to the Python programmer
complete control on the creation of classes. This simple statement
has far reaching consequences, since the ability of interfering with
the process of class creation, enable the programmer to make miracles.

In this and in the following chapters, I will show some of these
miracles.

This chapter will focus on subtle problems of metaclasses in inheritance
and multiple inheritance, including multiple inheritance of metaclasses
with classes and metaclasses with metaclasses.

The next chapter will focus more on applications.
\begin{figure}[b]\hypertarget{id45}[23]
Metaclasses in Python 1.5 [A.k.a the killer joke] 
\href{http://www.python.org/doc/essays/metaclasses/}{http://www.python.org/doc/essays/metaclasses/}
\end{figure}

There is very little documentation about metaclasses, except Guido's
essays and the papers by David Mertz and myself published in IBMdeveloperWorks
\begin{quote}

\href{http://www-106.ibm.com/developerworks/library/l-pymeta.html}{http://www-106.ibm.com/developerworks/library/l-pymeta.html}
\end{quote}


%___________________________________________________________________________

\hypertarget{metaclasses-as-class-factories}{}
\pdfbookmark[1]{Metaclasses as class factories}{metaclasses-as-class-factories}
\subsection*{Metaclasses as class factories}

In the Python object model (inspired from the Smalltalk, that had metaclasses 
a quarter of century ago!) classes themselves are objects. 
Now, since objects are instances of classes, that means that classes 
themselves can be seen as instances of special classes called \emph{metaclasses}.
Notice that things get hairy soon, since by following this idea, one could 
say the metaclasses themselves are classes and therefore objects;  that 
would mean than even metaclasses can be seen as 
instances of special classes called meta-metaclasses. On the other hand,
meta-meta-classes can be seen as instances of meta-meta-metaclasses,
etc. Now, it should be obvious why metaclasses have gained such a 
reputation of brain-exploders ;). However, fortunately, the situation 
is not so bad in practice, since the infinite recursion of metaclasses is 
avoided because there is a metaclass that is the ''mother of all metaclasses``: 
the built-in metaclass \emph{type}. 'type' has the property of being its own 
metaclass, therefore the recursion stops. Consider for instance the following
example:
\begin{quote}
\begin{verbatim}>>> class C(object): pass # a generic class
>>> type(C) #gives the metaclass of C
<type 'type'>
>>> type(type(C)) #gives the metaclass of type
<type 'type'>\end{verbatim}
\end{quote}

The recursion stops, since the metaclass of 'type' is 'type'.
One cool consequence of classes being instances of 'type', 
is that since \emph{type} is a subclass of object,
\begin{quote}
\begin{verbatim}>>> issubclass(type,object)
True \end{verbatim}
\end{quote}

any Python class is not only a subclass of \texttt{object}, but also
an instance of 'object':
\begin{quote}
\begin{verbatim}>>> isinstance(C,type)
True
>>> isinstance(C,object) 
True
>>> issubclass(C,object) 
True\end{verbatim}
\end{quote}

Notice that 'type' is an instance of itself (!) and therefore of 'object':
\begin{quote}
\begin{verbatim}>>> isinstance(type,type) # 'type' is an instance of 'type'
True
>>> isinstance(type,object) # therefore 'type' is an instance of 'object'
True\end{verbatim}
\end{quote}

As it is well known, \texttt{type(X)} returns the type of \texttt{X}; however, 
\texttt{type} has also a second form in which it acts as a class factory.
The form is \texttt{type(name,bases,dic)} where \texttt{name} is the name of
the new class to be created, bases is the tuple of its bases and dic
is the class dictionary. Let me give a few examples:
\begin{quote}
\begin{verbatim}>>> C=type('C',(),{})
>>> C
<class '__main__.C'>
>>> C.__name__
'C'
>>> C.__bases__
(<type 'object'>,)
>>> C.__dict__
<dict-proxy object at 0x8109054>\end{verbatim}
\end{quote}

Notice that since all metaclasses inherits from \texttt{type}, as a consequences
all metaclasses can be used as class factories.

A fairy tale example will help in understanding the concept
and few subtle points on how attributes are transmitted from metaclasses
to their instances.

Let me start by defining a 'Nobility' metaclass :
\begin{quote}
\begin{verbatim}>>> class Nobility(type): attributes="Power,Richness,Beauty"\end{verbatim}
\end{quote}

instances of 'Nobility' are classes such 'Princes', 'Dukes', 'Barons', etc.
\begin{quote}
\begin{verbatim}>>> Prince=Nobility("Prince",(),{})\end{verbatim}
\end{quote}

Instances of 'Nobility' inherits its attributes, just as instances of normal
classes inherits the class docstring:
\begin{quote}
\begin{verbatim}>>> Prince.attributes
'Power,Richness,Beauty'\end{verbatim}
\end{quote}

Nevertheless, 'attributes' will not be retrieved by the \texttt{dir} function:
\begin{quote}
\begin{verbatim}>>> print dir(Prince)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
 '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__repr__', 
 '__setattr__', '__str__', '__weakref__']\end{verbatim}
\end{quote}

However, this is a limitation of \texttt{dir}, in reality \texttt{Prince.attributes}
is there. On the other hand, the situation is different for a specific 
'Prince' object
\begin{quote}
\begin{verbatim}>>> charles=Prince()
>>> charles.attributes #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'Prince' object has no attribute 'attributes'\end{verbatim}
\end{quote}

The transmission of metaclass attributes is not transitive:
instances of the metaclass inherits the attributes, but not the instances 
of the instances. This behavior is by design and is needed in order to avoid 
troubles with special methods. This point will be throughly 
explained in the last paragraph. For the moment, I my notice that the
behaviour is reasonable, since the abstract qualities  'Power,Richness,Beauty'
are more qualities of the 'Prince' class than of one specific representative.
They can always be retrieved via the \texttt{{\_}{\_}class{\_}{\_}} attribute:
\begin{quote}
\begin{verbatim}>>> charles.__class__.attributes
'Power,Richness,Beauty'\end{verbatim}
\end{quote}

Le me now define a metaclass 'Froggyness':
\begin{quote}
\begin{verbatim}>>> class Frogginess(type): attributes="Powerlessness,Poverty,Uglyness"\end{verbatim}
\end{quote}

Instances of 'Frogginess' are classes like 'Frog', 'Toad', etc.
\begin{quote}
\begin{verbatim}>>> Frog=Frogginess("Frog",(),{})
>>> Frog.attributes
'Powerlessness,Poverty,Uglyness'\end{verbatim}
\end{quote}

However, in Python miracles can happen:
\begin{quote}
\begin{verbatim}>>> def miracle(Frog): Frog.__class__=Nobility
>>> miracle(Frog); Frog.attributes
'Powerlessness,Richness,Beauty'\end{verbatim}
\end{quote}

In this example a miracle happened on the class 'Frog', by changing its
(meta)class to 'Nobility'; therefore its attributes have changed accordingly.

However, there is subtle point here. Suppose we explicitly specify the 'Frog'
attributes, in such a way that it can be inherited by one of its specific
representative:
\begin{quote}
\begin{verbatim}>>> Frog.attributes="poor, small, ugly"
>>> jack=Frog(); jack.attributes
'poor, small, ugly'\end{verbatim}
\end{quote}

Then the miracle cannot work:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<fairytale2.py>}\\
\mbox{}\\
\mbox{class~Nobility(type):~attributes="Power,~Richness,~Beauty"}\\
\mbox{Prince=Nobility("Prince",(),{\{}{\}})}\\
\mbox{charles=Prince()}\\
\mbox{}\\
\mbox{class~Frogginess(type):~attributes="Inpuissance,~Poverty,~Uglyness"}\\
\mbox{Frog=Frogginess("Frog",(),{\{}{\}})}\\
\mbox{Frog.attributes="poor,~small,~ugly"}\\
\mbox{jack=Frog()}\\
\mbox{}\\
\mbox{def~miracle(Frog):~Frog.{\_}{\_}class{\_}{\_}=Nobility}\\
\mbox{}\\
\mbox{miracle(Frog)}\\
\mbox{}\\
\mbox{print~"I~am",Frog.attributes,"even~if~my~class~is",Frog.{\_}{\_}class{\_}{\_}}\\
\mbox{}\\
\mbox{{\#}</fairytale2.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{I~am~poor,~small,~ugly~even~if~my~class~is~<class~'{\_}{\_}main{\_}{\_}.Nobility'>}
\end{flushleft}\end{ttfamily}
\end{quote}

The reason is that Python first looks at specific attributes of an object
(in this case the object is the class 'Frog') an only if they are not found, 
it looks at the attributes of its class (here the metaclass 'Nobility').Since 
in this example the 'Frog' class has explicit attributes, the
result is \texttt{poor, small, ugly}. If you think a bit, it makes sense.

Remark:

In Python 2.3 there are restrictions when changing the \texttt{{\_}{\_}class{\_}{\_}} 
attribute for classes:
\begin{quote}
\begin{verbatim}>>> C=type('C',(),{})
>>> C.__class__ = Nobility #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __class__ assignment: only for heap types\end{verbatim}
\end{quote}

Here changing \texttt{C.{\_}{\_}class{\_}{\_}} is not allowed, since 'C' is an instance
of the built-in metaclass 'type'. This restriction, i.e. the fact that 
the built-in metaclass cannot be changed, has been imposed for
security reasons, in order to avoid dirty tricks with the built-in
classes. For instance, if it was possible to change the metaclass
of the 'bool' class, we could arbitrarily change the behavior of
boolean objects. This could led to abuses. 
Thanks to this restriction,
the programmer is always sure that built-in classes behaves as documented.
This is also the reason why 'bool' cannot be subclassed:
\begin{quote}
\begin{verbatim}>>> print bool.__doc__ # in Python 2.2 would give an error
bool(x) -> bool
Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.\end{verbatim}
\end{quote}

In any case, changing the class of a class is not a good idea, since it
does not play well with inheritance, i.e. changing the metaclass of a base 
class does not change the metaclass of its children:
\begin{quote}
\begin{verbatim}>>> class M1(type): f=lambda cls: 'M1.f' #metaclass1
>>> class M2(type): f=lambda cls: 'M2.f' #metaclass2
>>> B=M1('B',(),{}) # B receives M1.f
>>> class C(B): pass #C receives M1.f
>>> B.f()
'M1.f'
B.__class__=M2 #change the metaclass
>>> B.f() #B receives M2.f
'M2.f'
C.f() #however C does *not* receive M2.f
>>> C.f()
'M1.f'
>>> type(B)
<class '__main__.M2'>
>>> type(C)
<class '__main__.M1'>\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{metaclasses-as-class-modifiers}{}
\pdfbookmark[1]{Metaclasses as class modifiers}{metaclasses-as-class-modifiers}
\subsection*{Metaclasses as class modifiers}

The interpretation of metaclasses in terms of class factories is quite
straightforward and I am sure that any Pythonista will be at home 
with the concept. However, metaclasses have such a reputation of black 
magic since their typical usage is \emph{not} as class factories, but as 
\emph{class modifiers}. This means that metaclasses are typically
used to modify \emph{in fieri} classes. The trouble is that the
modification can be utterly magical.
Here there is another fairy tale example showing the syntax
(via the \texttt{{\_}{\_}metaclass{\_}{\_}} hook) and the magic of the game:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~UglyDuckling(PrettyPrinted):}\\
\mbox{~~~~"A~plain,~regular~class"}\\
\mbox{~~~~formatstring="Not~beautiful,~I~am~{\%}s"}\\
\mbox{}\\
\mbox{class~MagicallyTransformed(type):}\\
\mbox{~~~~"Metaclass~changing~the~formatstring~of~its~instances"}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,*args):}\\
\mbox{~~~~~~~~cls.formatstring="Very~beautiful,~since~I~am~{\%}s"}\\
\mbox{~~~~~~~~}\\
\mbox{class~TransformedUglyDuckling(PrettyPrinted):}\\
\mbox{~~~~"A~class~metamagically~modified"}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}~=~MagicallyTransformed}\\
\mbox{~~~~formatstring="Not~beautiful,~I~am~{\%}s"~{\#}~will~be~changed}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{>>>~from~oopp~import~*}\\
\mbox{>>>~print~UglyDuckling()}\\
\mbox{Not~beautiful,~I~am~<UglyDuckling>}
\end{flushleft}\end{ttfamily}
\end{quote}

In this example, even if in 'TransformedUglyDuckling' we explicitely
set the formatstring to  ''Not beautiful, I am {\%}s``, the metaclass changes 
it to ''Very beautiful, even if I am {\%}s`` and thus
\begin{quote}
\begin{verbatim}>>> print TransformedUglyDuckling() # gives
Very beautiful, since I am <TransformedUglyDuckling>\end{verbatim}
\end{quote}

Notice that the \texttt{{\_}{\_}metaclass{\_}{\_}} hook passes to the metaclass 
\texttt{MagicallyTransformed} the name, bases and dictionary of the class 
being created, i.e. 'TransformedUglyDucking'.

Metaclasses, when used as class modifiers, act \emph{differently}
from functions, when inheritance is
involved. To clarify this subtle point, consider a subclass 'Swan' 
of 'UglyDuckling':
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> class Swan(UglyDuckling): 
...     formatstring="Very beautiful, I am %s"
>>> print Swan()
Very beautiful, I am <Swan>\end{verbatim}
\end{quote}

Now, let me define a simple function acting as a class modifier:
\begin{quote}
\begin{verbatim}>>> def magicallyTransform(cls): 
...    "Modifies the class formatstring"
...    customize(cls,formatstring="Very beautiful, even if I am %s")
...    return cls\end{verbatim}
\end{quote}

The function works:
\begin{quote}
\begin{verbatim}>>> magicallyTransform(UglyDuckling)
>>> print UglyDuckling()
Very beautiful, even if I am <UglyDuckling>\end{verbatim}
\end{quote}

This approach is destructive, since we cannot have the original 
and the transformed class at the same time, and has potentially bad side
effects in the derived classes. Nevertheless, in this case it works
and it is not dangereous for the derived class 'Swan', since 'Swan' 
explicitly overrides the 'formatstring' attribute and doesn't care about 
the change in 'UglyDuckling.formatstring'. Therefore the output
of
\begin{quote}
\begin{verbatim}>>> print Swan()
Very beautiful, I am <Swan>\end{verbatim}
\end{quote}

is still the same as before the action of the function \texttt{magicallyTransform}.
The situation is quite different if we use the 'MagicallyTransformed'
metaclass:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> class Swan(TransformedUglyDuckling): 
...     formatstring="Very beautiful, I am %s"\end{verbatim}
\begin{verbatim}>>> print TransformedUglyDuckling() 
Very beautiful, since I am <UglyDuckling>
>>> print Swan() # does *not* print "Very beautiful, I am <Swan>"
Very beautiful, since I am <Swan> \end{verbatim}
\end{quote}

Therefore,  not only the metaclass has magically transformed the 
'TransformedUglyDuckling.formatstring', it has also transformed the 
'Swan.formatstring'! And that, despite the fact that 
'Swan.formatstring' is explicitly set.

The reason for this behaviour is that since 'UglyDuckling' is a base 
class with metaclass 'MagicallyTransformed', and since 'Swan' inherits from
'UglyDuckling', then 'Swan' inherits the metaclass 'MagicallyTransformed',
which is automatically called at 'Swan' creation time.
That's the reason why metaclasses are much more magical and much 
more dangerous than
functions: functions do not override attributes in the derived classes,
metaclasses do, since they are automagically called at the time of
creation of the subclass. In other words, functions are explicit,
metaclasses are implicit. Nevertheless, this behavior can be pretty
useful in many circumstances, and it is a feature, not a bug. In the
situations where this behavior is not intended, one should use a function,
not a metaclass. In general, metaclasses are better than functions,
since metaclasses are classes and as such they can inherit one from each 
other. This means that one can improve a basic metaclass trough 
(multiple) inheritance, with \emph{reuse} of code.


%___________________________________________________________________________

\hypertarget{a-few-caveats-about-the-usage-of-metaclasses}{}
\pdfbookmark[1]{A few caveats about the usage of metaclasses}{a-few-caveats-about-the-usage-of-metaclasses}
\subsection*{A few caveats about the usage of metaclasses}

Let me start with some caveats about the \texttt{{\_}{\_}metaclass{\_}{\_}} hook, which
commonly used and quite powerful, but also quite dangereous.

Let's imagine a programmer not
knowing about metaclasses and looking at the 'TransformedUglyDuckling'
code (assuming there are no comments): she would probably think
that ''{\_}{\_}metaclass{\_}{\_}`` is some special attribute used for introspection
purposes only, with no other effects, and she would probably expect
the output of the script to be ''Not much, I am the class 
TransformedUglyDucking`` whereas it is exacly the contrary! In other
words, when metaclasses are involved,  \emph{what you see, is not what you get}.
The situation is even more implicit when the metaclass is inherited
from some base class, therefore lacking also the visual clue of the hook.

For these reasons, metaclasses are something to be used with great care; 
they can easily make your code unreadable and confuse inexpert programmers. 
Moreover, it is more difficult to debug programs involving metaclasses, since
methods are magically transformed by routines defined in the metaclass,
and the code you see in the class is \emph{not} what Python sees. I think
the least confusing way of using metaclasses, is to concentrate all
the dynamics on them and to write empty classes except for the
metaclass hook. If you write a class with no methods such as
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{class~TransformedUglyDuckling(object):}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=MagicallyTransformed}
\end{flushleft}\end{ttfamily}
\end{quote}

then the only place to look at, is the metaclass. I have found extremely
confusing to have some of the methods defined in the class and some in
the metaclass, especially during debugging.

Another point to make, is that the \texttt{{\_}{\_}metaclass{\_}{\_}}
hook should not be used to modify pre-existing classes, 
since it requires modifying the source code (even if it is enough to 
change one line only). Moreover, it is confusing, since adding a 
\texttt{{\_}{\_}metaclass{\_}{\_}} attribute \emph{after} the class creation would not do the job:
\begin{quote}
\begin{verbatim}>>> from oopp import UglyDuckling, MagicallyTransformed
>>> UglyDuckling.__metaclass__=MagicallyTransformed
>>> print UglyDuckling()
"Not much, I am the class UglyDuckling"\end{verbatim}
\end{quote}

The reason is that we have to think of UglyDuckling as an instance of 
\texttt{type}, the built-in metaclasses; merely adding a \texttt{{\_}{\_}metaclass{\_}{\_}} 
attribute does not re-initialize the class.
The problem is elegantly solved by avoiding the hook and creating
an enhanced copy of the original class trough \texttt{MagicallyTransformed}
used as a class factory.
\begin{quote}
\begin{verbatim}>>> name=UglyDuckling.__name__
>>> bases=UglyDuckling.__bases__
>>> dic=UglyDuckling.__dict__.copy()
>>> UglyDuckling=MagicallyTransformed(name,bases,dic)\end{verbatim}
\end{quote}

Notice that I have recreated 'UglyDuckling', giving to the new class
the old identifier.
\begin{quote}
\begin{verbatim}>>> print UglyDuckling()
Very beautiful, since I am <UglyDuckling>>\end{verbatim}
\end{quote}

The metaclass of this new 'UglyDuckling' has been specified and will 
accompanies all future children of 'UglyDuckling':
\begin{quote}
\begin{verbatim}>>> class Swan(UglyDuckling): pass
...
>>> type(Swan)
<class '__main__.MagicallyTransformed'>\end{verbatim}
\end{quote}

Another caveat, is in the overridding of `` {\_}{\_}init{\_}{\_}`` in the metaclass.
This is quite common in the case of metaclasses called trough the
\texttt{{\_}{\_}metaclass{\_}{\_}} hook mechanism, since in this case the class
has been already defined (if not created) in the class statement,
and we are interested in initializing it, more than in recreating
it (which is still possible, by the way). 
The problem is that overriding \texttt{{\_}{\_}init{\_}{\_}} has severe limitations 
with respect to overriding \texttt{{\_}{\_}new{\_}{\_}},
since the 'name', 'bases' and 'dic' arguments cannot be directly
changed. Let me show an example:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<init{\_}in{\_}metaclass.py>}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{}\\
\mbox{class~M(type):}\\
\mbox{~~~~"Shows~that~dic~cannot~be~modified~in~{\_}{\_}init{\_}{\_},~only~in~{\_}{\_}new{\_}{\_}"}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,name,bases,dic):}\\
\mbox{~~~~~~~~name='C~name~cannot~be~changed~in~{\_}{\_}init{\_}{\_}'}\\
\mbox{~~~~~~~~bases='cannot~be~changed'}\\
\mbox{~~~~~~~~dic['changed']=True}\\
\mbox{}\\
\mbox{class~C(object):}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=M}\\
\mbox{~~~~changed=False}\\
\mbox{}\\
\mbox{print~C.{\_}{\_}name{\_}{\_}~~{\#}~=>~C}\\
\mbox{print~C.{\_}{\_}bases{\_}{\_}~{\#}~=>~(<type~'object'>,)}\\
\mbox{print~C.changed~~~{\#}~=>~False}\\
\mbox{}\\
\mbox{{\#}</init{\_}in{\_}metaclass.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output of this script is \texttt{False}: the dictionary cannot be changed in 
\texttt{{\_}{\_}init{\_}{\_}} method. However, replacing \texttt{dic['changed']=True} with 
\texttt{cls.changed=True} would work. Analougously, changing  \texttt{cls.{\_}{\_}name{\_}{\_}} 
would work. On the other hand, \texttt{{\_}{\_}bases{\_}{\_}} is a read-only attribute and 
cannot be changed once the class has been created, therefore there is no 
way it can be touched in \texttt{{\_}{\_}init{\_}{\_}}. However, \texttt{{\_}{\_}bases{\_}{\_}} could be
changed in \texttt{{\_}{\_}new{\_}{\_}} before the class creation.


%___________________________________________________________________________

\hypertarget{metaclasses-and-inheritance}{}
\pdfbookmark[1]{Metaclasses and inheritance}{metaclasses-and-inheritance}
\subsection*{Metaclasses and inheritance}

It is easy to get confused about the difference between a metaclass
and a mix-in class in multiple inheritance, since 
both are denoted by adjectives and both share the same idea of 
enhancing a hierarchy. Moreover, both mix-in classes and metaclasses 
can be inherited in the whole hierarchy.
Nevertheless, they behaves differently
and there are various subtle point to emphasize. We have already
noticed in the first section that attributes of a metaclass
are transmitted to its instances, but not to the instances of the
instances, whereas the normal inheritance is transitive: the 
grandfather transmits its attributes to the children and to the grandchild 
too. The difference can be represented with the following picture, where
'M' is the metaclass, 'B' a base class, 'C' a children of 'B'
and c an instance of 'C':
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{M~(attr)~~~~~~~~~B~(attr)~~~}\\
\mbox{:~~~~~~~~~~~~~~~~|}\\
\mbox{C~(attr)~~~~~~~~~C~(attr)~~~~}\\
\mbox{:~~~~~~~~~~~~~~~~:}\\
\mbox{c~()~~~~~~~~~~~~~c~(attr)~~~~}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that here the relation of instantiation is denoted by a dotted line.

This picture is valid when C has metaclass M but not base class, on when C
has base class but not metaclass. However, what happens whrn the class C has 
both a metaclass M and a base class B ?
\begin{quote}
\begin{verbatim}>>> class M(type): a='M.a'
>>> class B(object): a='B.a'
>>> class C(B): __metaclass__=M
>>> c=C()\end{verbatim}
\end{quote}

The situation can be represented by in the following graph,
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{(M.a)~~~M~~~B~~(B.a)}\\
\mbox{~~~~~~~~:~~/}\\
\mbox{~~~~~~~~:~/}\\
\mbox{~~~(?)~~C}\\
\mbox{~~~~~~~~:}\\
\mbox{~~~~~~~~:}\\
\mbox{~~~(?)~~c}
\end{flushleft}\end{ttfamily}
\end{quote}

Here the metaclass M and the base class B are fighting one against the other.
Who wins ? C should inherit the attribute 'B.a' from its base B, however,
the metaclass would like to induce an attribute 'M.a'.
The answer is that the inheritance constraint wins on the metaclass contraint:
\begin{quote}
\begin{verbatim}>>> C.a
'B.a'
>>> c.a
'B.a'\end{verbatim}
\end{quote}

The reason is the same we discussed in the fairy tale example: 'M.a' is
an attribute of the metaclass, if its instance C has already a specified
attributed C.a (in this case specified trough inheritance from B), then
the attribute is not modified. However, one could \emph{force} the modification:
\begin{quote}
\begin{verbatim}>>> class M(type):
...     def __init__(cls,*args): cls.a='M.a'
>>> class C(B): __metaclass__=M
>>> C.a
'M.a'\end{verbatim}
\end{quote}

In this case the metaclass M would win on the base class B. Actually,
this is not surprising, since it is explicit. What could be surprising,
had we not explained why inheritance silently wins, is that
\begin{quote}
\begin{verbatim}>>> c.a
'B.a'\end{verbatim}
\end{quote}

This explain the behaviour for special methods like  
\texttt{{\_}{\_}new{\_}{\_},{\_}{\_}init{\_}{\_},{\_}{\_}str{\_}{\_}}, 
etc. which are defined both in the class and the metaclass with the same 
name (in both cases,they are inherited from \texttt{object}).

In the chapter on objects, we learned that the printed representation of
an object can be modified by overring the \texttt{{\_}{\_}str{\_}{\_}} methods of its
class. In the same sense, the printed representation of a class can be 
modified by overring the \texttt{{\_}{\_}str{\_}{\_}} methods of its metaclass. Let me show an 
example:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Printable(PrettyPrinted,type):}\\
\mbox{~~~"""Apparently~does~nothing,~but~actually~makes~PrettyPrinted~acting~as}\\
\mbox{~~~~~~a~metaclass."""}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Instances of 'Printable' are classes with a nice printable representation:
\begin{quote}
\begin{verbatim}>>> from oopp import Printable 
>>> C=Printable('Classname',(),{})
>>> print C
Classname\end{verbatim}
\end{quote}

However, the internal string representation stays the same:
\begin{quote}
\begin{verbatim}>>> C # invokes Printable.__repr__
<class '__main__.Classname'>\end{verbatim}
\end{quote}

Notice that the name of class 'C' is \texttt{Classname} and not 'C' !

Consider for instance the following code:
\begin{quote}
\begin{verbatim}>>> class M(type):
...    def __str__(cls):
...        return cls.__name__
...    def method(cls):
...        return cls.__name__
...
>>> class C(object):
...    __metaclass__=M
>>> c=C()\end{verbatim}
\end{quote}

In this case the \texttt{{\_}{\_}str{\_}{\_}} method in \texttt{M} cannot override the 
\texttt{{\_}{\_}str{\_}{\_}} method in C, which is inherited from \texttt{object}.
Moreover, if you experiment a little, you will see that
\begin{quote}
\begin{verbatim}>>> print C # is equivalent to print M.__str__(C)
C
>>> print c # is equivalent to print C.__str__(c)
<__main__.C object at 0x8158f54>\end{verbatim}
\end{quote}

The first \texttt{{\_}{\_}str{\_}{\_}} is ''attached`` to the metaclass and the
second to the class.

Consider now the standard method ''method``. It is both attached to the
metaclass
\begin{quote}
\begin{verbatim}>>> print M.method(C)
C\end{verbatim}
\end{quote}

and to the class
\begin{quote}
\begin{verbatim}>>> print C.method() #in a sense, this is a class method, i.e. it receives 
C                    #the class as first argument\end{verbatim}
\end{quote}

Actually it can be seen as a class method of 'C' (cfr. Guido van Rossum
''Unifying types and classes in Python 2.2``. When he discusses
classmethods he says: \emph{''Python also has real metaclasses, and perhaps 
methods defined in a metaclass have more right to the name ``class method''; 
but I expect that most programmers won't be using metaclasses``}). Actually,
this is the SmallTalk terminology, Unfortunately, in Python the word
\texttt{classmethod} denotes an attribute descriptor, therefore it is better
to call the methods defined in a metaclass \emph{metamethods}, in order to avoid
any possible confusion.

The difference between \texttt{method} and \texttt{{\_}{\_}str{\_}{\_}} is that you cannot use the
syntax
\begin{quote}
\begin{verbatim}>>> print C.__str__() #error
TypeError: descriptor '__str__' of 'object' object needs an argument\end{verbatim}
\end{quote}

because of the confusion with the other {\_}{\_}str{\_}{\_}; you can only use the
syntax
\begin{quote}
\begin{verbatim}>>> print M.__str__(C)\end{verbatim}
\end{quote}

Suppose now I change C's definition by adding a method called ''method``:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{class~C(object):}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=M}\\
\mbox{~~~~def~{\_}{\_}str{\_}{\_}(self):}\\
\mbox{~~~~~~~~return~"instance~of~{\%}s"~{\%}~self.{\_}{\_}class{\_}{\_}}\\
\mbox{~~~~def~method(self):}\\
\mbox{~~~~~~~~return~"instance~of~{\%}s"~{\%}~self.{\_}{\_}class{\_}{\_}}
\end{flushleft}\end{ttfamily}
\end{quote}

If I do so, then there is name clashing and the previously working
statement print C.method() gives now an error:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Traceback~(most~recent~call~last):}\\
\mbox{~~File~"<stdin>",~line~24,~in~?}\\
\mbox{TypeError:~unbound~method~method()~must~be~called~with~C~instance~as}\\
\mbox{first~argument~(got~nothing~instead)}
\end{flushleft}\end{ttfamily}
\end{quote}

Conclusion: \texttt{{\_}{\_}str{\_}{\_}, {\_}{\_}new{\_}{\_}, {\_}{\_}init{\_}{\_}} etc. defined in the metaclass
have name clashing with the standard methods defined in the class, therefore
they must be invoked with the extended syntax (ex. \texttt{M.{\_}{\_}str{\_}{\_}(C)}),
whereas normal methods in the metaclass with no name clashing with the methods
of the class can be used as class methods (ex. \texttt{C.method()} instead of
\texttt{M.method(C)}).
Metaclass methods are always bound to the metaclass, they bind to the class 
(receiving the class as first argument) only if there is no name clashing with 
already defined methods in the class. Which is the case for \texttt{{\_}{\_}str{\_}{\_}},
\texttt{{\_}{\_}{\_}init{\_}{\_}}, etc.


%___________________________________________________________________________

\hypertarget{conflicting-metaclasses}{}
\pdfbookmark[1]{Conflicting metaclasses}{conflicting-metaclasses}
\subsection*{Conflicting metaclasses}

Consider a class 'A' with metaclass 'M{\_}A' and a class 'B' with 
metaclass 'M{\_}B'; suppose I derive 'C' from 'A' and 'B'. The question is: 
what is the metaclass of 'C' ? Is it 'M{\_}A' or 'M{\_}B' ?

The correct answer (see ''Putting metaclasses to work`` for a thought 
discussion) is 'M{\_}C', where 'M{\_}C' is a metaclass that inherits from 
'M{\_}A' and 'M{\_}B', as in the following graph:
\begin{quote}
\begin{figure}

\includegraphics{fig1.ps}
\end{figure}
\end{quote}

However, Python is not yet that magic, and it does not automatically create 
'M{\_}C'. Instead, it will raise a \texttt{TypeError}, warning the programmer of
the possible confusion:
\begin{quote}
\begin{verbatim}>>> class M_A(type): pass
>>> class M_B(type): pass
>>> A=M_A('A',(),{})
>>> B=M_B('B',(),{})
>>> class C(A,B): pass #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: metatype conflict among bases\end{verbatim}
\end{quote}

This is an example where the metaclasses 'M{\_}A' and 'M{\_}B' fight each other
to generate 'C' instead of cooperating. The metatype conflict can be avoided 
by assegning the correct metaclass to 'C' by hand:
\begin{quote}
\begin{verbatim}>>> class C(A,B): __metaclass__=type("M_AM_B",(M_A,M_B),{})
>>> type(C)
<class '__main__.M_AM_B'>\end{verbatim}
\end{quote}

In general, a class A(B, C, D , ...) can be generated without conflicts only
if type(A) is a  subclass of each of type(B), type(C), ...

In order to avoid conflicts, the following function, that generates
the correct metaclass by looking at the metaclasses of the base
classes, is handy:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{metadic={\{}{\}}}\\
\mbox{}\\
\mbox{def~{\_}generatemetaclass(bases,metas,priority):}\\
\mbox{~~~~trivial=lambda~m:~sum([issubclass(M,m)~for~M~in~metas],m~is~type)}\\
\mbox{~~~~{\#}~hackish!!~m~is~trivial~if~it~is~'type'~or,~in~the~case~explicit}\\
\mbox{~~~~{\#}~metaclasses~are~given,~if~it~is~a~superclass~of~at~least~one~of~them}\\
\mbox{~~~~metabs=tuple([mb~for~mb~in~map(type,bases)~if~not~trivial(mb)])}\\
\mbox{~~~~metabases=(metabs+metas,~metas+metabs)[priority]}\\
\mbox{~~~~if~metabases~in~metadic:~{\#}~already~generated~metaclass}\\
\mbox{~~~~~~~~return~metadic[metabases]}\\
\mbox{~~~~elif~not~metabases:~{\#}~trivial~metabase}\\
\mbox{~~~~~~~~meta=type~}\\
\mbox{~~~~elif~len(metabases)==1:~{\#}~single~metabase}\\
\mbox{~~~~~~~~meta=metabases[0]}\\
\mbox{~~~~else:~{\#}~multiple~metabases}\\
\mbox{~~~~~~~~metaname="{\_}"+''.join([m.{\_}{\_}name{\_}{\_}~for~m~in~metabases])}\\
\mbox{~~~~~~~~meta=makecls()(metaname,metabases,{\{}{\}})}\\
\mbox{~~~~return~metadic.setdefault(metabases,meta)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

This function is particularly smart since:
\begin{quote}
\newcounter{listcnt29}
\begin{list}{\arabic{listcnt29}.}
{
\usecounter{listcnt29}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
Avoid duplications ..

\item {} 
Remember its results.

\end{list}
\end{quote}

We may generate the child of a tuple of base classes with a given metaclass 
and avoiding metatype conflicts thanks to the following \texttt{child} function:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~makecls(*metas,**options):}\\
\mbox{~~~~"""Class~factory~avoiding~metatype~conflicts.~The~invocation~syntax~is}\\
\mbox{~~~~makecls(M1,M2,..,priority=1)(name,bases,dic).~If~the~base~classes~have~}\\
\mbox{~~~~metaclasses~conflicting~within~themselves~or~with~the~given~metaclasses,}\\
\mbox{~~~~it~automatically~generates~a~compatible~metaclass~and~instantiate~it.~}\\
\mbox{~~~~If~priority~is~True,~the~given~metaclasses~have~priority~over~the~}\\
\mbox{~~~~bases'~metaclasses"""}\\
\mbox{}\\
\mbox{~~~~priority=options.get('priority',False)~{\#}~default,~no~priority}\\
\mbox{~~~~return~lambda~n,b,d:~{\_}generatemetaclass(b,metas,priority)(n,b,d)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here is an example of usage:
\begin{quote}
\begin{verbatim}>>> class C(A,B): __metaclass__=makecls()
>>> print C,type(C)
<class 'oopp.AB_'> <class 'oopp._M_AM_B'>\end{verbatim}
\end{quote}

Notice that the automatically generated metaclass does not pollute the 
namespace:
\begin{quote}
\begin{verbatim}>>> _M_A_M_B #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name '_M_A_M_B' is not defined\end{verbatim}
\end{quote}

It can only be accessed as \texttt{type(C)}.

Put it shortly, the \texttt{child} function allows to generate a child from bases 
enhanced by different custom metaclasses, by generating under the hood a 
compatibile metaclass via multiple inheritance from the original metaclasses. 
However, this logic can only work if the original metaclasses are
cooperative, i.e. their methods are written in such a way to avoid
collisions. This can be done by using the cooperative the \texttt{super} call 
mechanism discussed in chapter 4.


%___________________________________________________________________________

\hypertarget{cooperative-metaclasses}{}
\pdfbookmark[1]{Cooperative metaclasses}{cooperative-metaclasses}
\subsection*{Cooperative metaclasses}

In this section I will discuss how metaclasses can be composed with
classes and with metaclasses, too. Since we will discusss even
complicated hierarchies, it is convenient to have an utility 
routine printing the MRO of a given class:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~MRO(cls):}\\
\mbox{~~~~count=0;~out=[]}\\
\mbox{~~~~print~"MRO~of~{\%}s:"~{\%}~cls.{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~for~c~in~cls.{\_}{\_}mro{\_}{\_}:}\\
\mbox{~~~~~~~~name=c.{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~~~~~bases=','.join([b.{\_}{\_}name{\_}{\_}~for~b~in~c.{\_}{\_}bases{\_}{\_}])}\\
\mbox{~~~~~~~~s="~~{\%}s~-~{\%}s({\%}s)"~{\%}~(count,name,bases)}\\
\mbox{~~~~~~~~if~type(c)~is~not~type:~s+="[{\%}s]"~{\%}~type(c).{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~~~~~out.append(s);~count+=1}\\
\mbox{~~~~return~'{\textbackslash}n'.join(out)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that \texttt{MRO} also prints the metaclass' name in square brackets, for
classes enhanced by a non-trivial metaclass.

Consider for instance the following hierarchy:
\begin{quote}
\begin{verbatim}>>> from oopp import MRO
>>> class B(object): pass
>>> class M(B,type): pass
>>> class C(B): __metaclass__=M\end{verbatim}
\end{quote}

Here 'M' is a metaclass that inherits from 'type' and the base class 'B'
and 'C' is both an instance of 'M' and a child of 'B'. The inheritance
graph can be draw as
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~object}\\
\mbox{~/~~~{\textbackslash}}\\
\mbox{B~~~~type}\\
\mbox{|~{\textbackslash}~~/}\\
\mbox{|~~M}\\
\mbox{{\textbackslash}~~:}\\
\mbox{~{\textbackslash}~:~~~~~}\\
\mbox{~~~C}
\end{flushleft}\end{ttfamily}
\end{quote}

Suppose now we want to retrieve the \texttt{{\_}{\_}new{\_}{\_}} method of B's superclass
with respect to the MRO of C: obviously, this is \texttt{object.{\_}{\_}new{\_}{\_}}, since
\begin{quote}
\begin{verbatim}>>> print MRO(C)
MRO of C:
  0 - C(B)[M]
  1 - B(object)
  2 - object()\end{verbatim}
\end{quote}

This allows to create an instance of 'C' in this way:
\begin{quote}
\begin{verbatim}>>> super(B,C).__new__(C) 
<__main__.C object at 0x4018750c>\end{verbatim}
\end{quote}

It is interesting to notice that this would not work in Python 2.2,
due to a bug in the implementation of \texttt{super}, therefore do not
try this trick with older version of Python.

Notice that everything works 
only because \texttt{B} inherits the \texttt{object.{\_}{\_}new{\_}{\_}} staticmethod that 
is cooperative and it turns out that it calls \texttt{type.{\_}{\_}new{\_}{\_}}. However, 
if I give to 'B' a non-cooperative method
\begin{quote}
\begin{verbatim}>>> B.__new__=staticmethod(lambda cls,*args: object.__new__(cls))\end{verbatim}
\end{quote}

things do not work:
\begin{quote}
\begin{verbatim}>>> M('D',(),{}) #error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <lambda>
TypeError: object.__new__(M) is not safe, use type.__new__()\end{verbatim}
\end{quote}

A cooperative method would solve the problem:
\begin{quote}
\begin{verbatim}>>> B.__new__=staticmethod(lambda m,*args: super(B,m).__new__(m,*args))
>>> M('D',(),{}) # calls B.__new__(M,'D',(),{})
<class '__main__.D'>\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{metamethods-vs-class-methods}{}
\pdfbookmark[1]{Metamethods vs class methods}{metamethods-vs-class-methods}
\subsection*{Metamethods vs class methods}

Meta-methods, i.e. methods defined in
a metaclass.

Python has already few built-in metamethods: \texttt{.mro()} 
and \texttt{{\_}{\_}subclass{\_}{\_}}. These are methods of the metaclass 'type' and
there of any of its sub-metaclasses.
\begin{quote}
\begin{verbatim}>>> dir(type)
['__base__', '__bases__', '__basicsize__', '__call__', '__class__', 
 '__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__', 
 '__flags__', '__getattribute__', '__hash__', '__init__', '__itemsize__', 
 '__module__', '__mro__', '__name__', '__new__', '__reduce__', '__repr__', 
 '__setattr__', '__str__', '__subclasses__', '__weakrefoffset__', 'mro']\end{verbatim}
\begin{verbatim}>>> print type.mro.__doc__
mro() -> list
return a type's method resolution order
>>> print type.__subclasses__.__doc__
__subclasses__() -> list of immediate subclasses\end{verbatim}
\begin{verbatim}>>> class A(object): pass
>>> class B(A): pass
>>> B.mro()
[<class '__main__.B'>, <class '__main__.A'>, <type 'object'>]
>>> A.__subclasses__()
[<class '__main__.B'>]\end{verbatim}
\end{quote}

Notice that \texttt{mro()} and \texttt{{\_}{\_}subclasses{\_}{\_}} are not retrieved by \texttt{dir}.

Let me constrast metamethods with the more traditional classmethods.
In many senses, the to concepts are akin:
\begin{quote}
\begin{verbatim}>>> class M(type): 
...    "Metaclass with a (meta)method mm"
...    def mm(cls): return cls
>>> D=M('D',(),{'cm':classmethod(lambda cls: cls)}) 
>>> # instance of M with a classmethod cm
>>> D.mm # the metamethod
<bound method M.mm of <class '__main__.C'>>
>>> D.cm # the classmethod
<unbound method D.<lambda>>\end{verbatim}
\end{quote}

Notice the similarities between the classmethod and the metamethod:
\begin{quote}
\begin{verbatim}>>> D.mm.im_self, D.cm.im_self # the same
(<class '__main__.D'>, <class '__main__.D'>)
>>> D.mm.im_class, D.cm.im_class # still the same
(<class '__main__.M'>, <class '__main__.M'>)\end{verbatim}
\end{quote}

There are no surprises for \texttt{im{\_}func}:
\begin{quote}
\begin{verbatim}>>> D.mm.im_func, D.cm.im_func
(<function mm at 0x402c272c>, <function <lambda> at 0x402c280c>)\end{verbatim}
\end{quote}

Nevertheless, there are differences: metamethods are not bounded to
instances of the class
\begin{quote}
\begin{verbatim}>>> D().cm() # the classmethod works fine
<class '__main__.D'>
>>> D().mm() # the metamethod does not: error
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'D' object has no attribute 'mm'\end{verbatim}
\end{quote}

and they are not retrieved by \texttt{dir}:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> attributes(D).keys() # mm is not retrieved, only cm
['cm']\end{verbatim}
\begin{verbatim}>>> cm.__get__('whatever') #under Python 2.2.0 would give a serious error
Segmentation fault
>>> cm.__get__(None) #under Python 2.3 there is no error
<bound method type.<lambda> of <type 'NoneType'>>\end{verbatim}
\end{quote}

Moreover metamethods behaves differently with respect to multiple
inheritance. If a class A define a classmethod cA and a class B
defines a classmethod cB, then the class C(A,B) inherits both the
classmethods cA and cB. In the case of metamethods defined in M{\_}A
and M{\_}B, the same is true only if one resolves the meta-type
conflict by hand, by generating the metaclass M{\_}C(M{\_}A,M{\_}B). In this
sense, classmethods are simpler to use than metamethods.


%___________________________________________________________________________

\hypertarget{the-magic-of-metaclasses-part-2}{}
\pdfbookmark[0]{THE MAGIC OF METACLASSES - PART 2}{the-magic-of-metaclasses-part-2}
\section*{THE MAGIC OF METACLASSES - PART 2}

Metaclasses are so powerful that a single chapter is not enough to make
justice to them ;) In this second chapter on metaclasses I will 
unravel their deepest secrets, covering topics such as meta-metaclasses,
anonymous inner metaclasses, global metaclasses and advanced class factories.

Moreover, I will give various magical applications of metaclasses,
in the realm of enhancing the Python language itself. Actually, this is
probably the most idiomatic application of metaclasses (Guido's examples
on the metaclass usage are all in this area). I will show
how metaclasses can be used to enhance the \texttt{super} cooperatice call
mechanism.

This is not a chapter for the faint of heart.


%___________________________________________________________________________

\hypertarget{the-secrets-of-the-metaclass-hook}{}
\pdfbookmark[1]{The secrets of the \_\_metaclass\_\_ hook}{the-secrets-of-the-metaclass-hook}
\subsection*{The secrets of the \texttt{{\_}{\_}metaclass{\_}{\_}} hook}

In the previous chapter we have seen how the \texttt{{\_}{\_}metaclass{\_}{\_}} hook can
be used as a way of metaclass enhancing pre-existing classes 
with a minimal change of the sourcecode.

But it has much deeper secrets.

The first and simplest of them, 
is the fact that the hook can be used it can also be defined 
at the module level, \emph{outside} the class. This allows a number of neat 
tricks, since in presence of a  \texttt{{\_}{\_}metaclass{\_}{\_}} hook at the module
level \emph{all} the old style classes in the module (including nested ones!)
acquire that hook. A first application is to rejuvenate old style classes 
to new style classes.

I remind that old style classes are retained with compability with old 
code, but they are a pain in the back, if you want to use features 
intended for new style classes only (for instance properties etc.). 
Naively, one would expect the conversion from old style classes
to new style to be long and error prone: suppose you have a very large 
application with hundreds of old style classes defined in dozens of modules. 
Suppose you want to update your application to Python 2.2+ classes in order
to take advantage of the new features I have discussed extensively in this
book: the naive way to go would be to go trough the source, look for
all classes definitions and change
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Classname:~-->~Classname(object)}
\end{flushleft}\end{ttfamily}
\end{quote}

One could solve this problem with a regular expression search and replace
in all modules, but this would require to change \emph{all} the source.
This is againt the spirit of OOP, we must \emph{reuse} old code.

Metaclasses are particularly handy to solve this problem: actually it is
enough to add to your modules the following line as first line:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\_}{\_}metaclass{\_}{\_}~=~type}
\end{flushleft}\end{ttfamily}
\end{quote}

Then, all your old style classes will have 'type' as their metaclass: this
is akin to say that all the old style classes  are \emph{automagically} rejuvenate 
to new style classes! And this also works for \emph{nested} classes!!
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<rejuvenate.py>}\\
\mbox{}\\
\mbox{{\_}{\_}metaclass{\_}{\_}~=~type~{\#}~this~rejuvanate~all~the~class~in~the~module}\\
\mbox{}\\
\mbox{class~C:}\\
\mbox{~~~class~D:~pass}\\
\mbox{}\\
\mbox{print~dir(C)~~~{\#}~both~C~and~C.D}\\
\mbox{print~dir(C.D)~{\#}~are~now~new~style~classes}\\
\mbox{}\\
\mbox{{\#}</rejuvenate.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

This very first example add consistence (if needed) to the
widespread belief that metaclasses have a well deserved reputation of magic.

The explanation is that defining a global metaclass called \texttt{{\_}{\_}metaclass{\_}{\_}}
automatically makes all old style classes (new style class simply ignore 
the existence of the global \texttt{{\_}{\_}metaclass{\_}{\_}})  defined in you module 
instances of the given metaclass; this automatically converts them to 
new style classes.


%___________________________________________________________________________

\hypertarget{anonymous-inner-metaclasses}{}
\pdfbookmark[1]{Anonymous inner metaclasses}{anonymous-inner-metaclasses}
\subsection*{Anonymous inner metaclasses}

A second, deeper secret of the \texttt{{\_}{\_}metaclass{\_}{\_}} hook is that it can be
used to define anonymous \emph{inner metaclasses}. The following example
explain what I mean:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~totuple(arg):}\\
\mbox{~~~~"Converts~the~argument~to~a~tuple,~if~need~there~is"}\\
\mbox{~~~~if~isinstance(arg,tuple):~return~arg~{\#}~do~nothing}\\
\mbox{~~~~else:~return~(arg,)~{\#}~convert~to~tuple}\\
\mbox{}\\
\mbox{class~BracketCallable(object):}\\
\mbox{~~~~"""Any~subclass~C(BracketCallable)~can~be~called~with~the~syntax~C[t],~}\\
\mbox{~~~~where~t~is~a~tuple~of~arguments~stored~in~bracket{\_}args;~~returns~the~}\\
\mbox{~~~~class~or~an~instance~of~it,~depending~on~the~flag~'returnclass'."""}\\
\mbox{}\\
\mbox{~~~~returnclass=True}\\
\mbox{~~~~class~{\_}{\_}metaclass{\_}{\_}(type):~{\#}~anonymous~inner~metaclass}\\
\mbox{~~~~~~~~def~{\_}{\_}getitem{\_}{\_}(cls,args):~{\#}~non~cooperative~metamethod}\\
\mbox{~~~~~~~~~~~~if~cls.returnclass:~}\\
\mbox{~~~~~~~~~~~~~~~~c=type(cls.{\_}{\_}name{\_}{\_},(cls,),{\{}'bracket{\_}args':totuple(args){\}})}\\
\mbox{~~~~~~~~~~~~~~~~return~c~{\#}~a~customized~copy~of~the~original~class}\\
\mbox{~~~~~~~~~~~~else:}\\
\mbox{~~~~~~~~~~~~~~~~self=cls();~self.bracket{\_}args=totuple(args)}\\
\mbox{~~~~~~~~~~~~~~~~return~self}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

In this code 'BracketCallable.{\_}{\_}metaclass{\_}{\_}' is the anonymous (actually 
it has a special name, \texttt{{\_}{\_}metaclass{\_}{\_}}) inner metaclass of 'BracketCallable'.

The effect of 'BracketCallable.{\_}{\_}metaclass{\_}{\_}' is the following: it makes
'BracketCallable' and its descendants callable with brackets. Since
the 'returnclass' flag is set, \texttt{{\_}{\_}getitem{\_}{\_}} returns the class
with an attribute 'bracket{\_}args' containing the tuple of the passed
arguments (otherwise it returns an instance of the class).
This works since when 
Python encounters an expression of kind \texttt{cls[arg]} it interprets it 
as  \texttt{type(cls).{\_}{\_}getitem{\_}{\_}(cls,arg)}. Therefore, if \texttt{cls} is a subclass 
of  'BracketCallable', this means that
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{cls[arg]~<=>~BracketCallable.{\_}{\_}metaclass{\_}{\_}.{\_}{\_}getitem{\_}{\_}(cls,arg)}
\end{flushleft}\end{ttfamily}
\end{quote}

Let me give few examples:
\begin{quote}
\begin{verbatim}>>> from oopp import BracketCallable
>>> type(BracketCallable)
<class 'oopp.__metaclass__'>
>>> print type(BracketCallable).__name__ # not really anonymous
__metaclass__
>>> print BracketCallable['a1'].bracket_args
('a1',)
>>> print BracketCallable['a1','a2'].bracket_args
('a1', 'a2')\end{verbatim}
\end{quote}

This syntactical feature is an example of a thing that can be done 
\emph{trough metaclasses only}: it cannot be emulated by functions.

Anonymous inner metaclasses are the least verbose manner 
of defining metamethods. Moreover, they are a neat trick to define 
mix-in classes that, when inherited, can metamagically enhance 
an entire multiple inheritance hierarchy.

In the previous example \texttt{{\_}{\_}getitem{\_}{\_}} is noncooperative, but nothing
forbids anonymous inner metaclasses from being made cooperative. However,
there is some subtlety one must be aware of.
Let me give an example. My 'WithCounter' class counts how many instances
of 'WithCounter' and its subclasses are generated. However, it does not
distinguishes bewteen different subclasses. 
This was correct in the pizza shop example, simple only the total
number of produced pizzas mattered, however, in other situations,
one may want to reset the counter each time a new subclass is created. 
This can be done automagically by a cooperative inner metaclass:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{class~WithMultiCounter(WithCounter):}\\
\mbox{~~~~"""Each~time~a~new~subclass~is~derived,~the~counter~is~reset"""}\\
\mbox{~~~~class~{\_}{\_}metaclass{\_}{\_}(type):}\\
\mbox{~~~~~~~~def~{\_}{\_}init{\_}{\_}(cls,*args):}\\
\mbox{~~~~~~~~~~~~cls.counter=0}\\
\mbox{~~~~~~~~~~~~super(cls.{\_}{\_}this,cls).{\_}{\_}init{\_}{\_}(*args)}\\
\mbox{~~~~reflective({\_}{\_}metaclass{\_}{\_})}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that the order of execution of this code is subtle:
\newcounter{listcnt30}
\begin{list}{\arabic{listcnt30})}
{
\usecounter{listcnt30}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
first, the fact that WithMulticounter has a non-trivial metaclass is
registered, but nothing else is done;

\item {} 
then, the line \texttt{reflective({\_}{\_}metaclass{\_}{\_})} is executed: this means
that the inner metaclass (and therefore its instances) get an
attribute \texttt{.{\_}metaclass{\_}{\_}this} containing a reference to the
inner metaclass;

\item {} 
then, the outer class is passed to its inner metaclass and created
by the inherited metaclass' \texttt{{\_}{\_}new{\_}{\_}} method;

\item {} 
at this point  \texttt{cls} exists and \texttt{cls.{\_}{\_}this} is inherited from
\texttt{{\_}{\_}metaclass{\_}{\_}.{\_}metaclass{\_}{\_}this}; this means that the expression
\texttt{super(cls.{\_}{\_}this,cls).{\_}{\_}init{\_}{\_}(*args)} is correctly recognized and
'WithMultiCounter' can be initialized;

\item {} 
only after that, the name 'WithMultiCounter' enters in the global namespace
and can be recognized.

\end{list}

Notice in particular that inside \texttt{super}, we could also
use \texttt{cls.{\_}{\_}metaclass{\_}{\_}} instead of \texttt{cls.{\_}{\_}this}, but this
would not work inside \texttt{{\_}{\_}new{\_}{\_}}, whereas \texttt{{\_}{\_}this} would be
recognized even in \texttt{{\_}{\_}new{\_}{\_}}.
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> print MRO(WithMultiCounter)
1 - WithMultiCounter(WithCounter)[__metaclass__]
2 - WithCounter(object)
3 - object()\end{verbatim}
\end{quote}

For sake of readability, often it is convenient
to give a name even to inner classes:
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~WithMultiCounter(WithCounter):}\\
\mbox{~~~~"""Each~time~a~new~subclass~is~derived,~the~counter~is~reset"""}\\
\mbox{~~~~class~ResetsCounter(type):}\\
\mbox{~~~~~~~~def~{\_}{\_}init{\_}{\_}(cls,*args):}\\
\mbox{~~~~~~~~~~~~cls.counter=0}\\
\mbox{~~~~~~~~~~~~super(cls.ResetsCounter,cls).{\_}{\_}init{\_}{\_}(*args)}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=ResetsCounter}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}

Notice that inside super we used the expression \texttt{cls.ResetsCounter} and
not \texttt{WithMultiCounter.ResetsCounter}: doing that would generate a
\texttt{NameError: global name 'WithMultiCounter' is not defined} since at the
time when  \texttt{ResetsCounter.{\_}{\_}init{\_}{\_}} is called for the first time, 
the class \texttt{WithMultiCounter} exists but is has not yet entered the global
namespace: this will happens only after the initialization in the
\texttt{ResetsCounter} metaclass, as we said before.

Without the metaclass one can reset the counter by hand each time, or
can reset the counter on all the classes of the hierarchy with a
convenient function (akin to the 'traceH' routine defined in chapter 6).

Example:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> class GrandFather(WithMultiCounter): pass
>>> class Father(GrandFather): pass
>>> class Child(Father): pass
>>> GrandFather()
<__main__.GrandFather object at 0x402f7f6c> # first GrandFather instance
>>> Father()
<__main__.Father object at 0x402f79ec> # first Father instance
>>> Father()
<__main__.Father object at 0x402f7d4c> # second Father instance
>>> Child.counter # zero instances
0
>>> Father.counter # two instances
2
>>> GrandFather.counter # one instance
1\end{verbatim}
\end{quote}

I leave as an exercise for the reader to show that the original 'WithCounter'
would fail to count correctly the different subclasses and would put the 
total number of instances in 'Child'.


%___________________________________________________________________________

\hypertarget{passing-parameters-to-meta-classes}{}
\pdfbookmark[1]{Passing parameters to (meta) classes}{passing-parameters-to-meta-classes}
\subsection*{Passing parameters to (meta) classes}

Calling a class with brackets is a way of passing parameters to it (or
to its instances, if the 'returnclass' flag is not set). 
There additional ways for of doing that. 
One can control the instantiation syntax of classes by redefining the 
\texttt{{\_}{\_}call{\_}{\_}} method of the metaclass. The point is that when we instantiate 
an object with the syntax \texttt{c=C()}, Python looks
at the \texttt{{\_}{\_}call{\_}{\_}} method of the metaclass of 'C'; the default behaviour
it is to call \texttt{C.{\_}{\_}new{\_}{\_}} and \texttt{C.{\_}{\_}init{\_}{\_}} in succession, however, that
behavior can be overridden. Let me give an example without using
anonymous metaclasses (for sake of clarity only).
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<metacall.py>}\\
\mbox{}\\
\mbox{class~M(type):~{\#}~this~is~C~metaclass}\\
\mbox{~~~~def~{\_}{\_}call{\_}{\_}(cls):}\\
\mbox{~~~~~~~~return~"Called~M.{\_}{\_}call{\_}{\_}"~}\\
\mbox{}\\
\mbox{C=M('C',(),{\{}{\}})~{\#}~calls~type(M).{\_}{\_}call{\_}{\_}}\\
\mbox{c=C()~{\#}~calls~type(C).{\_}{\_}call{\_}{\_}}\\
\mbox{{\#}~attention:~c~is~a~string!}\\
\mbox{print~c~{\#}=>~Called~M.{\_}{\_}call{\_}{\_}}\\
\mbox{}\\
\mbox{{\#}</metacall.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

In this example, \texttt{M.{\_}{\_}call{\_}{\_}} simply 
returns the string \texttt{Called M.{\_}{\_}call{\_}{\_}}, and the class
'C' is \emph{not} instantiated. Overriding the metaclass
\texttt{{\_}{\_}call{\_}{\_} `` method therefore provides another way to implement
the ``Singleton} pattern. However, savage overridings as the one in
this example, are not a good idea, since it will confuse everybody.
This is an example where metaclasses change the semantics: whereas
usually the notation \texttt{C()} means ''creates a C instance``, the
metaclass can give to the syntax \texttt{C()} any meaning we want.
Here there is both the power and the danger of metaclasses: they
allows to make both miracles and disasters. Nevertheless, used with
a grain of salt, they provide a pretty nice convenience.

Anyway, overriding the '{\_}{\_}call{\_}{\_}' method of the metaclass can be 
confusing, since parenthesis are usually reserved to mean instantion,
therefore I will prefere to pass arguments trough brackets.

The beauty and the magic of metaclasses stays in the fact that this mechanism 
is completely general: since metaclasses themselves are classes, we can
'CallableWithBrackets' to pass arguments to a metaclass, i.e.
'CallableWithBrackets' can also be used as a meta-metaclass!

I leave as an exercise for the reader to figure out
how to define meta-meta-metaclasses, meta-meta-meta-metaclasses, etc.
etc. (there is no limit to the abstraction level you can reach with 
metaclasses;-)

Let me show an example: a magical way of making methods cooperative.
This can be done trough a 'Cooperative' metaclass that inherits from
'BracketCallable' and therefore has 'BracketCallable.{\_}{\_}metaclass{\_}{\_}'
as (meta)metaclass:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Cooperative(BracketCallable,type):}\\
\mbox{~~~~"""Bracket-callable~metaclass~implementing~cooperative~methods.~Works}\\
\mbox{~~~~well~for~plain~methods~returning~None,~such~as~{\_}{\_}init{\_}{\_}"""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,*args):}\\
\mbox{~~~~~~~~methods=cls.bracket{\_}args}\\
\mbox{~~~~~~~~for~meth~in~methods:~}\\
\mbox{~~~~~~~~~~~~setattr(cls,meth,cls.coop{\_}method(meth,vars(cls).get(meth)))}\\
\mbox{~~~~def~coop{\_}method(cls,name,method):~{\#}~method~can~be~None}\\
\mbox{~~~~~~~~"""Calls~both~the~superclass~method~and~the~class~method~(if~the~}\\
\mbox{~~~~~~~~class~has~an~explicit~method).~Implemented~via~a~closure"""}\\
\mbox{~~~~~~~~def~{\_}(self,*args,**kw):}\\
\mbox{~~~~~~~~~~~~getattr(super(cls,self),name)(*args,**kw)~{\#}~call~the~supermethod}\\
\mbox{~~~~~~~~~~~~if~method:~method(self,*args,**kw)~{\#}~call~the~method}\\
\mbox{~~~~~~~~return~{\_}}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The code above works for methods returing \texttt{None}, such as \texttt{{\_}{\_}init{\_}{\_}}.
Here I give a first example of application: a hierarchy where the \texttt{{\_}{\_}init{\_}{\_}}
methods are automatically called (similar to automatic initialization
in Java).
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<cooperative.py>}\\
\mbox{}\\
\mbox{from~oopp~import~Cooperative}\\
\mbox{}\\
\mbox{class~B(object):}\\
\mbox{~~~~"""Cooperative~base~class;~all~its~descendants~will~automagically~}\\
\mbox{~~~~invoke~their~ancestors~{\_}{\_}init{\_}{\_}~methods~in~chain."""}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=Cooperative['{\_}{\_}init{\_}{\_}']}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,*args,**kw):}\\
\mbox{~~~~~~~~print~"This~is~B.{\_}{\_}init{\_}{\_}"}\\
\mbox{}\\
\mbox{class~C(B):}\\
\mbox{~~~~"Has~not~explicit~{\_}{\_}init{\_}{\_}"}\\
\mbox{}\\
\mbox{class~D(C):}\\
\mbox{~~~~"""The~metaclass~makes~D.{\_}{\_}init{\_}{\_}~to~call~C.{\_}{\_}init{\_}{\_}~and~}\\
\mbox{~~~~therefore~B.{\_}{\_}init{\_}{\_}"""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,*args,**kw):}\\
\mbox{~~~~~~~~print~"This~is~D.{\_}{\_}init{\_}{\_}"}\\
\mbox{}\\
\mbox{d=D()}\\
\mbox{}\\
\mbox{print~"The~metaclass~of~B~is",type(B)}\\
\mbox{print~"The~meta-metaclass~of~B~is",~type(type(B))}\\
\mbox{}\\
\mbox{{\#}</cooperative.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{This~is~B.{\_}{\_}init{\_}{\_}}\\
\mbox{This~is~D.{\_}{\_}init{\_}{\_}}\\
\mbox{The~metaclass~of~B~is~<class~'oopp.Cooperative'>}\\
\mbox{The~meta-metaclass~of~B~~is~<class~'oopp.{\_}{\_}metaclass{\_}{\_}'>}
\end{flushleft}\end{ttfamily}
\end{quote}

A second example, is the following, an alternative way of
making the paleoanthropological hierarchy of chapter 4 cooperative:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<paleo.py>}\\
\mbox{}\\
\mbox{from~oopp~import~Cooperative,Homo}\\
\mbox{}\\
\mbox{class~HomoHabilis(Homo):}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=Cooperative['can']}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~print~"~-~make~tools"}\\
\mbox{}\\
\mbox{class~HomoSapiens(HomoHabilis):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~print~"~-~make~abstractions"}\\
\mbox{~~~~}\\
\mbox{class~HomoSapiensSapiens(HomoSapiens):}\\
\mbox{~~~~def~can(self):}\\
\mbox{~~~~~~~~print~"~-~make~art"}\\
\mbox{}\\
\mbox{HomoSapiensSapiens().can()}\\
\mbox{}\\
\mbox{{\#}~Output:}\\
\mbox{}\\
\mbox{{\#}~<HomoSapiensSapiens>~can:}\\
\mbox{{\#}~~-~make~tools}\\
\mbox{{\#}~~-~make~abstractions}\\
\mbox{{\#}~~-~make~art}\\
\mbox{}\\
\mbox{{\#}</paleo.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Metaclasses can be used to violate the old good rule ''explicit is
better than implicit``. Looking at the source code for 'HomoSapiens'
and 'HomoSapiensSapiens' one would never imagine the \texttt{can} is
somewhat special. That is why in the following I will prefer to
use the anonymous super call mechanism, which is explicit, instead
of the implicit cooperative mechanism.


%___________________________________________________________________________

\hypertarget{meta-functions}{}
\pdfbookmark[1]{Meta-functions}{meta-functions}
\subsection*{Meta-functions}

The third and deepest secret of the \texttt{{\_}{\_}metaclass{\_}{\_}} hook is that, even if
it is typically used in conjunction with metaclasses, actually the hook 
can refer to generic class factories callable with the signature
\texttt{(name,bases,dic)}. Let me show a few examples 
where \texttt{{\_}{\_}metaclass{\_}{\_}} is a function or a generic callable object
instead of being a metaclass:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<metafun.py>}\\
\mbox{}\\
\mbox{from~oopp~import~kwdict}\\
\mbox{}\\
\mbox{class~Callable(object):}\\
\mbox{~~~~def~{\_}{\_}call{\_}{\_}(self,name,bases,dic):}\\
\mbox{~~~~~~~~print~name,bases,'{\textbackslash}n',kwdict(dic)}\\
\mbox{~~~~~~~~return~type(name,bases,dic)}\\
\mbox{}\\
\mbox{callableobj=Callable()}\\
\mbox{}\\
\mbox{class~C:~{\_}{\_}metaclass{\_}{\_}=callableobj}\\
\mbox{}\\
\mbox{print~"type~of~C:",C.{\_}{\_}class{\_}{\_}}\\
\mbox{}\\
\mbox{def~f(name,bases,dic):}\\
\mbox{~~~~print~name,bases,'{\textbackslash}n',kwdict(dic)}\\
\mbox{~~~~return~type(name,bases,dic)}\\
\mbox{}\\
\mbox{class~D:~{\_}{\_}metaclass{\_}{\_}=f}\\
\mbox{}\\
\mbox{print~"type~of~D:",D.{\_}{\_}class{\_}{\_}}\\
\mbox{}\\
\mbox{class~B(object):}\\
\mbox{~~~~def~{\_}{\_}metaclass{\_}{\_}(name,bases,dic):}\\
\mbox{~~~~~~~~"""In~this~form,~the~{\_}{\_}metaclass{\_}{\_}~attribute~is~a~function.~}\\
\mbox{~~~~~~~~In~practice,~it~works~as~a~special~static~method~analogous~}\\
\mbox{~~~~~~~~to~{\_}{\_}new{\_}{\_}"""}\\
\mbox{~~~~~~~~print~"name:~",~name}\\
\mbox{~~~~~~~~print~"bases:",~bases}\\
\mbox{~~~~~~~~print~"dic:{\textbackslash}n",kwdict(dic)}\\
\mbox{~~~~~~~~return~type(name,bases,dic)}\\
\mbox{}\\
\mbox{class~E(B):~pass}\\
\mbox{}\\
\mbox{print~"type~of~E:",E.{\_}{\_}class{\_}{\_}}\\
\mbox{print~"Non-called~E.{\_}{\_}metaclass{\_}{\_}:",~E.{\_}{\_}metaclass{\_}{\_}}\\
\mbox{}\\
\mbox{{\#}</metafun.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

With output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{C~()~}\\
\mbox{{\_}{\_}metaclass{\_}{\_}~=~<Callable~object~at~0x401c964c>}\\
\mbox{{\_}{\_}module{\_}{\_}~=~{\_}{\_}builtin{\_}{\_}}\\
\mbox{type~of~C:~<type~'type'>}\\
\mbox{D~()~}\\
\mbox{{\_}{\_}metaclass{\_}{\_}~=~<function~f~at~0x401c4994>}\\
\mbox{{\_}{\_}module{\_}{\_}~=~{\_}{\_}builtin{\_}{\_}}\\
\mbox{type~of~D:~<type~'type'>}\\
\mbox{name:~~B}\\
\mbox{bases:~(<type~'object'>,)}\\
\mbox{dic:~}\\
\mbox{{\_}{\_}metaclass{\_}{\_}~=~<function~{\_}{\_}metaclass{\_}{\_}~at~0x401c4a3c>}\\
\mbox{{\_}{\_}module{\_}{\_}~=~{\_}{\_}builtin{\_}{\_}}\\
\mbox{type~of~E:~<type~'type'>}\\
\mbox{Non-called~E.{\_}{\_}metaclass{\_}{\_}:~<unbound~method~E.{\_}{\_}metaclass{\_}{\_}>}
\end{flushleft}\end{ttfamily}
\end{quote}

The advantage/disadvantage of this solution is that the \texttt{{\_}{\_}metaclass{\_}{\_}} 
hook is called only once, i.e. it is not called again if a new class
is derived from the original one. For instance in this example 'E' is
derived from 'B', but the function \texttt{B.{\_}{\_}metaclass{\_}{\_}} is \emph{not} called
during the creation of 'E'.

Metafunctions can also be used when one does not want to transmit the
metaclass contraint. Therefore they usage is convenient in exactly
the opposite situation of a cooperative metaclass.


%___________________________________________________________________________

\hypertarget{anonymous-cooperative-super-calls}{}
\pdfbookmark[1]{Anonymous cooperative super calls}{anonymous-cooperative-super-calls}
\subsection*{Anonymous cooperative super calls}

As I noticed in the previous chapters, the \texttt{super} 
mechanism has an annoying 
problem: one needs to pass explicitely the name of the base class. Typically, 
this is simply an 
inelegance since it is annoying to be forced to retype the name of the base 
class. However, in particular
cases, it can be a problem. This happens for instance if we try to
pass the class's methods to a different class: one cannot do that,
since the methods contains an explicit reference to the original class
and would not work with the new one. Moreover, having named super calls
is annoying in view of refactoring. Consider for
instance the previous \texttt{supernew.py} script: in the \texttt{{\_}{\_}new{\_}{\_}} method
defined inside the class 'B', we called \texttt{Super} with the syntax
\texttt{Super(B,cls)} by repeating the name of the class 'B'. Now,
if in the following I decide to give to 'B' a more descriptive
name, I have to go trough the source, search all the \texttt{super}
calls, and change them accordingly to the new name. It would be
nice having Python do the job for me. A first solution is to call
\texttt{super} (or \texttt{Super}) with the syntax \texttt{super(self.{\_}{\_}this,obj)},
where the special name \texttt{{\_}{\_}this} is explicitly replaced by the name 
of the class where the call is defined by the 'reflective' function
of last chapter. This approach has the disadvantage that each time we
derive a new class, we need to invoke \emph{explicitely} the routine 
\texttt{reflective}. It would be marvelous to instruct Python to invoke
\texttt{reflective} automatically at each class creation. Actually, this
seems to be deep magic and indeed it is: fortunately, a custom metaclass 
can perform this deep magic in few lines:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{~~}\\
\mbox{class~Reflective(type):}\\
\mbox{~~~~"""Cooperative~metaclass~that~defines~the~private~variable~{\_}{\_}this~in}\\
\mbox{~~~~its~instances.~{\_}{\_}this~contains~a~reference~to~the~class,~therefore}\\
\mbox{~~~~it~allows~anonymous~cooperative~super~calls~in~the~class."""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,*args):}\\
\mbox{~~~~~~~~super(Reflective,cls).{\_}{\_}init{\_}{\_}(*args)}\\
\mbox{~~~~~~~~reflective(cls)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now, let me show how 'Reflective' can be used in a practical example.

By deriving new metaclasses from 'Reflective', one can easily 
create powerful class factories that generate reflective classes.

Suppose I want to define a handy class 
factory with the abilitity of counting the number of its instances.

This can be done by noticing that metaclasses are just classes, therefore 
they can be composed with regular classes in multiple inheritance. In 
particular one can derive a 'Logged' metaclass from 'WithLogger': in
this way we send a message to a log file each time a new class is created.
This can be done by composing 'WithLogger' with 'WithMultiCounter.{\_}{\_}metaclass{\_}{\_}'
and with 'Reflective':
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Logged(WithLogger,Reflective):~}\\
\mbox{~~~~"""Metaclass~that~reuses~the~features~provided~by~WithLogger.~In~particular}\\
\mbox{~~~~the~classes~created~by~Logged~are~Reflective,~PrettyPrinted~}\\
\mbox{~~~~and~Customizable."""~{\#}WithLogger~provides~logfile~and~verboselog}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,*args,**kw):}\\
\mbox{~~~~~~~~super(Logged,cls).{\_}{\_}init{\_}{\_}(*args,**kw)~}\\
\mbox{~~~~~~~~bases=','.join([c.{\_}{\_}name{\_}{\_}~for~c~in~cls.{\_}{\_}bases{\_}{\_}])}\\
\mbox{~~~~~~~~print~>>~cls.logfile,~"{\%}s~is~a~child~of~{\%}s"~{\%}~(cls,bases)}\\
\mbox{~~~~~~~~print~>>~cls.logfile,'and~an~instance~of~{\%}s'~{\%}~type(cls).{\_}{\_}name{\_}{\_}}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The MRO is
\begin{quote}
\begin{verbatim}>>> print MRO(Logged)
MRO of Logged:
  0 - Logged(WithLogger,Reflective)
  1 - WithLogger(WithCounter,PrettyPrinted)
  2 - WithCounter(object)
  3 - PrettyPrinted(object)
  4 - Reflective(type)
  5 - type(object)
  6 - object()\end{verbatim}
\end{quote}

and the inheritance graph can be drawn as follows:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}{\_}~object~6~{\_}{\_}{\_}}\\
\mbox{~~~~~~/~~~~~~~~~~~~~~~~~~~~~~~~/~~~~~~~~~{\textbackslash}}\\
\mbox{2~WithCounter~~~~~~~~3~PrettyPrinted~~~~~~~~type~5}\\
\mbox{~~~~~~~~~{\textbackslash}~~~~~~~~~~~~~~~~/~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~{\textbackslash}~~~~~~~~~~~~~~/~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~~/~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~/~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~{\textbackslash}~~~~~~~~/~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~{\textbackslash}~~~~~~/~~~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~1~WithLogger~~~~~~~Reflective~4}\\
\mbox{~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~{\textbackslash}~~~~~/}\\
\mbox{~~~~~~~~~~~~~~~~Logged~0}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~:}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~:}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~C1}
\end{flushleft}\end{ttfamily}
\end{quote}

'WithCounter' acts now as a metaclass, since WithCounter.{\_}{\_}new{\_}{\_} invokes
type.{\_}{\_}new{\_}{\_}. Since \texttt{type.{\_}{\_}new{\_}{\_}} is non-cooperative,
in the composition of a metaclass with a regular class, the metaclass
should be put first: this guarantees that \texttt{{\_}{\_}new{\_}{\_}} derives from
\texttt{type.{\_}{\_}new{\_}{\_}}, thus avoiding the error message.
\begin{quote}
\begin{verbatim}>>> Logged.verboselog=True
>>> C1=Logged('C1',(),{})
*****************************************************************************
Tue Apr 22 18:47:05 2003
1. Created 'C1'
with accessibile non-special attributes:
_C1__this = 'C1'
'C1' is a child of object
and an instance of Logged\end{verbatim}
\end{quote}

Notice that any instance of 'WithCounterReflective' inherits the 'WithCounter' 
attribute \texttt{counter}, that counts the number of classes that have been 
instantiated (however it is not retrieved by \texttt{dir}; moreover the 
instances of 'WithCounterReflective' instances have no \texttt{counter} attribute).
\begin{quote}
\begin{verbatim}>>> C1.counter
1\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{more-on-metaclasses-as-class-factories}{}
\pdfbookmark[1]{More on metaclasses as class factories}{more-on-metaclasses-as-class-factories}
\subsection*{More on metaclasses as class factories}

A slight disadvantage of the approach just described, 
is that 'Logged'  cooperatively invokes the \texttt{type.{\_}{\_}new{\_}{\_}} 
static method, therefore, when we invoke the metaclass, we must explicitly 
provide a name, a tuple of base classes and a dictionary, since the 
\texttt{type.{\_}{\_}new{\_}{\_}} staticmethod requires that signature. Actually, 
the expression
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{C=Logged(name,bases,dic)}
\end{flushleft}\end{ttfamily}
\end{quote}

is roughly syntactic sugar for
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{C=Logged.{\_}{\_}new{\_}{\_}(Logged,name,bases,dic)~}\\
\mbox{assert~isinstance(C,Logged)}\\
\mbox{Logged.{\_}{\_}init{\_}{\_}(C,name,bases,dic)}
\end{flushleft}\end{ttfamily}
\end{quote}

If a different interface is desired, the best way is to use a class
factory 'ClsFactory' analogous to the object factory 'Makeobj' 
defined in chapter 4. It is convenient to make 'ClsFactory'
bracket-callable.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~ClsFactory(BracketCallable):}\\
\mbox{~~~~"""Bracket~callable~non-cooperative~class~acting~as~}\\
\mbox{~~~~a~factory~of~class~factories.}\\
\mbox{}\\
\mbox{~~~~ClsFactory~instances~are~class~factories~accepting~0,1,2~or~3~arguments.~}\\
\mbox{~~.~They~automatically~converts~functions~to~static~methods~}\\
\mbox{~~~~if~the~input~object~is~not~a~class.~If~an~explicit~name~is~not~passed}\\
\mbox{~~~~the~name~of~the~created~class~is~obtained~by~adding~an~underscore~to~}\\
\mbox{~~~~the~name~of~the~original~object."""}\\
\mbox{~~~~}\\
\mbox{~~~~returnclass=False~{\#}~ClsFactory[X]~returns~an~*instance*~of~ClsFactory}\\
\mbox{}\\
\mbox{~~~~def~{\_}{\_}call{\_}{\_}(self,~*args):}\\
\mbox{~~~~~~~~"""Generates~a~new~class~using~self.meta~and~avoiding~conflicts.}\\
\mbox{~~~~~~~~The~first~metaobject~can~be~a~dictionary,~an~object~with~a}\\
\mbox{~~~~~~~~dictionary~(except~a~class),~or~a~simple~name."""}\\
\mbox{~~~~~~~~}\\
\mbox{~~~~~~~~{\#}~default~attributes}\\
\mbox{~~~~~~~~self.name="CreatedWithClsFactory"~~~~}\\
\mbox{~~~~~~~~self.bases=()}\\
\mbox{~~~~~~~~self.dic={\{}{\}}}\\
\mbox{~~~~~~~~self.metas=self.bracket{\_}args}\\
\mbox{}\\
\mbox{~~~~~~~~if~len(args)==1:}\\
\mbox{~~~~~~~~~~~~arg=args[0]}\\
\mbox{~~~~~~~~~~~~if~isinstance(arg,str):~{\#}~is~a~name~}\\
\mbox{~~~~~~~~~~~~~~~~self.name=arg}\\
\mbox{~~~~~~~~~~~~elif~hasattr(arg,'{\_}{\_}name{\_}{\_}'):~{\#}~has~a~name}\\
\mbox{~~~~~~~~~~~~~~~~self.name=arg.{\_}{\_}name{\_}{\_}+'{\_}'}\\
\mbox{~~~~~~~~~~~~self.setbasesdic(arg)}\\
\mbox{~~~~~~~~elif~len(args)==2:~}\\
\mbox{~~~~~~~~~~~~self.name=args[0]~}\\
\mbox{~~~~~~~~~~~~assert~isinstance(self.name,str)~{\#}~must~be~a~name}\\
\mbox{~~~~~~~~~~~~self.setbasesdic(args[1])}\\
\mbox{~~~~~~~~elif~len(args)==3:~{\#}~must~be~name,bases,dic}\\
\mbox{~~~~~~~~~~~~self.name=args[0]}\\
\mbox{~~~~~~~~~~~~self.bases+=args[1]}\\
\mbox{~~~~~~~~~~~~self.dic.update(args[2])}\\
\mbox{~~~~~~~~if~len(args)<3~and~not~self.bases:~{\#}~creating~class~from~a~non-class}\\
\mbox{~~~~~~~~~~~~for~k,v~in~self.dic.iteritems():}\\
\mbox{~~~~~~~~~~~~~~~~if~isfunction(v):~self.dic[k]=staticmethod(v)}\\
\mbox{~~~~~~~~{\#}return~child(*self.bases,**vars(self))}\\
\mbox{~~~~~~~~return~makecls(*self.metas)(self.name,self.bases,self.dic)}\\
\mbox{}\\
\mbox{~~~~def~setbasesdic(self,obj):}\\
\mbox{~~~~~~~~if~isinstance(obj,tuple):~{\#}~is~a~tuple}\\
\mbox{~~~~~~~~~~~~self.bases+=obj}\\
\mbox{~~~~~~~~elif~hasattr(obj,'{\_}{\_}bases{\_}{\_}'):~{\#}~is~a~class}\\
\mbox{~~~~~~~~~~~~self.bases+=obj.{\_}{\_}bases{\_}{\_}}\\
\mbox{~~~~~~~~if~isinstance(obj,dict):~{\#}~is~a~dict}\\
\mbox{~~~~~~~~~~~~self.dic.update(obj)}\\
\mbox{~~~~~~~~elif~hasattr(obj,"{\_}{\_}dict{\_}{\_}"):~{\#}~has~a~dict}\\
\mbox{~~~~~~~~~~~~self.dic.update(obj.{\_}{\_}dict{\_}{\_})}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

'ClsFactory[X]' where 'X' is a metaclass returns callable objects acting as
class factories. For instance
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{Class=ClsFactory[type]~{\#}~generates~non-conflicting~classes}\\
\mbox{Mixin=ClsFactory[Reflective]~{\#}~generates~reflective~classes}\\
\mbox{~~}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

can be used as a class factories that automatically provides a default name,
base classes and dictionary, and avoids meta-type conflicts.
'Mixin' generates reflective classes that can be used as mixin in multiple
inheritance hierarchies. Here I give few example of usage of 'Class':
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> C1,C2,C3=[Class('C'+str(i+1)) for i in range(3)]
>>> C1
<class 'oopp.C1'>
>>> C2
<class 'oopp.C2'>
>>> C3
<class 'oopp.C3'>]\end{verbatim}
\begin{verbatim}>>> Clock=Class('Clock',{'get_time':get_time})
>>> Clock
<class 'oopp.Clock'>
>>> Clock.get_time()
16:01:02\end{verbatim}
\end{quote}

Another typical usage of 'Class' is the conversion of a module in a class: 
for instance
\begin{quote}
\begin{verbatim}>>> time_=Class(time)
>>> time_
<class 'oopp.time_'>\end{verbatim}
\end{quote}

Notice the convention of adding an underscore to the name of the class 
generated from the 'time' module.
\begin{quote}
\begin{verbatim}>>> time_.asctime()
'Mon Jan 20 16:33:21 2003'\end{verbatim}
\end{quote}

Notice that all the functions in the module \texttt{time} has been magically 
converted in staticmethods of the class \texttt{time{\_}}. An advantage of this 
approach is that now the module is a class and can be enhanced with 
metaclasses: for instance we could add tracing capabilities, debugging 
features, etc.

By design, 'Class' and 'Reflective' also works when the first argument 
is a class or a tuple of base classes:
\begin{quote}
\begin{verbatim}>>> ClsFactory_=Class(ClsFactory)
>>> type(ClsFactory_)
<class 'oopp.__metaclass__'>
>>> ClsFactory_=Mixin(ClsFactory) 
>>> type(ClsFactory_) # automagically generated metaclass
<class 'oopp._Reflective__metaclass__'>\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{programming-with-metaclasses}{}
\pdfbookmark[1]{Programming with metaclasses}{programming-with-metaclasses}
\subsection*{Programming with metaclasses}

In order to how a non-trivial application of metaclasses in real life,
let me come back to the pizza shop example discussed in chapter 4 and 6.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~Pizza(toppings,**dic):~}\\
\mbox{~~~~~"""This~function~produces~classes~inheriting~from~GenericPizza~and~}\\
\mbox{~~~~~WithLogger,~using~a~metaclass~inferred~from~Logged"""}\\
\mbox{~~~~~toppinglist=toppings.split()}\\
\mbox{~~~~~name='Pizza'+''.join([n.capitalize()~for~n~in~toppinglist])}\\
\mbox{~~~~~dic['toppinglist']=toppinglist}\\
\mbox{~~~~~return~ClsFactory[Logged](name,}\\
\mbox{~~~~~~~~~~~~(GenericPizza,WithLogger,WithMultiCounter),dic)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{>>>~from~oopp~import~*}\\
\mbox{>>>~Margherita=Pizza('tomato~mozzarella',verboselog=True)}\\
\mbox{*****************************************************************************}\\
\mbox{Tue~May~13~14:42:17~2003}\\
\mbox{1.~Created~'PizzaTomatoMozzarella'}\\
\mbox{with~accessibile~non-special~attributes:}\\
\mbox{ResetsCounter~=~<class~'oopp.ResetsCounter'>}\\
\mbox{{\_}GenericPizza{\_}{\_}this~=~<class~'oopp.GenericPizza'>}\\
\mbox{{\_}WithCounter{\_}{\_}this~=~<class~'oopp.WithCounter'>}\\
\mbox{{\_}WithLogger{\_}{\_}this~=~<class~'oopp.WithLogger'>}\\
\mbox{baseprice~=~1}\\
\mbox{counter~=~0}\\
\mbox{formatstring~=~{\%}s}\\
\mbox{logfile~=~<open~file~'<stdout>',~mode~'w'~at~0x402c2058>}\\
\mbox{price~=~<unbound~method~PizzaTomatoMozzarella.price>}\\
\mbox{sizefactor~=~{\{}'small':~1,~'large':~3,~'medium':~2{\}}}\\
\mbox{topping{\_}unit{\_}price~=~0.5}\\
\mbox{toppinglist~=~['tomato',~'mozzarella']}\\
\mbox{toppings{\_}price~=~<unbound~method~PizzaTomatoMozzarella.toppings{\_}price>}\\
\mbox{verboselog~=~True}\\
\mbox{'PizzaTomatoMozzarella'~is~a~child~of~GenericPizza,WithLogger,}\\
\mbox{WithMultiCounter~and~an~instance~of~{\_}LoggedResetsCounter}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice the \emph{deep} magic: \texttt{Pizza} invokes \texttt{ClsFactory[Logged]} which in 
turns calls the class factory \texttt{child} that creates 'Margherita' from 
'GenericPizza', 'WithLogger' and 'WithMultiCounter' by using the
metaclass 'Logged': however, since 'WithMultiCounter', has the internal
metaclass 'ResetsCounter' , there is a metatype conflict:
\texttt{child} \emph{automagically} solves the conflict by creating the metaclass
'{\_}LoggedResetsCounter' that inherits both from 'Logged' and 'ResetsCounter'. 
At this point, 'Margherita' can be safely created
by '{\_}LoggedResetsCounter'. As such, the creation of 'Margherita'
will be registered in the log file and 'Margherita' (with all its
children) will continue to be able to recognize the special identifier 
\texttt{this}.
\begin{quote}
\begin{verbatim}>>> print Margherita('large')
*****************************************************************************
Tue May 13 14:47:03 2003
1. Created large pizza with tomato,mozzarella, cost $ 6.0
with accessibile non-special attributes:
ResetsCounter = <class 'oopp.ResetsCounter'>
_GenericPizza__this = <class 'oopp.GenericPizza'>
_WithCounter__this = <class 'oopp.WithCounter'>
_WithLogger__this = <class 'oopp.WithLogger'>
baseprice = 1
counter = 1
formatstring = %s
logfile = <open file '<stdout>', mode 'w' at 0x402c2058>
price = <bound method PizzaTomatoMozzarella.price of 
<oopp.PizzaTomatoMozzarella object at 0x4032764c>>
size = large
sizefactor = {'small': 1, 'large': 3, 'medium': 2}
topping_unit_price = 0.5
toppinglist = ['tomato', 'mozzarella']
toppings_price = <bound method PizzaTomatoMozzarella.toppings_price of 
<oopp.PizzaTomatoMozzarella object at 0x4032764c>>
verboselog = True
large pizza with tomato,mozzarella, cost $ 6.0
>>> print MRO(Margherita)
MRO of PizzaTomatoMozzarella:
  0 - PizzaTomatoMozzarella(GenericPizza,WithLogger)[_LoggedResetsCounter]
  1 - GenericPizza(object)
  2 - WithLogger(WithCounter,Customizable,PrettyPrinted)
  3 - WithMultiCounter(WithCounter)[ResetsCounter]
  4 - WithCounter(object)
  5 - PrettyPrinted(object)
  6 - object()\end{verbatim}
\end{quote}

Notice that
\begin{quote}
\begin{verbatim}>>> print Margherita
'PizzaTomatoMozzarella'\end{verbatim}
\end{quote}

The power of inheritance in this example is quite impressive, since
I have reused the same class 'WithLogger' (and its children) both in the 
metaclass hierarchy and in the regular hierarchy: this means that I have added
logging capabilities both to classes and their instances in a
strike! And there is no confusion between the two. For instance,
there is a \texttt{counter} attribute for the metaclass 'Logged' 
and many independent \texttt{counter} attributes for any generated class,
i.e. for any kind of pizza.
\begin{quote}

It is interesting to notice that '' itself is an instance of
its inner metaclass, as \texttt{type()} would show. This technique
avoids the need for inventing a new name for the metaclass. The inner
metaclass is automatically inherited by classes inheriting from the outer
class.
\end{quote}


%___________________________________________________________________________

\hypertarget{metaclass-aided-operator-overloading}{}
\pdfbookmark[1]{Metaclass-aided operator overloading}{metaclass-aided-operator-overloading}
\subsection*{Metaclass-aided operator overloading}

As we discussed in chapter 4, inheriting from built-in types is generally
painful. The problem is that if P is a primitive class, i.e. a 
Python built-in type, and D=D(P) is a derived class, then the 
primitive methods returning P-objects have to be modified (wrapped) in 
such a way to return D-objects.

The problem is expecially clear in the context of operator overloading.

Consider for instance the problem of defining a 'Vector' class in the 
mathematical sense. Mathematically-speaking, vectors are defined as objects 
that can be summed each other and multiplied by numbers; they can be represented 
by (finite or infinite) sequences. In the case of finite sequences, vectors 
can be represented with lists and a vector class can be naturally
implemented by subclassing \texttt{list}:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<vector.py>}\\
\mbox{}\\
\mbox{class~Vector(list):}\\
\mbox{~~~~"""Implements~finite~dimensional~vectors~as~lists.~Can~be~instantiated}\\
\mbox{~~~~as~Vector([a,b,c,..])~or~as~Vector(a,b,c~..)"""}\\
\mbox{~~~~def~{\_}{\_}add{\_}{\_}(self,other):}\\
\mbox{~~~~~~~~return~[el+other[i]~for~i,el~in~enumerate(self)]}\\
\mbox{~~~~{\_}{\_}radd{\_}{\_}={\_}{\_}add{\_}{\_}}\\
\mbox{~~~~def~{\_}{\_}mul{\_}{\_}(self,scalar):}\\
\mbox{~~~~~~~~return~[el*scalar~for~el~in~self]}\\
\mbox{~~~~def~{\_}{\_}rmul{\_}{\_}(self,scalar):}\\
\mbox{~~~~~~~~return~[scalar*el~for~el~in~self]}\\
\mbox{}\\
\mbox{v=Vector([1,0])}\\
\mbox{w=Vector([0,1])}\\
\mbox{}\\
\mbox{print~v+w,~type(v+w)~}\\
\mbox{print~2*v,~type(2*v)~}\\
\mbox{print~v*2,~type(v*2)~}\\
\mbox{}\\
\mbox{{\#}</vector.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

With output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{[1,~1]~<type~'list'>}\\
\mbox{[2,~0]~<type~'list'>}\\
\mbox{[2,~0]~<type~'list'>}
\end{flushleft}\end{ttfamily}
\end{quote}

The problem is that the overloaded methods must be wrapped in such a way
to return \texttt{Vector} object and not \texttt{list} object; moreover, if
\texttt{Vector} is subclassed (for instance by defining a \texttt{NumericVector}),
the overloaded methods must return instances of the subclass. There is
only one way of doing that automatically: trough the magic of metaclasses.

Here is the solution, involving an \texttt{autowrappedmethod} descriptor class,
that wraps the overloaded operators and is automatically invoked by
the metaclass \texttt{AutoWrapped}.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~autowrappedmethod(wrappedmethod):}\\
\mbox{~~~~"""Makes~the~method~returning~cls~instances,~by~wrapping~its}\\
\mbox{~~~~output~with~cls"""}\\
\mbox{~~~~klass=None~{\#}~has~to~be~fixed~dynamically~from~outside}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,meth):}\\
\mbox{~~~~~~~~super(autowrappedmethod,self).{\_}{\_}init{\_}{\_}(meth)~{\#}~cooperative}\\
\mbox{~~~~~~~~self.klass=self.klass~{\#}~class~variable~->~instance~variable}\\
\mbox{~~~~def~wrapper(self):~{\#}~closure}\\
\mbox{~~~~~~~~return~lambda~*args,**kw:~self.klass(self.func(*args,**kw))}\\
\mbox{}\\
\mbox{class~AutoWrapped(type):}\\
\mbox{~~~~"""Metaclass~that~looks~at~the~methods~declared~in~the~attributes~}\\
\mbox{~~~~builtinlist~and~wraplist~of~its~instances~and~wraps~them~with}\\
\mbox{~~~~autowrappedmethod."""}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,name,bases,dic):}\\
\mbox{~~~~~~~~super(AutoWrapped,cls).{\_}{\_}init{\_}{\_}(name,bases,dic)~{\#}~cooperative}\\
\mbox{~~~~~~~~cls.builtinlist=getattr(cls,'builtinlist',[])}\\
\mbox{~~~~~~~~if~not~hasattr(cls,'diclist')~:~{\#}~true~only~at~the~first~call}\\
\mbox{~~~~~~~~~~~~cls.diclist=[(a,vars(bases[0])[a])~for~a~in~cls.builtinlist]}\\
\mbox{~~~~~~~~if~dic.has{\_}key('wraplist'):~{\#}~can~be~true~at~any~call}\\
\mbox{~~~~~~~~~~~~cls.diclist+=[(a,dic[a])~for~a~in~cls.wraplist]~}\\
\mbox{~~~~~~~~wrapper=autowrappedmethod.With(klass=cls)}\\
\mbox{~~~~~~~~d=dict([(a,wrapper(v))~for~a,v~in~cls.diclist])}\\
\mbox{~~~~~~~~customize(cls,**d)}\\
\mbox{~~~~}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Now the \texttt{Vector} class can be written as
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Vector(list):}\\
\mbox{~~~~"""Implements~finite~dimensional~vectors~as~lists.~Can~be~instantiated}\\
\mbox{~~~~as~Vector([a,b,c,..])~or~as~Vector(a,b,c~..)"""}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=AutoWrapped}\\
\mbox{~~~~wraplist='{\_}{\_}add{\_}{\_}~{\_}{\_}radd{\_}{\_}~{\_}{\_}mul{\_}{\_}~{\_}{\_}rmul{\_}{\_}'.split()}\\
\mbox{~~~~def~{\_}{\_}add{\_}{\_}(self,other):}\\
\mbox{~~~~~~~~return~[el+other[i]~for~i,el~in~enumerate(self)]}\\
\mbox{~~~~{\_}{\_}radd{\_}{\_}={\_}{\_}add{\_}{\_}}\\
\mbox{~~~~def~{\_}{\_}mul{\_}{\_}(self,scalar):}\\
\mbox{~~~~~~~~return~[scalar*el~for~el~in~self]}\\
\mbox{~~~~def~{\_}{\_}rmul{\_}{\_}(self,scalar):}\\
\mbox{~~~~~~~~return~[el*scalar~for~el~in~self]}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here the \texttt{AutoWrapped} metaclass wraps the output of \texttt{{\_}{\_}add{\_}{\_}, 
{\_}{\_}radd{\_}{\_}, {\_}{\_}mul{\_}{\_}, {\_}{\_}rmul{\_}{\_}}, guaranteeing that they returns \texttt{Vector}
instances or instances of some subclass of \texttt{Vector}, if \texttt{Vector} is
subclassed. This is an example of usage:
\begin{quote}
% doctest 
\begin{verbatim}>>> from oopp import Vector
>>> v=Vector([1,0])
>>> v
<oopp.Vector object at 0x4032858c>
>>> w=Vector([0,1])
>>> v+2*w
<oopp.Vector object at 0x403190ac>
>>> print v+2*w
[1, 2]\end{verbatim}
\end{quote}

It should be clear by now that metaclasses are the natural framework where
to discuss operator overloading
(at least in languages that have metaclasses ;-). After all, operator
overloading is another kind of (very nice) syntactic sugar and we know
already that metaclasses are very good when we need syntactic sugar.


%___________________________________________________________________________

\hypertarget{advanced-metaprogramming-techniques}{}
\pdfbookmark[0]{ADVANCED METAPROGRAMMING TECHNIQUES}{advanced-metaprogramming-techniques}
\section*{ADVANCED METAPROGRAMMING TECHNIQUES}

In elementary OOP, the programmer works with objects; in advanced OOP,
the programmer works with classes, taking full advantage of
(multiple) inheritance and metaclasses. Metaprograming is the activity of 
building, composing and modifying classes.

I will give various examples of metaprogramming techniques
using run-time class modifications
multiple inheritance, metaclasses, attribute descriptors and
even simple functions.

Moreover, I will show show metaclasses can change the
semantics of Python programs: hence theire reputation of \emph{black} magic. 
That is to say that the techniques explained here are dangerous!


%___________________________________________________________________________

\hypertarget{on-code-processing}{}
\pdfbookmark[1]{On code processing}{on-code-processing}
\subsection*{On code processing}

It is a good programming practice to avoid the direct modification
of source code. Nevertheless, there are situations where the ability of 
modifying the source code \emph{dynamically} is invaluable. Python has the
capability of
\newcounter{listcnt31}
\begin{list}{\arabic{listcnt31})}
{
\usecounter{listcnt31}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
generating new code from scratch;

\item {} 
modifying pre-existing source code;

\item {} 
executing the newly created/modified code at run-time.

\end{list}

The capability of creating source code and executing it \emph{immediately} has
no equivalent in static languages such as C/C++/Java and it is maybe the 
most poweful feature of dynamics languages such as Java/Python/Perl.
This feature has been exploited to its ultimate consequences in the languages
of the Lisp family, in which one can use incredibly poweful macros, which
in a broad sense, are programs that write themselves

In this chapter I will discuss how to implement macros in Python and I will
present some of the miracles you may perform with this technique. To this
aim, I will discuss various ways of manipulating Python source code, by
using regular expressions and state machines.


%___________________________________________________________________________

\hypertarget{regular-expressions}{}
\pdfbookmark[1]{Regular expressions}{regular-expressions}
\subsection*{Regular expressions}
\begin{quote}
\begin{flushleft}
\emph{Some~people,~when~confronted~with~a~problem,~~\\
think~''I~know,~I'll~use~regular~expressions.``~~\\
Now~they~have~two~problems.}~\\
~~~--~Jamie~Zawinski
\end{flushleft}
\end{quote}

Python source code is a kind of text and can manipulated with the same
techniques that are used to manipulate text:
\newcounter{listcnt32}
\begin{list}{\arabic{listcnt32}.}
{
\usecounter{listcnt32}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
the trivial search and replace;

\item {} 
regular expressions;

\item {} 
state machines;

\item {} 
parsers

\end{list}

There is not very much to say about the search and replace methods: it
is fast, efficient and it works. It should always be used whenever
possible. However, in this chapter I will only be interested in cases
where something more sophisticated than a plain search and replace is
needed. Cases that can be managed with regular expressions
or with something even more sophisticated than them: a state machine or
even a full featured parser.

I will \emph{not} give a primer on regular expression here, since they are
already well documented in the standard documentation (see Andrew's
Kuchling 'Howto') as well in many books (for instance 'Mastering Regular
Expression' first edition and 'Python in a Nutshell'). 
Instead, I will give various practical examples of usage.
\begin{quote}
\begin{verbatim}>>> import re
>>> reobj=re.compile(r'x')\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{more-on-metaclasses-and-subclassing-built-in-types}{}
\pdfbookmark[1]{More on metaclasses and subclassing built-in types}{more-on-metaclasses-and-subclassing-built-in-types}
\subsection*{More on metaclasses and subclassing built-in types}

Subclassing \texttt{list} is easy since there are no methods returning lists
except the methods correspondings to the '+' and '*' operators. 
Subclassing \texttt{str} is more complicated, since one has many methods
that return strings. Nevertheless, it can be done with the \texttt{AutoWrapped}
metaclass, simply by specifying the list of the builtins to be wrapped.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Str(str):}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=AutoWrapped}\\
\mbox{~~~~builtinlist="""{\_}{\_}add{\_}{\_}~{\_}{\_}mod{\_}{\_}~{\_}{\_}mul{\_}{\_}~{\_}{\_}rmod{\_}{\_}~{\_}{\_}rmul{\_}{\_}~capitalize}\\
\mbox{~~~~~~~~center~expandtabs~join~ljust~lower~lstrip~replace~rjust~rstrip~strip}\\
\mbox{~~~~~~~~swapcase~title~translate~upper~zfill""".split()}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here I show various tests.
\begin{quote}
% doctest 
\begin{verbatim}>>> from oopp import Str
>>> sum=Str('a')+Str('b') # check the sum
>>> print sum, type(sum)
ab <class 'oopp.Str'>
>>> rprod=Str('a')*2 # check the right product 
>>> print rprod,type(rprod)
aa <class 'oopp.Str'>
>>> lprod=2*Str('a') # check the left product 
>>> print lprod,type(lprod)
aa <class 'oopp.Str'>
>>> r=Str('a').replace('a','b') # check replace
>>> print r,type(r)
b <class 'oopp.Str'>
>>> r=Str('a').capitalize() # check capitalize
>>> print r,type(r)
A <class 'oopp.Str'>\end{verbatim}
\end{quote}

\texttt{Str} acts as a nice base class to built abstractions based on strings.
In particular, regular expressions can be built on top of strings describing
their representation (I remind that if \texttt{x} is a regular expression object, 
\texttt{x.pattern} is its string representation). Then, the sum of two regular 
expressions \texttt{x} and \texttt{y} can be defined as the sum of their string 
representation, \texttt{(x+y).pattern=x.pattern+y.pattern}. Moreover, it is
convenient to define the \texttt{{\_}{\_}or{\_}{\_}} method of two regular expression in
such a way that \texttt{(x | y).pattern=x.pattern+'|'+y.pattern}.
All this can be achieved trough the following class:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~BaseRegexp(Str):}\\
\mbox{}\\
\mbox{~~~~builtinlist=['{\_}{\_}radd{\_}{\_}',~'{\_}{\_}ror{\_}{\_}']}\\
\mbox{~~~~wraplist=['{\_}{\_}add{\_}{\_}','{\_}{\_}or{\_}{\_}']}\\
\mbox{}\\
\mbox{~~~~{\_}{\_}add{\_}{\_}~=~lambda~self,other:~self.pattern~+~other~}\\
\mbox{~~~~{\_}{\_}or{\_}{\_}~~=~lambda~self,other:~self.pattern+'|'+other}\\
\mbox{}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}~(self,regexp):}\\
\mbox{~~~~~~~~"Adds~to~str~methods~the~regexp~methods"}\\
\mbox{~~~~~~~~reobj=re.compile(regexp)}\\
\mbox{~~~~~~~~for~attr~in~dir(reobj)+['pattern']:}\\
\mbox{~~~~~~~~~~~~setattr(self,attr,getattr(reobj,attr))}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{>>>~from~oopp~import~*}\\
\mbox{>>>~aob=BaseRegexp('a')|BaseRegexp('b');~print~aob}\\
\mbox{a|b}\\
\mbox{>>>~print~pretty(attributes(aob))}\\
\mbox{encode~=~<built-in~method~encode~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{endswith~=~<built-in~method~endswith~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{expandtabs~=~<function~{\_}~at~0x401b69cc>}\\
\mbox{find~=~<built-in~method~find~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{findall~=~<built-in~method~findall~of~{\_}sre.SRE{\_}Pattern~object~at~0x4019b890>}\\
\mbox{finditer~=~<built-in~method~finditer~of~{\_}sre.SRE{\_}Pattern~object~at~}\\
\mbox{0x4019b890>}\\
\mbox{index~=~<built-in~method~index~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{isalnum~=~<built-in~method~isalnum~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{isalpha~=~<built-in~method~isalpha~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{isdigit~=~<built-in~method~isdigit~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{islower~=~<built-in~method~islower~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{isspace~=~<built-in~method~isspace~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{istitle~=~<built-in~method~istitle~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{isupper~=~<built-in~method~isupper~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{join~=~<function~{\_}~at~0x401b6a74>}\\
\mbox{ljust~=~<function~{\_}~at~0x401b6b1c>}\\
\mbox{lower~=~<function~{\_}~at~0x401b6cdc>}\\
\mbox{lstrip~=~<function~{\_}~at~0x401b6d84>}\\
\mbox{match~=~<built-in~method~match~of~{\_}sre.SRE{\_}Pattern~object~at~0x4019b890>}\\
\mbox{pattern~=~ba}\\
\mbox{replace~=~<function~{\_}~at~0x401b6ed4>}\\
\mbox{rfind~=~<built-in~method~rfind~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{rindex~=~<built-in~method~rindex~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{rjust~=~<function~{\_}~at~0x401ba0d4>}\\
\mbox{rstrip~=~<function~{\_}~at~0x401ba10c>}\\
\mbox{scanner~=~<built-in~method~scanner~of~{\_}sre.SRE{\_}Pattern~object~at~0x4019b890>}\\
\mbox{search~=~<built-in~method~search~of~{\_}sre.SRE{\_}Pattern~object~at~0x4019b890>}\\
\mbox{split~=~<built-in~method~split~of~{\_}sre.SRE{\_}Pattern~object~at~0x4019b890>}\\
\mbox{splitlines~=~<built-in~method~splitlines~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{startswith~=~<built-in~method~startswith~of~BaseRegexp~object~at~0x401b25cc>}\\
\mbox{strip~=~<function~{\_}~at~0x401ba25c>}\\
\mbox{sub~=~<built-in~method~sub~of~{\_}sre.SRE{\_}Pattern~object~at~0x4019b890>}\\
\mbox{subn~=~<built-in~method~subn~of~{\_}sre.SRE{\_}Pattern~object~at~0x4019b890>}\\
\mbox{swapcase~=~<function~{\_}~at~0x401ba294>}\\
\mbox{title~=~<function~{\_}~at~0x401ba4fc>}\\
\mbox{translate~=~<function~{\_}~at~0x401ba534>}\\
\mbox{upper~=~<function~{\_}~at~0x401ba5dc>}\\
\mbox{wraplist~=~['{\_}{\_}add{\_}{\_}',~'{\_}{\_}radd{\_}{\_}',~'{\_}{\_}or{\_}{\_}',~'{\_}{\_}ror{\_}{\_}']}\\
\mbox{zfill~=~<function~{\_}~at~0x401ba614>}\\
\mbox{}\\
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Regexp(BaseRegexp):}\\
\mbox{~~~~class~{\_}{\_}metaclass{\_}{\_}(BaseRegexp.{\_}{\_}metaclass{\_}{\_}):}\\
\mbox{~~~~~~~~def~{\_}{\_}setattr{\_}{\_}(cls,name,value):}\\
\mbox{~~~~~~~~~~~~if~name==name.upper():~{\#}~all~caps~means~regexp~constant}\\
\mbox{~~~~~~~~~~~~~~~~if~not~isinstance(value,cls):~value=cls(value)}\\
\mbox{~~~~~~~~~~~~~~~~value.name=name~{\#}~set~regexp~name}\\
\mbox{~~~~~~~~~~~~BaseRegexp.{\_}{\_}metaclass{\_}{\_}.{\_}{\_}setattr{\_}{\_}(cls,name,value)}\\
\mbox{~~~~~~~~~~~~{\#}~basic~setattr}\\
\mbox{}\\
\mbox{~~~~def~named(self,name=None):}\\
\mbox{~~~~~~~~name=getattr(self,'name',name)}\\
\mbox{~~~~~~~~if~name~is~None:~raise~'Unnamed~regular~expression'}\\
\mbox{~~~~~~~~return~self.{\_}{\_}class{\_}{\_}('(?P<{\%}s>{\%}s)'~{\%}~(name,self.pattern))}\\
\mbox{}\\
\mbox{~~~~generateblocks=generateblocks}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The magic of \texttt{Regexp.{\_}{\_}metaclass{\_}{\_}} allows to generate a library of 
regular expressions in an elegant way:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{r=Regexp}\\
\mbox{}\\
\mbox{customize(r,}\\
\mbox{~~~~DOTALL~=r'(?s)'~,~~~~~~{\#}~starts~the~DOTALL~mode;~must~be~at~the~beginning}\\
\mbox{~~~~NAME~~~=r'{\textbackslash}b[a-zA-Z{\_}]{\textbackslash}w*',~{\#}~plain~Python~name}\\
\mbox{~~~~EXTNAME=r'{\textbackslash}b[a-zA-Z{\_}][{\textbackslash}w{\textbackslash}.]*',~{\#}~Python~name~with~or~without~dots}\\
\mbox{~~~~DOTNAME=r'{\textbackslash}b[a-zA-Z{\_}]{\textbackslash}w*{\textbackslash}.[{\textbackslash}w{\textbackslash}.]*',{\#}~Python~name~with~(at~least~one)~dots}\\
\mbox{~~~~COMMENT=r"{\#}.*?(?={\textbackslash}n)",~{\#}~Python~comment}\\
\mbox{~~~~QUOTED1="'.+?'",~~~~~~~{\#}~single~quoted~string~'}\\
\mbox{~~~~QUOTED2='".+?"',~~~~~~~{\#}~single~quoted~string~"}\\
\mbox{~~~~TRIPLEQ1="'''.+?'''",~~{\#}~triple~quoted~string~'}\\
\mbox{~~~~TRIPLEQ2='""".+?"""'~~~{\#}~triple~quoted~string~"~}\\
\mbox{~~)}\\
\mbox{}\\
\mbox{r.STRING=r.TRIPLEQ1|r.TRIPLEQ2|r.QUOTED1|r.QUOTED2}\\
\mbox{r.CODESEP=r.DOTALL+r.COMMENT.named()|r.STRING.named()}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The trick is in the redefinition of \texttt{{\_}{\_}setattr{\_}{\_}}, which magically converts
all caps attributes in \texttt{Regexp} objects.

The features of \texttt{Regexp} can be tested with the following code:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<test{\_}re.py>}\\
\mbox{}\\
\mbox{"""This~script~looks~at~its~own~source~code~and~extracts~dotted~names,}\\
\mbox{i.e.~names~containing~at~least~one~dot,~such~as~object.attribute~or}\\
\mbox{more~general~one,~such~as~obj.attr.subattr."""}\\
\mbox{}\\
\mbox{{\#}~Notice~that~dotted.names~in~comments~and~literal~strings~are~ignored}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{import~{\_}{\_}main{\_}{\_}}\\
\mbox{}\\
\mbox{text=inspect.getsource({\_}{\_}main{\_}{\_})}\\
\mbox{}\\
\mbox{regexp=Regexp.CODESEP|~Regexp.DOTNAME.named()}\\
\mbox{}\\
\mbox{print~'Using~the~regular~expression',regexp}\\
\mbox{}\\
\mbox{print~"I~have~found~the~following~dotted~names:{\textbackslash}n{\%}s"~{\%}~[}\\
\mbox{~~~~MO.group()~for~MO~in~regexp.finditer(text)~if~MO.lastgroup=='DOTNAME']}\\
\mbox{}\\
\mbox{{\#}</test{\_}re.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Using~the~regular~expression~(?s)(?P<COMMENT>{\#}.*?(?={\textbackslash}n))|(?P<STRING>}\\
\mbox{'''.+?'''|""".+?"""|'.+?'|".+?")|(?P<DOTNAME>[a-zA-Z{\_}]{\textbackslash}w*{\textbackslash}.[{\textbackslash}w{\textbackslash}.]*)}\\
\mbox{I~have~found~the~following~dotted~names:}\\
\mbox{['inspect.getsource',~'Regexp.CODESEP',~'Regexp.DOTNAME.named',~'MO.group',~}\\
\mbox{~'dotname.finditer',~'MO.lastgroup']}
\end{flushleft}\end{ttfamily}
\end{quote}

Now one can define a good \texttt{CodeStr} class with replacing features

Let me consider for instance the solution to the problem discussed in chapter
4, i.e. the definition of a \texttt{TextStr} class able to indent and dedent
blocks of text.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~codeprocess(code,TPO):~{\#}~TPO=text~processing~operator}\\
\mbox{~~~~code=code.replace("{\textbackslash}{\textbackslash}'","{\textbackslash}x01").replace('{\textbackslash}{\textbackslash}"','{\textbackslash}x02')}\\
\mbox{~~~~genblock,out~=~Regexp.CODESEP.generateblocks(code),[]}\\
\mbox{~~~~for~block~in~genblock:}\\
\mbox{~~~~~~~~out.append(TPO(block))}\\
\mbox{~~~~~~~~out.append(genblock.next())}\\
\mbox{~~~~return~''.join(out).replace("{\textbackslash}x01","{\textbackslash}{\textbackslash}'").replace('{\textbackslash}x02','{\textbackslash}{\textbackslash}"')}\\
\mbox{}\\
\mbox{def~quotencode(text):}\\
\mbox{~~~~return~text.replace("{\textbackslash}{\textbackslash}'","{\textbackslash}x01").replace('{\textbackslash}{\textbackslash}"','{\textbackslash}x02')}\\
\mbox{}\\
\mbox{def~quotdecode(text):}\\
\mbox{~~~~return~text.replace("{\textbackslash}x01","{\textbackslash}{\textbackslash}'").replace('{\textbackslash}x02','{\textbackslash}{\textbackslash}"')}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here is an example of usage: replacing 'Print' with 'print' except in
comments and literal strings.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<codeproc.py>}\\
\mbox{}\\
\mbox{from~oopp~import~codeprocess}\\
\mbox{}\\
\mbox{wrongcode=r'''}\\
\mbox{"""Code~processing~example:~replaces~'Print'~with~'print'~except~in}\\
\mbox{comments~and~literal~strings"""}\\
\mbox{Print~"This~program~prints~{\textbackslash}"Hello~World!{\textbackslash}""~{\#}~look~at~this~line!}\\
\mbox{'''}\\
\mbox{}\\
\mbox{fixPrint=lambda~s:~s.replace('Print','print')}\\
\mbox{validcode=codeprocess(wrongcode,fixPrint)}\\
\mbox{}\\
\mbox{print~'Source~code:{\textbackslash}n',validcode}\\
\mbox{print~'Output:{\textbackslash}n';~exec~validcode}\\
\mbox{}\\
\mbox{{\#}</codeproc.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Source~code:}\\
\mbox{}\\
\mbox{"""Code~processing~example:~replaces~'Print'~with~'print'~except~in}\\
\mbox{comments~and~literal~strings"""}\\
\mbox{print~"Prints~{\textbackslash}"Hello~World!{\textbackslash}""~{\#}~look~at~this~line!}\\
\mbox{}\\
\mbox{Output:}\\
\mbox{}\\
\mbox{This~program~prints~"Hello~World!"}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{a-simple-state-machine}{}
\pdfbookmark[1]{A simple state machine}{a-simple-state-machine}
\subsection*{A simple state machine}

Regular expression, however powerful, are limited in scope since they
cannot recognize recursive structures. For instance, they cannot parse
parenthesized expression.

The simplest way to parse a parenthesized expression is to use a state
machine.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{(?:...)~non-grouping}\\
\mbox{(?P<name>...)~}\\
\mbox{}\\
\mbox{(?=...)~look-ahead~}\\
\mbox{(?!...)~negative}\\
\mbox{(?<=...)~look-behind~}\\
\mbox{(?<!...)~negative}\\
\mbox{}\\
\mbox{dec={\textbackslash}}\\
\mbox{"""}\\
\mbox{paren~~~=~(~..~)}\\
\mbox{bracket~=~[~..~]}\\
\mbox{brace~~~=~{\{}~..~{\}}}\\
\mbox{comment~=~{\#}~..~{\textbackslash}n}\\
\mbox{"""}\\
\mbox{}\\
\mbox{actions={\textbackslash}}\\
\mbox{"""}\\
\mbox{reobj1~~:~R'~..~(?<!{\textbackslash}{\textbackslash})'~->~Regexp(r'''~..~''')}\\
\mbox{reobj2~~:~R"~..~(?<!{\textbackslash}{\textbackslash})"~->~Regexp(r"""~..~""")}\\
\mbox{string1~:~(?<!')'(?!')~..~(?<!{\textbackslash}{\textbackslash})'(?!')~->~'''~..~'''}\\
\mbox{string2~:~(?<!")"(?!")~..~(?<!{\textbackslash}{\textbackslash})"(?!")~->~"""~..~"""}\\
\mbox{"""}\\
\mbox{}\\
\mbox{beg=0;~end=1}\\
\mbox{}\\
\mbox{string1[beg]=r"(?<!')'(?!')"~{\#}~an~isolated~single~quote}\\
\mbox{string2[beg]=r'(?<!")"(?!")'~{\#}~an~isolated~double~quote}\\
\mbox{string1[end]=r"(?<!{\textbackslash}{\textbackslash})'(?!')"~{\#}~ending~single~quote}\\
\mbox{string2[end]=r'(?<!{\textbackslash}{\textbackslash})"(?!")'~{\#}~ending~double~quote}\\
\mbox{}\\
\mbox{reobj1[beg]=r"R'"~~~~~~~}\\
\mbox{reobj2[beg]=r'R"'~}\\
\mbox{reobj1[end]=string1[end]~{\#}~ending~single~quote}\\
\mbox{reobj2[end]=string2[end]~{\#}~ending~double~quote}\\
\mbox{}\\
\mbox{actions={\textbackslash}}\\
\mbox{"""}\\
\mbox{reobj1~~:~R'~..~(?<!{\textbackslash}{\textbackslash})'~->~Regexp(r'''~..~''')}\\
\mbox{reobj2~~:~R"~..~(?<!{\textbackslash}{\textbackslash})"~->~Regexp(r"""~..~""")}\\
\mbox{string1~:~(?<!')'(?!')~..~(?<!{\textbackslash}{\textbackslash})'(?!')~->~'''~..~'''}\\
\mbox{string2~:~(?<!")"(?!")~..~(?<!{\textbackslash}{\textbackslash})"(?!")~->~"""~..~"""}\\
\mbox{"""}\\
\mbox{}\\
\mbox{beg={\{}{\}};~end={\{}{\}};~ls=[]}\\
\mbox{for~line~in~decl.splitlines():}\\
\mbox{~~~mode,rest=line.split('~:~')}\\
\mbox{~~~s,r=rest.split('~->~')}\\
\mbox{}\\
\mbox{~beg[mode],end[mode]=s.split('~..~')}\\
\mbox{~ls.append('(?P<beg{\_}{\%}s>{\%}s)'~{\%}~(mode,beg[mode]))}\\
\mbox{~ls.append('(?P<end{\_}{\%}s>{\%}s)'~{\%}~(mode,end[mode]))}\\
\mbox{}\\
\mbox{~beg2[mode],end2[mode]=r.split('~..~')}\\
\mbox{~ls.append(beg2[mode])}\\
\mbox{~ls.append(end2[mode])}\\
\mbox{}\\
\mbox{delimiters='({\%}s)'~{\%}~re.compile('|'.join(ls))}\\
\mbox{splitlist=['']+delimiters.split(source)}\\
\mbox{for~delim,text~in~splitlist:}\\
\mbox{~~~~delimiters.match(delim).lastgroup}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{creating-classes}{}
\pdfbookmark[1]{Creating classes}{creating-classes}
\subsection*{Creating classes}

TODO


%___________________________________________________________________________

\hypertarget{modifying-modules}{}
\pdfbookmark[1]{Modifying modules}{modifying-modules}
\subsection*{Modifying modules}

Metaclasses are extremely
useful since they allows to change the behaviour of the code without
changing the sources. For instance, suppose you have a large library written 
by others that you want to enhance in some way.

Typically, it is always a bad idea to modify the sources, for many reasons:
\begin{itemize}
\item {} 
touching code written by others, you may introduce new bugs;

\item {} 
you may have many scripts that requires the original version 
of the library, not the modified one;

\item {} 
if you change the sources and then you buy the new version of the
library, you have to change the sources again!

\end{itemize}

The solution is to enhance the proprierties of the library at run
time, when the module is imported, by using metaclasses.

To show a concrete example, let me consider the case of the module
\emph{commands} in the Standard Library. This module is Unix-specific,
and cannot be used under Windows. It would be nice to have a
metaclass able to enhance the module in such a way that
when it is invoked on a Windows platform, Windows specific replacement
of the Unix functions provided in the module are used. However,
for sake of brevity, I will only give a metaclasses that display
a nice message in the case we are in a Window platform, without
raising an error (one could easily implement such a behaviour,
however).
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<recognizewindows.py>}\\
\mbox{}\\
\mbox{import~oopp,sys,commands}\\
\mbox{}\\
\mbox{class~WindowsAware(type):}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,*args):~}\\
\mbox{~~~~~~~~if~sys.platform=='win32':~}\\
\mbox{~~~~~~~~~~~~for~key,val~in~vars(cls).iteritems():}\\
\mbox{~~~~~~~~~~~~~~~~if~isinstance(val,staticmethod):}\\
\mbox{~~~~~~~~~~~~~~~~~~~~setattr(cls,key,staticmethod(lambda~*args:~}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~"Sorry,~you~are~(or~I~pretend~you~are)~on~Windows,"}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~~~"~you~cannot~use~the~{\%}s.module"~{\%}~cls.{\_}{\_}name{\_}{\_}))}\\
\mbox{~~~~~~~~~~}\\
\mbox{sys.platform="win32"~{\#}just~in~case~you~are~not~on~Windows}\\
\mbox{}\\
\mbox{commands=oopp.ClsFactory[WindowsAware](commands)}\\
\mbox{}\\
\mbox{print~commands.getoutput('date')~{\#}cannot~be~executed~on~Windows}\\
\mbox{}\\
\mbox{{\#}</recognizewindows.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The output of this script is
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{Sorry,~you~are~on~Windows,~you~cannot~use~the~commands.module}
\end{flushleft}\end{ttfamily}
\end{quote}

However, if you are on Linux and you comment out the line
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{sys.platform="win32"~}
\end{flushleft}\end{ttfamily}
\end{quote}

you will see that the script works.

Notice that the line \texttt{commands=WindowsAware(commands)} actually
converts the 'commands' module in a 'commands' class, but since
the usage is the same, this will fool all programs using the
commands module. In this case the class factory 'WindowsAware'
can also be thought as a module modifier. In this sense, it is
very useful to denote the metaclass with an \emph{adjective}.


%___________________________________________________________________________

\hypertarget{metaclasses-and-attribute-descriptors}{}
\pdfbookmark[1]{Metaclasses and attribute descriptors}{metaclasses-and-attribute-descriptors}
\subsection*{Metaclasses and attribute descriptors}

Descriptors are especially useful in conjunction with metaclasses, since
a custom metaclass can use them as low level tools to modify the methods 
and the attributes of its instances. This allows to implement very 
sophisticated features with few lines of code.

Notice, anyway, that
even plain old function can be thought of as of descriptors.

Descriptors share at least two features with metaclasses:
\newcounter{listcnt33}
\begin{list}{\arabic{listcnt33}.}
{
\usecounter{listcnt33}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
as metaclasses, descriptors are best used as adjectives, since they
are intended to modify and enhance standard methods and attributes, in the
same sense metaclasses modify and enhance standard classes;

\item {} 
as metaclasses, descriptors can change the \emph{semantics} of Python, i.e.
what you see is not necessarely what you get. As such, they are a 
dangerous feature. Use them with judgement!

\end{list}

Now I will show a possible application of properties.
Suppose one has a given class with various kind of
attributes (plain methods, regular methods, static methods,
class methods, properties and data attributes) and she wants
to trace to access to the data attributes (notice that the motivation
for the following problem come from a real question asked in
comp.lang.python). Then one needs to retrieve data
attributes from the class and convert them in properties
controlling their access syntax. The first problem is solved
by a simple function
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~isplaindata(a):}\\
\mbox{~~~~"""A~data~attribute~has~no~{\_}{\_}get{\_}{\_}~or~{\_}{\_}set{\_}{\_}~attributes,~is~not}\\
\mbox{~~~~a~built-in~function,~nor~a~built-in~method."""~}\\
\mbox{~~~~return~not(hasattr(a,'{\_}{\_}get{\_}{\_}')~or~hasattr(a,'{\_}{\_}set{\_}{\_}')}\\
\mbox{~~~~~~~~~~~~~~~or~isinstance(a,BuiltinMethodType)~or}\\
\mbox{~~~~~~~~~~~~~~~isinstance(a,BuiltinFunctionType))}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

whereas the second problem is elegantly solved by a custom metaclass:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<tracedaccess.py>}\\
\mbox{}\\
\mbox{from~oopp~import~isplaindata,inspect}\\
\mbox{}\\
\mbox{class~TracedAccess(type):}\\
\mbox{~~~~"Metaclass~converting~data~attributes~to~properties"}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,name,bases,dic):}\\
\mbox{~~~~~~~~cls.datadic={\{}{\}}}\\
\mbox{~~~~~~~~for~a~in~dic:}\\
\mbox{~~~~~~~~~~~~if~isplaindata(a):}\\
\mbox{~~~~~~~~~~~~~~~~cls.datadic[a]=dic[a]}\\
\mbox{~~~~~~~~~~~~~~~~def~get(self,a=a):}\\
\mbox{~~~~~~~~~~~~~~~~~~~~v=cls.datadic[a]}\\
\mbox{~~~~~~~~~~~~~~~~~~~~print~"Accessing~{\%}s,~value={\%}s"~{\%}~(a,v)}\\
\mbox{~~~~~~~~~~~~~~~~~~~~return~v}\\
\mbox{~~~~~~~~~~~~~~~~def~set(self,v,a=a):}\\
\mbox{~~~~~~~~~~~~~~~~~~~~print~"Setting~{\%}s,~value={\%}s"~{\%}~(a,v)}\\
\mbox{~~~~~~~~~~~~~~~~~~~~cls.datadic[a]=v}\\
\mbox{~~~~~~~~~~~~~~~~setattr(cls,a,property(get,set))}\\
\mbox{}\\
\mbox{class~C(object):}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}~=~TracedAccess}\\
\mbox{~~~~a1='x'}\\
\mbox{}\\
\mbox{class~D(C):~{\#}~shows~that~the~approach~works~well~with~inheritance}\\
\mbox{~~~~a2='y'}\\
\mbox{}\\
\mbox{i=D()}\\
\mbox{i.a1~{\#}~=>~Accessing~a1,~value=x}\\
\mbox{i.a2~{\#}~=>~Accessing~a2,~value=y}\\
\mbox{i.a1='z'~{\#}~=>~Setting~a1,~value=z}\\
\mbox{i.a1~{\#}~=>~Accessing~a1,~value=z}\\
\mbox{}\\
\mbox{{\#}</tracedaccess.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

In this example the metaclass looks at the plain data attributes (recognized
thanks ot the \texttt{isplaindata} function) of its instances and put them
in the dictionary \texttt{cls.datadic}. Then the original attributes are replaced
with property objects tracing the access to them. The solution is a 4-line 
custom metaclass doing the boring job for me:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~Wrapped(Customizable,type):}\\
\mbox{~~~~"""A~customizable~metaclass~to~wrap~methods~with~a~given~wrapper~and}\\
\mbox{~~~~a~given~condition"""}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=Reflective}\\
\mbox{~~~~wrapper=wrappedmethod}\\
\mbox{~~~~condition=lambda~k,v:~True~{\#}~wrap~all}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,*args):}\\
\mbox{~~~~~~~~super(cls.{\_}{\_}this,cls).{\_}{\_}init{\_}{\_}(*args)}\\
\mbox{~~~~~~~~wrap(cls,cls.wrapper,cls.condition.im{\_}func)}\\
\mbox{}\\
\mbox{Traced=Wrapped.With(wrapper=tracedmethod,{\_}{\_}name{\_}{\_}='Traced')}\\
\mbox{Timed=Wrapped.With(wrapper=timedmethod,{\_}{\_}name{\_}{\_}='Timed')}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here is an example of usage:
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> time_=ClsFactory[Traced](time)
>>> print time_.asctime()
[time_] Calling 'asctime' with arguments
(){} ...
-> 'time_.asctime' called with result: Sun May  4 07:30:51 2003
Sun May  4 07:30:51 2003\end{verbatim}
\end{quote}

Another is
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<tracemain.py>}\\
\mbox{}\\
\mbox{from~oopp~import~ClsFactory,Traced,Reflective}\\
\mbox{}\\
\mbox{def~f1(x):~return~x~~~~~{\#}~nested~functions~}\\
\mbox{def~f2(x):~return~f1(x)~{\#}~we~want~to~trace}\\
\mbox{}\\
\mbox{f1orf2=lambda~k,v~:~v~is~f1~or~v~is~f2}\\
\mbox{make=ClsFactory[Reflective,Traced.With(condition=f1orf2)]}\\
\mbox{traced=make('traced',globals())}\\
\mbox{}\\
\mbox{traced.f2('hello!')~{\#}~call~traced.f2}\\
\mbox{}\\
\mbox{{\#}</tracemain.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{[{\_}{\_}main{\_}{\_}]~Calling~'f2'~with~arguments}\\
\mbox{('hello!',){\{}{\}}~...}\\
\mbox{[{\_}{\_}main{\_}{\_}]~Calling~'f1'~with~arguments}\\
\mbox{('hello!',){\{}{\}}~...}\\
\mbox{->~'{\_}{\_}main{\_}{\_}.f1'~called~with~result:~hello!}\\
\mbox{->~'{\_}{\_}main{\_}{\_}.f2'~called~with~result:~hello!}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{id46}{}
\pdfbookmark[1]{Modifying hierarchies}{id46}
\subsection*{Modifying hierarchies}

Suppose one wants to enhance a pre-existing class, for instance
by adding tracing capabilities to it. The problem is non-trivial
since it is not enough to derive a new class from the original
class using the 'Traced' metaclass. For instance, we could imagine of 
tracing the 'Pizza' class introduced in chapter 4 by defining
\begin{quote}
\begin{verbatim}>>> from oopp import *
>>> class TracedTomatoPizza(GenericPizza,WithLogger):
...     __metaclass__=ClsFactory[Traced] 
...     toppinglist=['tomato']\end{verbatim}
\end{quote}

However, this would only trace the methods of the newly defined class,
not of the original one. Since the new class does not introduce any 
non-trivial method, the addition of 'Traced' is practically without
any effect:
\begin{quote}
\begin{verbatim}>>> marinara=TracedTomatoPizza('small') # nothing happens
*****************************************************************************
Tue Apr 15 11:00:17 2003
1. Created small pizza with tomato, cost $ 1.5\end{verbatim}
\end{quote}


%___________________________________________________________________________

\hypertarget{tracing-hierarchies}{}
\pdfbookmark[1]{Tracing hierarchies}{tracing-hierarchies}
\subsection*{Tracing hierarchies}
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<traceH.py>}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{}\\
\mbox{def~wrapMRO(cls,wrapped):}\\
\mbox{~~~~for~c~in~cls.{\_}{\_}mro{\_}{\_}[:-1]:}\\
\mbox{~~~~~~~~wrap(c,wrapped)}\\
\mbox{}\\
\mbox{tracing=tracedmethod.With(logfile=file('trace.txt','w'))}\\
\mbox{wrapMRO(HomoSapiensSapiens,tracing)}\\
\mbox{HomoSapiensSapiens().can()}\\
\mbox{}\\
\mbox{{\#}</traceH.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

with output in trace.txt
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{[HomoSapiensSapiens]~Calling~'can'~with~arguments}\\
\mbox{~(<oopp.HomoSapiensSapiens~object~at~0x4020364c>,){\{}{\}}~...}\\
\mbox{~~~~[HomoSapiens]~Calling~'can'~with~arguments}\\
\mbox{~~~~~(<oopp.HomoSapiensSapiens~object~at~0x4020364c>,){\{}{\}}~...}\\
\mbox{~~~~~~~~[HomoHabilis]~Calling~'can'~with~arguments}\\
\mbox{~~~~~~~~~(<oopp.HomoSapiensSapiens~object~at~0x4020364c>,){\{}{\}}~...}\\
\mbox{~~~~~~~~~~~~[Homo]~Calling~'can'~with~arguments}\\
\mbox{~~~~~~~~~~~~~(<oopp.HomoSapiensSapiens~object~at~0x4020364c>,){\{}{\}}~...}\\
\mbox{~~~~~~~~~~~~~~~~[PrettyPrinted]~Calling~'{\_}{\_}str{\_}{\_}'~with~arguments}\\
\mbox{~~~~~~~~~~~~~~~~~(<oopp.HomoSapiensSapiens~object~at~0x4020364c>,){\{}{\}}~...}\\
\mbox{~~~~~~~~~~~~~~~~[PrettyPrinted.{\_}{\_}str{\_}{\_}]~called~with~result:~}\\
\mbox{~~~~~~~~~~~~~~~~~<HomoSapiensSapiens>}\\
\mbox{~~~~~~~~~~~~[Homo.can]~called~with~result:~None}\\
\mbox{~~~~~~~~[HomoHabilis.can]~called~with~result:~None}\\
\mbox{~~~~[HomoSapiens.can]~called~with~result:~None}\\
\mbox{[HomoSapiensSapiens.can]~called~with~result:~None}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{modifying-source-code}{}
\pdfbookmark[1]{Modifying source code}{modifying-source-code}
\subsection*{Modifying source code}

The real solution would be to derive the original class 'GenericPizza'
from 'Traced' and not from 'object'. One could imagine of creating
a new class inhering from 'Traced' and with all the methods of the
original 'GenericPizza' class; then one should create copies of
all the classes in the whole multiple inheritance hierarchy.
This would be a little annoying, but feasable; the real problem is
that this approach would not work with cooperative methods, since 
cooperative calls in the derived classes would invoked methods in 
the original classes, which are not traced.

This is a case where the modification of the original source code is 
much more appealing and simpler that any other method: it is enough 
to perform a search and replace in the original source code, by adding
the metaclass 'Traced', to enhance the whole multiple inheritance hierarchy.
Let me assume that the hierarchy is contained in a module (which is
typical case). The idea, is to generate \emph{dynamically} a new module from the
modified source code, with a suitable name to avoid conflicts with the
original module. Incredibily enough, this can be done in few lines:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~modulesub(s,r,module):}\\
\mbox{~~~~"Requires~2.3"}\\
\mbox{~~~~name=module.{\_}{\_}name{\_}{\_}}\\
\mbox{~~~~source=inspect.getsource(module).replace(s,r)}\\
\mbox{~~~~dic={\{}name:~module{\}};~exec~source~in~dic~{\#}~exec~the~modified~module}\\
\mbox{~~~~module2=ModuleType(name+'2')~{\#}~creates~an~an~empty~module~}\\
\mbox{~~~~customize(module2,**dic)~{\#}~populates~it~with~dic}\\
\mbox{~~~~return~module2}\\
\mbox{~~}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Notice that the \texttt{sub} function, that modifies the source code of
a given module and returns a modified module, requires Python 2.3
to work. This is a due to a subtle bug in \texttt{exec} in Python 2.2.
Anyway, the restriction to Python 2.3 allows me to take advantage
of one of the most elegant convenience of Python 2.3: the name in
the \texttt{types} module acts are type factories and in particular
\texttt{ModuleType(s)} returns an (empty) module named \texttt{s}.
Here is an example of usage:
\begin{quote}
\begin{verbatim}>>> import oopp
>>> s='GenericPizza(object):'
>>> oopp2=oopp.modulesub(s,s+'\n    __metaclass__=oopp.Traced',oopp) \end{verbatim}
\end{quote}

Name clashes are avoided, being 'oopp2' a different module from 
'oopp'; we have simultaneously access to both the original hierarchy
in 'oopp' (non-traced) and the modified one in 'oopp2' (traced).
In particular 'oopp2.CustomizablePizza' is traced and therefore
\begin{quote}
\begin{verbatim}>>> class PizzaLog(oopp2.CustomizablePizza,oopp2.WithLogger):
...     __metaclass__=makecls()
>>> marinara=PizzaLog.With(toppinglist=['tomato'])('small')\end{verbatim}
\end{quote}

gives the output
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{[PizzaLog]~Calling~'{\_}{\_}init{\_}{\_}'~with~arguments}\\
\mbox{(<oopp.PizzaLog~object~at~0x40470dac>,~'small'){\{}{\}}~...}\\
\mbox{->~'PizzaLog.{\_}{\_}init{\_}{\_}'~called~with~result:~None}\\
\mbox{}\\
\mbox{*****************************************************************************}\\
\mbox{Thu~Mar~27~09:18:28~2003}\\
\mbox{[PizzaLog]~Calling~'{\_}{\_}str{\_}{\_}'~with~arguments}\\
\mbox{(<oopp.PizzaLog~object~at~0x40470dac>,){\{}{\}}~...}\\
\mbox{[PizzaLog]~Calling~'price'~with~arguments}\\
\mbox{(<oopp.PizzaLog~object~at~0x40470dac>,){\{}{\}}~...}\\
\mbox{[PizzaLog]~Calling~'toppings{\_}price'~with~arguments}\\
\mbox{(<oopp.PizzaLog~object~at~0x40470dac>,){\{}{\}}~...}\\
\mbox{->~'PizzaLog.toppings{\_}price'~called~with~result:~0.5}\\
\mbox{}\\
\mbox{->~'PizzaLog.price'~called~with~result:~1.5}\\
\mbox{}\\
\mbox{->~'PizzaLog.{\_}{\_}str{\_}{\_}'~called~with~result:~small~pizza~with~tomato,~cost~{\$}~1.5}\\
\mbox{}\\
\mbox{1.~Created~small~pizza~with~tomato,~cost~{\$}~1.5}
\end{flushleft}\end{ttfamily}
\end{quote}

From that we understand what is happening:
\begin{itemize}
\item {} 
\texttt{PizzaLog.{\_}{\_}init{\_}{\_}} calls \texttt{GenericPizza.{\_}{\_}init{\_}{\_}} that defines size and
cooperatively calls \texttt{WithLogger.{\_}{\_}init{\_}{\_}}

\item {} 
WithLogger.{\_}{\_}init{\_}{\_} cooperatively calls \texttt{WithCounter.{\_}{\_}init{\_}{\_}} 
that increments the count attribute;

\item {} 
at this point, the instruction 'print self' in \texttt{WithLogger.{\_}{\_}init{\_}{\_}} calls 
\texttt{PizzaLog.{\_}{\_}str{\_}{\_}} (inherited from \texttt{GenericPizza.{\_}{\_}str{\_}{\_}});

\item {} 
\texttt{GenericPizza.{\_}{\_}str{\_}{\_}} calls 'price' that in turns calls 
'toppings{\_}price'.

\end{itemize}

On top of that, notice that the metaclass of 'PizzaLog' is
\texttt{{\_}TracedReflective} that has been automagically generated by 
\texttt{makecls} from the metaclasses of 'CustomizablePizza' (i.e. 'Traced')
and of 'WithLogger' (i.e. 'Reflective'); the leading underscore helps 
to understand the dynamical origin of '{\_}TracedReflective'.
It turns out that '{\_}TracedReflective' has a dynamically
generated (meta-meta)class:
\begin{quote}
\begin{verbatim}>>> print type(type(PizzaLog)) #meta-metaclass
<class 'oopp._WithWrappingCapabilitiesReflective'>\end{verbatim}
\end{quote}

Therefore this example has a non-trivial class hierarchy
\begin{quote}
\begin{verbatim}>>> print oopp.MRO(PizzaLog)
MRO of PizzaLog:
  0 - PizzaLog(CustomizablePizza,WithLogger)[Traced]
  1 - CustomizablePizza(GenericPizza,Customizable)[Traced]
  2 - GenericPizza(object)[Traced]
  3 - WithLogger(WithCounter,Customizable,PrettyPrinted)
  4 - WithCounter(object)
  5 - Customizable(object)
  6 - PrettyPrinted(object)
  7 - object()\end{verbatim}
\end{quote}

a non-trivial metaclass hierarchy,
\begin{quote}
\begin{verbatim}>>> print oopp.MRO(type(PizzaLog)) # the metaclass hierarchy
MRO of Traced:
  0 - Traced(Reflective)[WithWrappingCapabilities]
  1 - Reflective(type)
  2 - type(object)
  3 - object()\end{verbatim}
\end{quote}

and a non-trivial meta-metaclass hierarchy:
\begin{quote}
\begin{verbatim}>>> print oopp.MRO(type(type(PizzaLog))) # the meta-metaclass hierarchy
MRO of WithWrappingCapabilities:
  0 - WithWrappingCapabilities(BracketCallable)
  1 - CallableWithBrackets(type)
  2 - type(object)
  3 - object()\end{verbatim}
\end{quote}

Pretty much complicated, isn't it ? ;)

This example is there to show what kind of maintenance one can have
with programs doing a large use of metaclasses, particularly, when
they should be understood by somebody else than the autor ...


%___________________________________________________________________________

\hypertarget{metaclass-regenerated-hierarchies}{}
\pdfbookmark[1]{Metaclass regenerated hierarchies}{metaclass-regenerated-hierarchies}
\subsection*{Metaclass regenerated hierarchies}
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{import~types}\\
\mbox{~~}\\
\mbox{def~hierarchy(self,cls):}\\
\mbox{~~~~d=dict([(t.{\_}{\_}name{\_}{\_},t)~for~t~in~vars(types).itervalues()}\\
\mbox{~~~~~~~~~~~~if~isinstance(t,type)])}\\
\mbox{~~~~def~new(c):}\\
\mbox{~~~~~~~~bases=tuple([d[b.{\_}{\_}name{\_}{\_}]~for~b~in~c.{\_}{\_}bases{\_}{\_}])}\\
\mbox{~~~~~~~~return~self(c.{\_}{\_}name{\_}{\_},~bases,~c.{\_}{\_}dict{\_}{\_}.copy())}\\
\mbox{~~~~mro=list(cls.{\_}{\_}mro{\_}{\_}[:-1])}\\
\mbox{~~~~mro.reverse()}\\
\mbox{~~~~for~c~in~mro:}\\
\mbox{~~~~~~~~if~not~c.{\_}{\_}name{\_}{\_}~in~d:}\\
\mbox{~~~~~~~~~~~~d[c.{\_}{\_}name{\_}{\_}]=new(c)}\\
\mbox{~~~~customize(self,**d)}\\
\mbox{}\\
\mbox{ClsFactory.hierarchy=hierarchy}\\
\mbox{traced=ClsFactory[Traced,Reflective]}
\end{flushleft}\end{ttfamily}
\end{quote}

Unfortunately, this approach does not work if the original hierarchy makes
named cooperative super calls.

Therefore the source-code run-time modification has its advantages.


%___________________________________________________________________________

\hypertarget{the-programmable-programming-language}{}
\pdfbookmark[0]{THE PROGRAMMABLE PROGRAMMING LANGUAGE}{the-programmable-programming-language}
\section*{THE PROGRAMMABLE PROGRAMMING LANGUAGE}
\begin{quote}

\emph{I think that lisp is a better applications language than Python.
However, Python is close enough, or at least so much better than the
alternatives, that Python's social and glue language advantages are
often decisive.}  -- Andy Freeman on c.l.p.
\end{quote}

I go in \emph{really} DEEP BLACK MAGIC here.

Lisp has been called the \emph{programmable programming language} [\hyperlink{id43}{22}]
since its macros allow the  programmer to change the \emph{syntax} of
the language. Python has no macros and the syntax of the language
cannot be changed. Nevertheless, Python metaclasses allows
to change the \emph{semantics} of the language. In this sense, they
are even more powerful and more dangerous than Lisp macros.
Python metaclass allow the user to customize the language (if not
its syntax). This is cool enough, however it can make your programs
unreadable by others. The techniques explained in this
chapter should be used with care. Nevertheless, I trust the judgement
of the programmer who has been able to reach this chapter, and I don't
mind providing him further rope to shoot in his/her foot ;)
\begin{figure}[b]\hypertarget{id48}[24]
Paul Graham, 'OnLisp'
citing
\end{figure}


%___________________________________________________________________________

\hypertarget{enhancing-the-python-language}{}
\pdfbookmark[1]{Enhancing the Python language}{enhancing-the-python-language}
\subsection*{Enhancing the Python language}

Let me start with some minor usage of metaclasses. In this section I
will show how the user can implement in few lines features that are
built-in in other languages, through a minimal usage of metaclasses.

For instance, suppose one wants to define a class which cannot be 
derived: in Java this can be done with the ''final`` keyword. 
In Python there is no need to add a new keyword to the language:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~NonDerivableError(Exception):~pass}\\
\mbox{}\\
\mbox{class~Final(type):~{\#}~better~derived~from~WithCounter,type}\\
\mbox{~~~~"Instances~of~Final~cannot~be~derived"}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(meta,name,bases,dic):}\\
\mbox{~~~~~~~~try:}\\
\mbox{~~~~~~~~~~~~meta.already{\_}called~is~True}\\
\mbox{~~~~~~~~except~AttributeError:~{\#}~not~already~called}\\
\mbox{~~~~~~~~~~~~meta.already{\_}called=True}\\
\mbox{~~~~~~~~~~~~return~super(Final,meta).{\_}{\_}new{\_}{\_}(meta,name,bases,dic)}\\
\mbox{~~~~~~~~else:~{\#}if~already~called}\\
\mbox{~~~~~~~~~~~~raise~NonDerivableError("I~cannot~derive~from~{\%}s"~{\%}~bases)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of usage:
\begin{quote}
\begin{verbatim}>>> from oopp import Final
>>> class C:
...    __metaclass__=Final
...
>>> class D(C): pass #error
...
NonDerivableError: D not created from (<class 'oopp.C'>,)\end{verbatim}
\end{quote}

It is interesting to notice that a similar effect can be reached
with a \texttt{singletonClass} class factory: a 'MetaSingleton' inherits
from \texttt{Singleton} and from 'type' (therefore it is a metaclass):
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{class~S(Singleton,type):~pass}\\
\mbox{singletonClass=ClsFactory[S]}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

If we write
\begin{quote}
\begin{verbatim}>>> from oopp import singletonClass
>>> C=singletonClass()
>>> class D(C):
...    pass\end{verbatim}
\end{quote}

we see that actually 'D' is not a new instance of 'Singleton', but
it coincides with 'C', instead:
\begin{quote}
\begin{verbatim}>>> id(C),id(D)
(135622140, 135622140)
>>> C is D
True
>>> type(C)
<class '__main__._Singleton'>
>>> type(C).__bases__
(<class 'oopp.Singleton'>, <type 'type'>)
>>> c=C(); d=D()
>>> id(c),id(d)
(1075378028, 1075378924)\end{verbatim}
\end{quote}

Notice the order: 'SingletonClass' must inherit from 'Singleton' 
first and from \texttt{Class} second, otherwise the \texttt{Class.{\_}{\_}new{\_}{\_}} method would
override the  \texttt{Singleton.{\_}{\_}new{\_}{\_}}, therefore losing the 'Singleton'
basic property of having only one instance. On the other hand, in
the correct order, 'Singleton' first and 'Class' second, the inheritance
diagram is
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~~~~~~~~~~object~~~5}\\
\mbox{~~~~~~~~~~~~~({\_}{\_}new{\_}{\_})}\\
\mbox{~~~~~~~~~~~~/~~~~~~~~~~{\textbackslash}}\\
\mbox{~~~~~~~~~~~/~~~~~~~~~~~~{\textbackslash}}\\
\mbox{2~~~~~~WithCounter~~~~~~~~~~type~~~~4}\\
\mbox{~~~~~~({\_}{\_}new{\_}{\_})~~~~~~~({\_}{\_}new{\_}{\_})}\\
\mbox{~~~~~~~~~~|~~~~~~~~~~~~~~|}\\
\mbox{~~~~~~~~~~|~~~~~~~~~~~~~~|}\\
\mbox{1~~~~~Singleton~~~~~~~~~Class~~~~3}\\
\mbox{~~~~~~({\_}{\_}new{\_}{\_})~~~~~~~({\_}{\_}new{\_}{\_})}\\
\mbox{~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~{\textbackslash}~~~~~~~~~~~/}\\
\mbox{~~~~~~~~~~~~SingletonClass~~~~0}\\
\mbox{~~~~~~~~~(Singleton.{\_}{\_}new{\_}{\_})}
\end{flushleft}\end{ttfamily}
\begin{ttfamily}\begin{flushleft}
\mbox{~~~~~~object}\\
\mbox{~~~~~/~~~~~{\textbackslash}}\\
\mbox{~~~~/~~~~~~~|}\\
\mbox{WithCounter~~~~~|~}\\
\mbox{~~~~|~~~~~~~|}\\
\mbox{Singleton~~type}\\
\mbox{~~~~~{\textbackslash}~~~~~/}\\
\mbox{~~~~~~{\textbackslash}~~~/}\\
\mbox{~~~MetaSingleton}\\
\mbox{~~~~~~~~:}\\
\mbox{~~~~~~~~:~~~~~~~}\\
\mbox{~~~~~~~~:~~~instantiation}\\
\mbox{~~~~~~~~:}\\
\mbox{~~~~~~~~:}\\
\mbox{~~~~~~C~=~D}
\end{flushleft}\end{ttfamily}
\end{quote}

whereas 'SingletonClass' inherits \texttt{Singleton.{\_}{\_}new{\_}{\_}} which, trough
the \texttt{super} mechanism, calls 'type.{\_}{\_}new{\_}{\_}' and therefore creates
the class 'C'. Notice that class 'D' is never created, it is simply
an alias for 'C'.

I think it is simpler to write down the class 'Final' explicitely
(explicit is better than implicit) as I did; however a fanatic of code
reuse could derive it from 'SingletonClass':
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<final.py>}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{}\\
\mbox{class~Final(Singleton,type):}\\
\mbox{~~~~"Inherits~the~'instance'~attribute~from~Singleton~(default~None)"}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(meta,name,bases,dic):}\\
\mbox{~~~~~~~~if~meta.counter==0:~{\#}~first~call}\\
\mbox{~~~~~~~~~~~~return~super(Final,meta).{\_}{\_}new{\_}{\_}(meta,name,bases,dic)}\\
\mbox{~~~~~~~~else:}\\
\mbox{~~~~~~~~~~~~raise~NonDerivableError("I~cannot~derive~from~{\%}s"~{\%}~bases)}\\
\mbox{~~}\\
\mbox{class~C:~~{\_}{\_}metaclass{\_}{\_}=Final}\\
\mbox{}\\
\mbox{try:}\\
\mbox{~~~~class~D(C):~pass}\\
\mbox{except~NonDerivableError,e:}\\
\mbox{~~~~print~e}\\
\mbox{}\\
\mbox{{\#}</final.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

The reader can check that this script has the correct output 
''I cannot derive from {\textless}class 'oopp.C'{\textgreater}``. I leave to the reader
to understand the issues with trying to implement 'NonDerivable'
from 'NonInstantiable'. {\#}And why an inner metaclass would not work.


%___________________________________________________________________________

\hypertarget{restricting-python-dynamism}{}
\pdfbookmark[1]{Restricting Python dynamism}{restricting-python-dynamism}
\subsection*{Restricting Python dynamism}
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{def~frozen(self,name,value):}\\
\mbox{~~~~if~hasattr(self,name):}\\
\mbox{~~~~~~~~type(self).{\_}{\_}bases{\_}{\_}[0].{\_}{\_}setattr{\_}{\_}(self,name,value)~}\\
\mbox{~~~~else:}\\
\mbox{~~~~~~~~raise~AttributeError("You~cannot~add~attributes~to~{\%}s"~{\%}~self)}\\
\mbox{}\\
\mbox{class~Frozen(object):}\\
\mbox{~~~~"""Subclasses~of~Frozen~are~frozen,~i.e.~it~is~impossibile~to~add}\\
\mbox{~~~~~new~attributes~to~them~and~their~instances"""}\\
\mbox{~~~~{\_}{\_}setattr{\_}{\_}~=~frozen}\\
\mbox{~~~~class~{\_}{\_}metaclass{\_}{\_}(type):}\\
\mbox{~~~~~~~~{\_}{\_}setattr{\_}{\_}~=~frozen}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{}\\
\mbox{{\#}<frozen.py>}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{}\\
\mbox{class~C(Frozen):}\\
\mbox{~~~~c=1}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self):~}\\
\mbox{~~~~~~~~{\#}self.x=5~{\#}~won't~work~anymore,~{\_}{\_}new{\_}{\_}~will~be~okay}\\
\mbox{~~~~~~~~pass}\\
\mbox{}\\
\mbox{class~D(C):}\\
\mbox{~~~~d=2}\\
\mbox{~~}\\
\mbox{C.c=2}\\
\mbox{}\\
\mbox{print~D().d}\\
\mbox{}\\
\mbox{{\#}</frozen.py>}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{changing-the-language-without-changing-the-language}{}
\pdfbookmark[1]{Changing the language without changing the language}{changing-the-language-without-changing-the-language}
\subsection*{Changing the language without changing the language}

In Lisp the user has the possibility of changing the syntax of the 
language to suit her purposes (or simply to fit her taste). 
In Python, the user cannot change the basic grammar of the language,
nevertheless, to a great extent, metaclasses allows to emulate this effect. 
Notice that using metaclasses to this aim is not necessarely 
a good idea, since once you start
changing the Python standard behaviour, it will become impossible for
others  to understand your programs (which is what happened to Lisp ;).

Let me show how metaclasses can be used to provide notational convenience 
(i.e. syntactic sugar) for Python.

As first example, I will show how we may use metaclasses to provide some
convenient notation for staticmethods and classmethods:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{class~MetaSugar(type):}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(cls,name,bases,clsdict):}\\
\mbox{~~~~~~~~for~key,value~in~clsdict.iteritems():}\\
\mbox{~~~~~~~~~~~~if~key.startswith("static{\_}"):}\\
\mbox{~~~~~~~~~~~~~~~~setattr(cls,key[7:],staticmethod(value))}\\
\mbox{~~~~~~~~~~~~elif~key.startwith("class{\_}"):}\\
\mbox{~~~~~~~~~~~~~~~~setattr(cls,key[6:],classmethod(value))}
\end{flushleft}\end{ttfamily}
\end{quote}

The same effect can be obtained trough normal inheritance
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{class~SyntacticSugar(object):}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self):}\\
\mbox{~~~~~~~~for~k,v~in~self.{\_}{\_}class{\_}{\_}.{\_}{\_}dict{\_}{\_}.iteritems():}\\
\mbox{~~~~~~~~~~~~if~k.startswith('static{\_}'):}\\
\mbox{~~~~~~~~~~~~~~~~self.{\_}{\_}class{\_}{\_}.{\_}{\_}dict{\_}{\_}[k[7:]]~=~staticmethod(v)}\\
\mbox{~~~~~~~~~~~~if~k.startswith('static{\_}'):}\\
\mbox{~~~~~~~~~~~~~~~~self.{\_}{\_}class{\_}{\_}.{\_}{\_}dict{\_}{\_}[k[7:]]~=~staticmethod(v)}
\end{flushleft}\end{ttfamily}
\end{quote}

Let me now implement some syntactic sugar for the {\_}{\_}metaclass{\_}{\_} hook.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}<oopp.py>}\\
\mbox{}\\
\mbox{import~re}\\
\mbox{squarednames=re.compile('{\textbackslash}[([A-Za-z{\_}][{\textbackslash}w{\textbackslash}.,~]*){\textbackslash}]')}\\
\mbox{}\\
\mbox{def~inferredfromdocstring(name,bases,dic):}\\
\mbox{~~~~docstring=dic['{\_}{\_}doc{\_}{\_}']}\\
\mbox{~~~~match=squarednames.match(docstring)}\\
\mbox{~~~~if~not~match:~return~ClsFactory[Reflective](name,bases,dic)}\\
\mbox{~~~~metanames=[name.strip()~for~name~in~match.group(1).split(',')]}\\
\mbox{~~~~metaname=''.join(metanames)~~}\\
\mbox{~~~~if~len(metanames)>1:~{\#}~creates~a~new~metaclass}\\
\mbox{~~~~~~~~metaclass=type(metaname,tuple(map(eval,metanames)),{\{}{\}})}\\
\mbox{~~~~else:}\\
\mbox{~~~~~~~~metaclass=eval(metaname)}\\
\mbox{~~~~return~ClsFactory[metaclass](name,bases,dic)}\\
\mbox{}\\
\mbox{{\#}</oopp.py>}\\
\mbox{}\\
\mbox{{\#}<sugar.py>}\\
\mbox{}\\
\mbox{from~oopp~import~*}\\
\mbox{{\_}{\_}metaclass{\_}{\_}~=~inferredfromdocstring}\\
\mbox{class~B:}\\
\mbox{~~~~"Do~nothing~class"}\\
\mbox{}\\
\mbox{class~C:~}\\
\mbox{~~~~"[Reflective]"}\\
\mbox{~~~~"~Do~nothing~class"}\\
\mbox{}\\
\mbox{class~D:}\\
\mbox{~~~~"[WithLogger,Final]"}\\
\mbox{~~~~"Do~nothing~class"}\\
\mbox{}\\
\mbox{class~E(C):}\\
\mbox{~~~~pass}\\
\mbox{}\\
\mbox{{\#}</sugar.py>}
\end{flushleft}\end{ttfamily}
\end{quote}

With output:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{*****************************************************************************}\\
\mbox{Fri~Feb~21~09:35:58~2003}\\
\mbox{Creating~class~Logged{\_}C~descending~from~(),}\\
\mbox{instance~of~<class~'oopp.Logged'>}\\
\mbox{}\\
\mbox{Logged{\_}C~dictionary:}\\
\mbox{~{\_}{\_}doc{\_}{\_}~=~Do~nothing~class}\\
\mbox{*****************************************************************************}\\
\mbox{Fri~Feb~21~09:35:58~2003}\\
\mbox{Creating~class~Logged{\_}Final{\_}D~descending~from~(),}\\
\mbox{instance~of~<class~'oopp.LoggedFinal'>}\\
\mbox{}\\
\mbox{Logged{\_}Final{\_}D~dictionary:}\\
\mbox{{\_}{\_}doc{\_}{\_}~=~Do~nothing~class}\\
\mbox{*****************************************************************************}\\
\mbox{Fri~Feb~21~09:35:58~2003}\\
\mbox{Creating~class~E~descending~from~(<class~'oopp.Logged{\_}C'>,),}\\
\mbox{instance~of~<class~'oopp.Logged'>}\\
\mbox{}\\
\mbox{E~dictionary:}\\
\mbox{<EMPTY>~}
\end{flushleft}\end{ttfamily}
\end{quote}

At the end, let me point out few observations:
Metaclasses can be used to provide syntactic sugar, as I have shown
in the previous example. However, I have given the previous
routines as a proof of concept: I do \emph{not} use these routines in
my actual code for many good reasons:
\newcounter{listcnt34}
\begin{list}{\arabic{listcnt34}.}
{
\usecounter{listcnt34}
\setlength{\rightmargin}{\leftmargin}
}
\item {} 
At the end a convenient notation will be provided in Python 2.4

\item {} 
I don't want to use magic tricks on my code, I want others to
be able to understand what the code is doing;

\item {} 
I want to be able myself to understand my own code in six months
from today ;)

\end{list}

Anyway, I think it is a good thing to know about this potentiality
of metaclasses, that can turn out to be very convenient in certain
applications: but this does not mean that should be blindly used
and/or abused. In other words: with great powers come 
great responsabilities ;)


%___________________________________________________________________________

\hypertarget{recognizing-magic-comments}{}
\pdfbookmark[1]{Recognizing magic comments}{recognizing-magic-comments}
\subsection*{Recognizing magic comments}

In this section, I will begin to unravel the secrets of the black magic art 
of changing Python semantics and I will show that with few lines 
involving metaclasses
and the standard library 'inspect' module, even comments can be made
significant! (let me continue with my series ''how to do what should not
be done``).

To this aim, I need a brief digression on regular expressions.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{class~RecognizesMagicComments(object):}\\
\mbox{~~~form=r'def~{\%}s(NAME)(args):{\#}!{\textbackslash}s?staticmethod'}\\
\mbox{~~~class~{\_}{\_}metaclass{\_}{\_}(type):}\\
\mbox{~~~~~~~def~{\_}{\_}new{\_}{\_}(meta,name,bases,dic):}\\
\mbox{~~~~~~~~~~~code=[]}\\
\mbox{~~~~~~~~~~~for~attr~in~dic:}\\
\mbox{~~~~~~~~~~~~~~~source=inspect.getsource(dic[attr]).splitlines()}\\
\mbox{~~~~~~~~~~~~~~~for~line~in~source:}\\
\mbox{~~~~~~~~~~~~~~~~~~~split=line.split('{\#}!')}\\
\mbox{~~~~~~~~~~~~~~~~~~~if~len(split)==2:}\\
\mbox{~~~~~~~~~~~~~~~~~~~~~~~descriptor=split[1];~code.append(split[0])}\\
\mbox{~~~~~~~~~~~~~~~~~~~else:~code.append(line)}\\
\mbox{~~~~~~~~~~~~~}\\
\mbox{class~C(RecognizesMagicComments):}\\
\mbox{~~~~{\#}!staticmethod}\\
\mbox{~~~~def~f(x):~{\#}!staticmethod}\\
\mbox{~~~~~~~~return~x}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{interpreting-python-source-code-on-the-fly}{}
\pdfbookmark[1]{Interpreting Python source code on the fly}{interpreting-python-source-code-on-the-fly}
\subsection*{Interpreting Python source code on the fly}

At this point, I can really go \emph{DEEP} in black magic.
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{import~sys,~inspect,~linecache,~re}\\
\mbox{}\\
\mbox{def~cls{\_}source(name,module):}\\
\mbox{~~~~lines~=~linecache.getlines(inspect.getsourcefile(module))}\\
\mbox{~~~~if~not~lines:~raise~IOError,~'could~not~get~source~code'}\\
\mbox{~~~~pat~=~re.compile(r'{\textasciicircum}{\textbackslash}s*class{\textbackslash}s*'~+~name~+~r'{\textbackslash}b')}\\
\mbox{~~~~for~i~in~range(len(lines)):}\\
\mbox{~~~~~~~~if~pat.match(lines[i]):~break}\\
\mbox{~~~~else:~raise~IOError,~'could~not~find~class~definition'}\\
\mbox{~~~~lines,~lnum~=~inspect.getblock(lines[i:]),~i~+~1}\\
\mbox{~~~~return~''.join(lines)}\\
\mbox{}\\
\mbox{class~Interpreter(object):}\\
\mbox{~~~~def~{\_}{\_}init{\_}{\_}(self,CPO):~{\#}~possible~composition~of~code~processing~opers}\\
\mbox{~~~~~~~~self.repl=CPO}\\
\mbox{~~~~def~{\_}{\_}call{\_}{\_}(self,name,bases,dic):}\\
\mbox{~~~~~~~~try:}\\
\mbox{~~~~~~~~~~~modulename=dic['{\_}{\_}module{\_}{\_}']~{\#}~module~where~the~class~is~defined}\\
\mbox{~~~~~~~~except~KeyError:~{\#}~no~{\_}{\_}module{\_}{\_}~attribute}\\
\mbox{~~~~~~~~~~~raise~IOError("Class~{\%}s~cannot~be~defined~dynamically~or~in~the{\textbackslash}n"}\\
\mbox{~~~~~~~~~~~"interpreter~and~the~source~code~cannot~came~from~a~pipe"{\%}~name)}\\
\mbox{~~~~~~~~module=sys.modules[modulename]~}\\
\mbox{~~~~~~~~source=self.repl(cls{\_}source(name,module))}\\
\mbox{~~~~~~~~source=re.sub('{\_}{\_}metaclass{\_}{\_}=.*','{\_}{\_}metaclass{\_}{\_}=type',source)}\\
\mbox{~~~~~~~~{\#}print~source}\\
\mbox{~~~~~~~~loc={\{}{\}};~exec~source~in~vars(module),loc}\\
\mbox{~~~~~~~~return~loc[name]}\\
\mbox{}\\
\mbox{regexp{\_}expand=Interpreter(regexp)}
\end{flushleft}\end{ttfamily}
\end{quote}


%___________________________________________________________________________

\hypertarget{implementing-lazy-evaluation}{}
\pdfbookmark[1]{Implementing lazy evaluation}{implementing-lazy-evaluation}
\subsection*{Implementing lazy evaluation}

At this point of our knowledge, it becomes trivial to implement lazy 
evaluation and then a ternary operator. (My original, simpler, implementation
is posted on c.l.p.; see the thread 'PEP 312 (and thus 308) implemented 
with a black magic trick')


%___________________________________________________________________________

\hypertarget{implementing-a-ternary-operator}{}
\pdfbookmark[1]{Implementing a ternary operator}{implementing-a-ternary-operator}
\subsection*{Implementing a ternary operator}
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{{\#}~module~ternary.py}\\
\mbox{}\\
\mbox{"PEP~308~and~312~implemented~via~a~metaclass-powered~dirty~trick"}\\
\mbox{}\\
\mbox{import~inspect,{\_}{\_}main{\_}{\_}}\\
\mbox{}\\
\mbox{{\#}~the~ternary~operator:}\\
\mbox{}\\
\mbox{def~if{\_}(cond,f,g):}\\
\mbox{~~~~"Short~circuiting~ternary~operator~implemented~via~lambdas"}\\
\mbox{~~~~if~cond:~return~f()}\\
\mbox{~~~~else:~return~g()}\\
\mbox{}\\
\mbox{{\#}~the~metaclass~black~magic:}\\
\mbox{}\\
\mbox{class~DirtyTrick(type):}\\
\mbox{~~~~"""Cooperative~metaclass~that~looks~at~the~source~code~of~its~instances~}\\
\mbox{~~~~and~replaces~the~string~'{\textasciitilde}'~with~'lambda~:'~before~the~class~creation"""}\\
\mbox{~~~~def~{\_}{\_}new{\_}{\_}(meta,name,bases,dic):}\\
\mbox{~~~~~~~~for~attr~in~dic.values():}\\
\mbox{~~~~~~~~~~~~if~inspect.isfunction(attr):~}\\
\mbox{~~~~~~~~~~~~~~~~code=inspect.getsource(attr)}\\
\mbox{~~~~~~~~~~~~~~~~if~code.find('{\textasciitilde}')==-1:~continue~{\#}~no~'{\textasciitilde}'~found,~skip}\\
\mbox{~~~~~~~~~~~~~~~~code=code.replace('{\textasciitilde}','lambda~:')}\\
\mbox{~~~~~~~~~~~~~~~~code=dedent(code)+'{\textbackslash}n'}\\
\mbox{~~~~~~~~~~~~~~~~exec~code~in~{\_}{\_}main{\_}{\_}.{\_}{\_}dict{\_}{\_},dic~{\#}~modifies~dic}\\
\mbox{~~~~~~~~return~super(DirtyTrick,meta).{\_}{\_}new{\_}{\_}(meta,name,bases,dic)}\\
\mbox{}\\
\mbox{{\#}~a~convenient~base~class:}\\
\mbox{}\\
\mbox{class~RecognizesImplicitLambdas:}\\
\mbox{~~~~"Children~of~this~class~do~recognize~implicit~lambdas"}\\
\mbox{~~~~{\_}{\_}metaclass{\_}{\_}=DirtyTrick}
\end{flushleft}\end{ttfamily}
\end{quote}

Here there is an example of usage:
\begin{quote}
\begin{ttfamily}\begin{flushleft}
\mbox{from~ternary~import~if{\_},~RecognizesImplicitLambdas}\\
\mbox{from~math~import~sqrt}\\
\mbox{}\\
\mbox{class~C(RecognizesImplicitLambdas):}\\
\mbox{~~~def~safesqrt(self,x):}\\
\mbox{~~~~~~~~return~if{\_}(~x>0,~{\textasciitilde}sqrt(x),~{\textasciitilde}0)~{\#}short-circuiting~ternary~operator}\\
\mbox{}\\
\mbox{c=C()}\\
\mbox{print~c.safesqrt(4),~c.safesqrt(-4)~}
\end{flushleft}\end{ttfamily}
\end{quote}

\end{document}