File: FAQ.html

package info (click to toggle)
python2.7 2.7.13-2%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 77,372 kB
  • sloc: python: 447,713; ansic: 441,673; sh: 17,442; asm: 14,304; makefile: 4,795; objc: 761; exp: 499; cpp: 128; xml: 74
file content (8997 lines) | stat: -rw-r--r-- 360,507 bytes parent folder | download | duplicates (40)
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
<HTML>
<HEAD>
<TITLE>The Whole Python FAQ</TITLE>
</HEAD>

<BODY BACKGROUND="http://www.python.org/pics/RedShort.gif"
      BGCOLOR="#FFFFFF"
      TEXT="#000000"
      LINK="#AA0000"
      VLINK="#906A6A">
<H1>The Whole Python FAQ</H1>
Last changed on Wed Feb 12 21:31:08 2003 CET

<P>(Entries marked with ** were changed within the last 24 hours;
entries marked with * were changed within the last 7 days.)
<P>

<P>
<HR>
<H2>1. General information and availability</H2>
<UL>
<LI><A HREF="#1.1">1.1. What is Python?</A>
<LI><A HREF="#1.2">1.2. Why is it called Python?</A>
<LI><A HREF="#1.3">1.3. How do I obtain a copy of the Python source?</A>
<LI><A HREF="#1.4">1.4. How do I get documentation on Python?</A>
<LI><A HREF="#1.5">1.5. Are there other ftp sites that mirror the Python distribution?</A>
<LI><A HREF="#1.6">1.6. Is there a newsgroup or mailing list devoted to Python?</A>
<LI><A HREF="#1.7">1.7. Is there a WWW page devoted to Python?</A>
<LI><A HREF="#1.8">1.8. Is the Python documentation available on the WWW?</A>
<LI><A HREF="#1.9">1.9. Are there any books on Python?</A>
<LI><A HREF="#1.10">1.10. Are there any published articles about Python that I can reference?</A>
<LI><A HREF="#1.11">1.11. Are there short introductory papers or talks on Python?</A>
<LI><A HREF="#1.12">1.12. How does the Python version numbering scheme work?</A>
<LI><A HREF="#1.13">1.13. How do I get a beta test version of Python?</A>
<LI><A HREF="#1.14">1.14. Are there copyright restrictions on the use of Python?</A>
<LI><A HREF="#1.15">1.15. Why was Python created in the first place?</A>
<LI><A HREF="#1.16">1.16. Do I have to like &quot;Monty Python's Flying Circus&quot;?</A>
<LI><A HREF="#1.17">1.17. What is Python good for?</A>
<LI><A HREF="#1.18">1.18. Can I use the FAQ Wizard software to maintain my own FAQ?</A>
<LI><A HREF="#1.19">1.19. Which editor has good support for editing Python source code?</A>
<LI><A HREF="#1.20">1.20. I've never programmed before. Is there a Python tutorial?</A>
<LI><A HREF="#1.21">1.21. Where in the world is www.python.org located?</A>

</UL>

<P>
<HR>
<H2>2. Python in the real world</H2>
<UL>
<LI><A HREF="#2.1">2.1. How many people are using Python?</A>
<LI><A HREF="#2.2">2.2. Have any significant projects been done in Python?</A>
<LI><A HREF="#2.3">2.3. Are there any commercial projects going on using Python?</A>
<LI><A HREF="#2.4">2.4. How stable is Python?</A>
<LI><A HREF="#2.5">2.5. What new developments are expected for Python in the future?</A>
<LI><A HREF="#2.6">2.6. Is it reasonable to propose incompatible changes to Python?</A>
<LI><A HREF="#2.7">2.7. What is the future of Python?</A>
<LI><A HREF="#2.8">2.8. What was the PSA, anyway?</A>
<LI><A HREF="#2.9">2.9. Deleted</A>
<LI><A HREF="#2.10">2.10. Deleted</A>
<LI><A HREF="#2.11">2.11. Is Python Y2K (Year 2000) Compliant?</A>
<LI><A HREF="#2.12">2.12. Is Python a good language in a class for beginning programmers?</A>

</UL>

<P>
<HR>
<H2>3. Building Python and Other Known Bugs</H2>
<UL>
<LI><A HREF="#3.1">3.1. Is there a test set?</A>
<LI><A HREF="#3.2">3.2. When running the test set, I get complaints about floating point operations, but when playing with floating point operations I cannot find anything wrong with them.</A>
<LI><A HREF="#3.3">3.3. Link errors after rerunning the configure script.</A>
<LI><A HREF="#3.4">3.4. The python interpreter complains about options passed to a script (after the script name).</A>
<LI><A HREF="#3.5">3.5. When building on the SGI, make tries to run python to create glmodule.c, but python hasn't been built or installed yet.</A>
<LI><A HREF="#3.6">3.6. I use VPATH but some targets are built in the source directory.</A>
<LI><A HREF="#3.7">3.7. Trouble building or linking with the GNU readline library.</A>
<LI><A HREF="#3.8">3.8. Trouble with socket I/O on older Linux 1.x versions.</A>
<LI><A HREF="#3.9">3.9. Trouble with prototypes on Ultrix.</A>
<LI><A HREF="#3.10">3.10. Other trouble building Python on platform X.</A>
<LI><A HREF="#3.11">3.11. How to configure dynamic loading on Linux.</A>
<LI><A HREF="#3.12">3.12. I can't get shared modules to work on Linux 2.0 (Slackware96)?</A>
<LI><A HREF="#3.13">3.13. Trouble when making modules shared on Linux.</A>
<LI><A HREF="#3.14">3.14. [deleted]</A>
<LI><A HREF="#3.15">3.15. Errors when linking with a shared library containing C++ code.</A>
<LI><A HREF="#3.16">3.16. Deleted</A>
<LI><A HREF="#3.17">3.17. Deleted.</A>
<LI><A HREF="#3.18">3.18. Compilation or link errors for the _tkinter module</A>
<LI><A HREF="#3.19">3.19. I configured and built Python for Tcl/Tk but &quot;import Tkinter&quot; fails.</A>
<LI><A HREF="#3.20">3.20. [deleted]</A>
<LI><A HREF="#3.21">3.21. Several common system calls are missing from the posix module.</A>
<LI><A HREF="#3.22">3.22. ImportError: No module named string, on MS Windows.</A>
<LI><A HREF="#3.23">3.23. Core dump on SGI when using the gl module.</A>
<LI><A HREF="#3.24">3.24. &quot;Initializer not a constant&quot; while building DLL on MS-Windows</A>
<LI><A HREF="#3.25">3.25. Output directed to a pipe or file disappears on Linux.</A>
<LI><A HREF="#3.26">3.26. [deleted]</A>
<LI><A HREF="#3.27">3.27. [deleted]</A>
<LI><A HREF="#3.28">3.28. How can I test if Tkinter is working?</A>
<LI><A HREF="#3.29">3.29. Is there a way to get the interactive mode of the python interpreter to perform function/variable name completion?</A>
<LI><A HREF="#3.30">3.30. Why is the Python interpreter not built as a shared library?</A>
<LI><A HREF="#3.31">3.31. Build with GCC on Solaris 2.6 (SunOS 5.6) fails</A>
<LI><A HREF="#3.32">3.32. Running &quot;make clean&quot; seems to leave problematic files that cause subsequent builds to fail.</A>
<LI><A HREF="#3.33">3.33. Submitting bug reports and patches</A>
<LI><A HREF="#3.34">3.34. I can't load shared libraries under Python 1.5.2, Solaris 7, and gcc 2.95.2</A>
<LI><A HREF="#3.35">3.35. In the regression test, test___all__ fails for the profile module. What's wrong?</A>
<LI><A HREF="#3.36">3.36. relocations remain against allocatable but non-writable sections</A>

</UL>

<P>
<HR>
<H2>4. Programming in Python</H2>
<UL>
<LI><A HREF="#4.1">4.1. Is there a source code level debugger with breakpoints, step, etc.?</A>
<LI><A HREF="#4.2">4.2. Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)? (Also phrased as: Can I use a built-in type as base class?)</A>
<LI><A HREF="#4.3">4.3. Is there a curses/termcap package for Python?</A>
<LI><A HREF="#4.4">4.4. Is there an equivalent to C's onexit() in Python?</A>
<LI><A HREF="#4.5">4.5. [deleted]</A>
<LI><A HREF="#4.6">4.6. How do I iterate over a sequence in reverse order?</A>
<LI><A HREF="#4.7">4.7. My program is too slow. How do I speed it up?</A>
<LI><A HREF="#4.8">4.8. When I have imported a module, then edit it, and import it again (into the same Python process), the changes don't seem to take place. What is going on?</A>
<LI><A HREF="#4.9">4.9. How do I find the current module name?</A>
<LI><A HREF="#4.10">4.10. I have a module in which I want to execute some extra code when it is run as a script. How do I find out whether I am running as a script?</A>
<LI><A HREF="#4.11">4.11. I try to run a program from the Demo directory but it fails with ImportError: No module named ...; what gives?</A>
<LI><A HREF="#4.12">4.12. [deleted]</A>
<LI><A HREF="#4.13">4.13. What GUI toolkits exist for Python?</A>
<LI><A HREF="#4.14">4.14. Are there any interfaces to database packages in Python?</A>
<LI><A HREF="#4.15">4.15. Is it possible to write obfuscated one-liners in Python?</A>
<LI><A HREF="#4.16">4.16. Is there an equivalent of C's &quot;?:&quot; ternary operator?</A>
<LI><A HREF="#4.17">4.17. My class defines __del__ but it is not called when I delete the object.</A>
<LI><A HREF="#4.18">4.18. How do I change the shell environment for programs called using os.popen() or os.system()? Changing os.environ doesn't work.</A>
<LI><A HREF="#4.19">4.19. What is a class?</A>
<LI><A HREF="#4.20">4.20. What is a method?</A>
<LI><A HREF="#4.21">4.21. What is self?</A>
<LI><A HREF="#4.22">4.22. What is an unbound method?</A>
<LI><A HREF="#4.23">4.23. How do I call a method defined in a base class from a derived class that overrides it?</A>
<LI><A HREF="#4.24">4.24. How do I call a method from a base class without using the name of the base class?</A>
<LI><A HREF="#4.25">4.25. How can I organize my code to make it easier to change the base class?</A>
<LI><A HREF="#4.26">4.26. How can I find the methods or attributes of an object?</A>
<LI><A HREF="#4.27">4.27. I can't seem to use os.read() on a pipe created with os.popen().</A>
<LI><A HREF="#4.28">4.28. How can I create a stand-alone binary from a Python script?</A>
<LI><A HREF="#4.29">4.29. What WWW tools are there for Python?</A>
<LI><A HREF="#4.30">4.30. How do I run a subprocess with pipes connected to both input and output?</A>
<LI><A HREF="#4.31">4.31. How do I call a function if I have the arguments in a tuple?</A>
<LI><A HREF="#4.32">4.32. How do I enable font-lock-mode for Python in Emacs?</A>
<LI><A HREF="#4.33">4.33. Is there a scanf() or sscanf() equivalent?</A>
<LI><A HREF="#4.34">4.34. Can I have Tk events handled while waiting for I/O?</A>
<LI><A HREF="#4.35">4.35. How do I write a function with output parameters (call by reference)?</A>
<LI><A HREF="#4.36">4.36. Please explain the rules for local and global variables in Python.</A>
<LI><A HREF="#4.37">4.37. How can I have modules that mutually import each other?</A>
<LI><A HREF="#4.38">4.38. How do I copy an object in Python?</A>
<LI><A HREF="#4.39">4.39. How to implement persistent objects in Python? (Persistent == automatically saved to and restored from disk.)</A>
<LI><A HREF="#4.40">4.40. I try to use __spam and I get an error about _SomeClassName__spam.</A>
<LI><A HREF="#4.41">4.41. How do I delete a file? And other file questions.</A>
<LI><A HREF="#4.42">4.42. How to modify urllib or httplib to support HTTP/1.1?</A>
<LI><A HREF="#4.43">4.43. Unexplicable syntax errors in compile() or exec.</A>
<LI><A HREF="#4.44">4.44. How do I convert a string to a number?</A>
<LI><A HREF="#4.45">4.45. How do I convert a number to a string?</A>
<LI><A HREF="#4.46">4.46. How do I copy a file?</A>
<LI><A HREF="#4.47">4.47. How do I check if an object is an instance of a given class or of a subclass of it?</A>
<LI><A HREF="#4.48">4.48. What is delegation?</A>
<LI><A HREF="#4.49">4.49. How do I test a Python program or component.</A>
<LI><A HREF="#4.50">4.50. My multidimensional list (array) is broken! What gives?</A>
<LI><A HREF="#4.51">4.51. I want to do a complicated sort: can you do a Schwartzian Transform in Python?</A>
<LI><A HREF="#4.52">4.52. How to convert between tuples and lists?</A>
<LI><A HREF="#4.53">4.53. Files retrieved with urllib contain leading garbage that looks like email headers.</A>
<LI><A HREF="#4.54">4.54. How do I get a list of all instances of a given class?</A>
<LI><A HREF="#4.55">4.55. A regular expression fails with regex.error: match failure.</A>
<LI><A HREF="#4.56">4.56. I can't get signal handlers to work.</A>
<LI><A HREF="#4.57">4.57. I can't use a global variable in a function? Help!</A>
<LI><A HREF="#4.58">4.58. What's a negative index? Why doesn't list.insert() use them?</A>
<LI><A HREF="#4.59">4.59. How can I sort one list by values from another list?</A>
<LI><A HREF="#4.60">4.60. Why doesn't dir() work on builtin types like files and lists?</A>
<LI><A HREF="#4.61">4.61. How can I mimic CGI form submission (METHOD=POST)?</A>
<LI><A HREF="#4.62">4.62. If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?</A>
<LI><A HREF="#4.63">4.63. How do I make a Python script executable on Unix?</A>
<LI><A HREF="#4.64">4.64. How do you remove duplicates from a list?</A>
<LI><A HREF="#4.65">4.65. Are there any known year 2000 problems in Python?</A>
<LI><A HREF="#4.66">4.66. I want a version of map that applies a method to a sequence of objects! Help!</A>
<LI><A HREF="#4.67">4.67. How do I generate random numbers in Python?</A>
<LI><A HREF="#4.68">4.68. How do I access the serial (RS232) port?</A>
<LI><A HREF="#4.69">4.69. Images on Tk-Buttons don't work in Py15?</A>
<LI><A HREF="#4.70">4.70. Where is the math.py (socket.py, regex.py, etc.) source file?</A>
<LI><A HREF="#4.71">4.71. How do I send mail from a Python script?</A>
<LI><A HREF="#4.72">4.72. How do I avoid blocking in connect() of a socket?</A>
<LI><A HREF="#4.73">4.73. How do I specify hexadecimal and octal integers?</A>
<LI><A HREF="#4.74">4.74. How to get a single keypress at a time?</A>
<LI><A HREF="#4.75">4.75. How can I overload constructors (or methods) in Python?</A>
<LI><A HREF="#4.76">4.76. How do I pass keyword arguments from one method to another?</A>
<LI><A HREF="#4.77">4.77. What module should I use to help with generating HTML?</A>
<LI><A HREF="#4.78">4.78. How do I create documentation from doc strings?</A>
<LI><A HREF="#4.79">4.79. How do I read (or write) binary data?</A>
<LI><A HREF="#4.80">4.80. I can't get key bindings to work in Tkinter</A>
<LI><A HREF="#4.81">4.81. &quot;import crypt&quot; fails</A>
<LI><A HREF="#4.82">4.82. Are there coding standards or a style guide for Python programs?</A>
<LI><A HREF="#4.83">4.83. How do I freeze Tkinter applications?</A>
<LI><A HREF="#4.84">4.84. How do I create static class data and static class methods?</A>
<LI><A HREF="#4.85">4.85. __import__('x.y.z') returns &lt;module 'x'&gt;; how do I get z?</A>
<LI><A HREF="#4.86">4.86. Basic thread wisdom</A>
<LI><A HREF="#4.87">4.87. Why doesn't closing sys.stdout (stdin, stderr) really close it?</A>
<LI><A HREF="#4.88">4.88. What kinds of global value mutation are thread-safe?</A>
<LI><A HREF="#4.89">4.89. How do I modify a string in place?</A>
<LI><A HREF="#4.90">4.90. How to pass on keyword/optional parameters/arguments</A>
<LI><A HREF="#4.91">4.91. How can I get a dictionary to display its keys in a consistent order?</A>
<LI><A HREF="#4.92">4.92. Is there a Python tutorial?</A>
<LI><A HREF="#4.93">4.93. Deleted</A>
<LI><A HREF="#4.94">4.94. How do I get a single keypress without blocking?</A>
<LI><A HREF="#4.95">4.95. Is there an equivalent to Perl chomp()? (Remove trailing newline from string)</A>
<LI><A HREF="#4.96">4.96. Why is join() a string method when I'm really joining the elements of a (list, tuple, sequence)?</A>
<LI><A HREF="#4.97">4.97. How can my code discover the name of an object?</A>
<LI><A HREF="#4.98">4.98. Why are floating point calculations so inaccurate?</A>
<LI><A HREF="#4.99">4.99. I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, 'Invalid argument'). Help! How can I restore my data?</A>
<LI><A HREF="#4.100">4.100. What are the &quot;best practices&quot; for using import in a module?</A>
<LI><A HREF="#4.101">4.101. Is there a tool to help find bugs or perform static analysis?</A>
<LI><A HREF="#4.102">4.102. UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)</A>
<LI><A HREF="#4.103">4.103. Using strings to call functions/methods</A>
<LI><A HREF="#4.104">4.104. How fast are exceptions?</A>
<LI><A HREF="#4.105">4.105. Sharing global variables across modules</A>
<LI><A HREF="#4.106">4.106. Why is cPickle so slow?</A>
<LI><A HREF="#4.107">4.107. When importing module XXX, why do I get &quot;undefined symbol: PyUnicodeUCS2_...&quot; ?</A>
<LI><A HREF="#4.108">4.108. How do I create a .pyc file?</A>

</UL>

<P>
<HR>
<H2>5. Extending Python</H2>
<UL>
<LI><A HREF="#5.1">5.1. Can I create my own functions in C?</A>
<LI><A HREF="#5.2">5.2. Can I create my own functions in C++?</A>
<LI><A HREF="#5.3">5.3. How can I execute arbitrary Python statements from C?</A>
<LI><A HREF="#5.4">5.4. How can I evaluate an arbitrary Python expression from C?</A>
<LI><A HREF="#5.5">5.5. How do I extract C values from a Python object?</A>
<LI><A HREF="#5.6">5.6. How do I use Py_BuildValue() to create a tuple of arbitrary length?</A>
<LI><A HREF="#5.7">5.7. How do I call an object's method from C?</A>
<LI><A HREF="#5.8">5.8. How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?</A>
<LI><A HREF="#5.9">5.9. How do I access a module written in Python from C?</A>
<LI><A HREF="#5.10">5.10. How do I interface to C++ objects from Python?</A>
<LI><A HREF="#5.11">5.11. mSQLmodule (or other old module) won't build with Python 1.5 (or later)</A>
<LI><A HREF="#5.12">5.12. I added a module using the Setup file and the make fails! Huh?</A>
<LI><A HREF="#5.13">5.13. I want to compile a Python module on my Red Hat Linux system, but some files are missing.</A>
<LI><A HREF="#5.14">5.14. What does &quot;SystemError: _PyImport_FixupExtension: module yourmodule not loaded&quot; mean?</A>
<LI><A HREF="#5.15">5.15. How to tell &quot;incomplete input&quot; from &quot;invalid input&quot;?</A>
<LI><A HREF="#5.16">5.16. How do I debug an extension?</A>
<LI><A HREF="#5.17">5.17. How do I find undefined Linux g++ symbols, __builtin_new or __pure_virtural</A>
<LI><A HREF="#5.18">5.18. How do I define and create objects corresponding to built-in/extension types</A>

</UL>

<P>
<HR>
<H2>6. Python's design</H2>
<UL>
<LI><A HREF="#6.1">6.1. Why isn't there a switch or case statement in Python?</A>
<LI><A HREF="#6.2">6.2. Why does Python use indentation for grouping of statements?</A>
<LI><A HREF="#6.3">6.3. Why are Python strings immutable?</A>
<LI><A HREF="#6.4">6.4. Delete</A>
<LI><A HREF="#6.5">6.5. Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</A>
<LI><A HREF="#6.6">6.6. Why can't I derive a class from built-in types (e.g. lists or files)?</A>
<LI><A HREF="#6.7">6.7. Why must 'self' be declared and used explicitly in method definitions and calls?</A>
<LI><A HREF="#6.8">6.8. Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</A>
<LI><A HREF="#6.9">6.9. Why can't lambda forms contain statements?</A>
<LI><A HREF="#6.10">6.10. [deleted]</A>
<LI><A HREF="#6.11">6.11. [deleted]</A>
<LI><A HREF="#6.12">6.12. Why is there no more efficient way of iterating over a dictionary than first constructing the list of keys()?</A>
<LI><A HREF="#6.13">6.13. Can Python be compiled to machine code, C or some other language?</A>
<LI><A HREF="#6.14">6.14. How does Python manage memory?</A>
<LI><A HREF="#6.15">6.15. Why are there separate tuple and list data types?</A>
<LI><A HREF="#6.16">6.16. How are lists implemented?</A>
<LI><A HREF="#6.17">6.17. How are dictionaries implemented?</A>
<LI><A HREF="#6.18">6.18. Why must dictionary keys be immutable?</A>
<LI><A HREF="#6.19">6.19. How the heck do you make an array in Python?</A>
<LI><A HREF="#6.20">6.20. Why doesn't list.sort() return the sorted list?</A>
<LI><A HREF="#6.21">6.21. How do you specify and enforce an interface spec in Python?</A>
<LI><A HREF="#6.22">6.22. Why do all classes have the same type? Why do instances all have the same type?</A>
<LI><A HREF="#6.23">6.23. Why isn't all memory freed when Python exits?</A>
<LI><A HREF="#6.24">6.24. Why no class methods or mutable class variables?</A>
<LI><A HREF="#6.25">6.25. Why are default values sometimes shared between objects?</A>
<LI><A HREF="#6.26">6.26. Why no goto?</A>
<LI><A HREF="#6.27">6.27. How do you make a higher order function in Python?</A>
<LI><A HREF="#6.28">6.28. Why do I get a SyntaxError for a 'continue' inside a 'try'?</A>
<LI><A HREF="#6.29">6.29. Why can't raw strings (r-strings) end with a backslash?</A>
<LI><A HREF="#6.30">6.30. Why can't I use an assignment in an expression?</A>
<LI><A HREF="#6.31">6.31. Why doesn't Python have a &quot;with&quot; statement like some other languages?</A>
<LI><A HREF="#6.32">6.32. Why are colons required for if/while/def/class?</A>
<LI><A HREF="#6.33">6.33. Can't we get rid of the Global Interpreter Lock?</A>

</UL>

<P>
<HR>
<H2>7. Using Python on non-UNIX platforms</H2>
<UL>
<LI><A HREF="#7.1">7.1. Is there a Mac version of Python?</A>
<LI><A HREF="#7.2">7.2. Are there DOS and Windows versions of Python?</A>
<LI><A HREF="#7.3">7.3. Is there an OS/2 version of Python?</A>
<LI><A HREF="#7.4">7.4. Is there a VMS version of Python?</A>
<LI><A HREF="#7.5">7.5. What about IBM mainframes, or other non-UNIX platforms?</A>
<LI><A HREF="#7.6">7.6. Where are the source or Makefiles for the non-UNIX versions?</A>
<LI><A HREF="#7.7">7.7. What is the status and support for the non-UNIX versions?</A>
<LI><A HREF="#7.8">7.8. I have a PC version but it appears to be only a binary. Where's the library?</A>
<LI><A HREF="#7.9">7.9. Where's the documentation for the Mac or PC version?</A>
<LI><A HREF="#7.10">7.10. How do I create a Python program file on the Mac or PC?</A>
<LI><A HREF="#7.11">7.11. How can I use Tkinter on Windows 95/NT?</A>
<LI><A HREF="#7.12">7.12. cgi.py (or other CGI programming) doesn't work sometimes on NT or win95!</A>
<LI><A HREF="#7.13">7.13. Why doesn't os.popen() work in PythonWin on NT?</A>
<LI><A HREF="#7.14">7.14. How do I use different functionality on different platforms with the same program?</A>
<LI><A HREF="#7.15">7.15. Is there an Amiga version of Python?</A>
<LI><A HREF="#7.16">7.16. Why doesn't os.popen()/win32pipe.popen() work on Win9x?</A>

</UL>

<P>
<HR>
<H2>8. Python on Windows</H2>
<UL>
<LI><A HREF="#8.1">8.1. Using Python for CGI on Microsoft Windows</A>
<LI><A HREF="#8.2">8.2. How to check for a keypress without blocking?</A>
<LI><A HREF="#8.3">8.3. $PYTHONPATH</A>
<LI><A HREF="#8.4">8.4. dedent syntax errors</A>
<LI><A HREF="#8.5">8.5. How do I emulate os.kill() in Windows?</A>
<LI><A HREF="#8.6">8.6. Why does os.path.isdir() fail on NT shared directories?</A>
<LI><A HREF="#8.7">8.7. PyRun_SimpleFile() crashes on Windows but not on Unix</A>
<LI><A HREF="#8.8">8.8. Import of _tkinter fails on Windows 95/98</A>
<LI><A HREF="#8.9">8.9. Can't extract the downloaded documentation on Windows</A>
<LI><A HREF="#8.10">8.10. Can't get Py_RunSimpleFile() to work.</A>
<LI><A HREF="#8.11">8.11. Where is Freeze for Windows?</A>
<LI><A HREF="#8.12">8.12. Is a *.pyd file the same as a DLL?</A>
<LI><A HREF="#8.13">8.13. Missing cw3215mt.dll (or missing cw3215.dll)</A>
<LI><A HREF="#8.14">8.14. How to make python scripts executable:</A>
<LI><A HREF="#8.15">8.15. Warning about CTL3D32 version from installer</A>
<LI><A HREF="#8.16">8.16. How can I embed Python into a Windows application?</A>
<LI><A HREF="#8.17">8.17. Setting up IIS 5 to use Python for CGI</A>
<LI><A HREF="#8.18">8.18. How do I run a Python program under Windows?</A>

</UL>

<HR>
<H1>1. General information and availability</H1>

<HR>
<H2><A NAME="1.1">1.1. What is Python?</A></H2>
Python is an interpreted, interactive, object-oriented programming
language.  It incorporates modules, exceptions, dynamic typing, very
high level dynamic data types, and classes.  Python combines
remarkable power with very clear syntax.  It has interfaces to many
system calls and libraries, as well as to various window systems, and
is extensible in C or C++.  It is also usable as an extension language
for applications that need a programmable interface.  Finally, Python
is portable: it runs on many brands of UNIX, on the Mac, and on PCs
under MS-DOS, Windows, Windows NT, and OS/2.
<P>
To find out more, the best thing to do is to start reading the
tutorial from the documentation set (see a few questions further
down).
<P>
See also question 1.17 (what is Python good for).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.001.htp">Log info</A>

/ Last changed on Mon May 26 16:05:18 1997 by
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
<P>

<HR>
<H2><A NAME="1.2">1.2. Why is it called Python?</A></H2>
Apart from being a computer scientist, I'm also a fan of "Monty
Python's Flying Circus" (a BBC comedy series from the seventies, in
the -- unlikely -- case you didn't know).  It occurred to me one day
that I needed a name that was short, unique, and slightly mysterious.
And I happened to be reading some scripts from the series at the
time...  So then I decided to call my language Python.
<P>
By now I don't care any more whether you use a Python, some other
snake, a foot or 16-ton weight, or a wood rat as a logo for Python!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.002.htp">Log info</A>

/ Last changed on Thu Aug 24 00:50:41 2000 by
<A HREF="mailto:guido@beopen.com">GvR</A>
<P>

<HR>
<H2><A NAME="1.3">1.3. How do I obtain a copy of the Python source?</A></H2>
The latest Python source distribution is always available from
python.org, at <A HREF="http://www.python.org/download">http://www.python.org/download</A>.  The latest development sources can be obtained via anonymous CVS from SourceForge, at <A HREF="http://www.sf.net/projects/python">http://www.sf.net/projects/python</A> .
<P>
The source distribution is a gzipped tar file containing the complete C source, LaTeX
documentation, Python library modules, example programs, and several
useful pieces of freely distributable software.  This will compile and
run out of the box on most UNIX platforms.  (See section 7 for
non-UNIX information.)
<P>
Older versions of Python are also available from python.org.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.003.htp">Log info</A>

/ Last changed on Tue Apr  9 17:06:16 2002 by
<A HREF="mailto:akuchlin@mems-exchange.org">A.M. Kuchling</A>
<P>

<HR>
<H2><A NAME="1.4">1.4. How do I get documentation on Python?</A></H2>
All documentation is available on-line, starting at <A HREF="http://www.python.org/doc">http://www.python.org/doc</A>/. 
<P>
The LaTeX source for the documentation is part of the source
distribution.  If you don't have LaTeX, the latest Python
documentation set is available, in various formats like postscript 
and html, by anonymous ftp - visit the above URL for links to the
current versions.
<P>
PostScript for a high-level description of Python is in the file nluug-paper.ps 
(a separate file on the ftp site).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.004.htp">Log info</A>

/ Last changed on Wed Jan 21 12:02:55 1998 by
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
<P>

<HR>
<H2><A NAME="1.5">1.5. Are there other ftp sites that mirror the Python distribution?</A></H2>
The following anonymous ftp sites keep mirrors of the Python
distribution:
<P>
USA:
<P>
<PRE>
        <A HREF="ftp://ftp.python.org/pub/python">ftp://ftp.python.org/pub/python</A>/
        <A HREF="ftp://gatekeeper.dec.com/pub/plan/python">ftp://gatekeeper.dec.com/pub/plan/python</A>/
        <A HREF="ftp://ftp.uu.net/languages/python">ftp://ftp.uu.net/languages/python</A>/
        <A HREF="ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python">ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python</A>/
        <A HREF="ftp://ftp.sterling.com/programming/languages/python">ftp://ftp.sterling.com/programming/languages/python</A>/
        <A HREF="ftp://uiarchive.cso.uiuc.edu/pub/lang/python">ftp://uiarchive.cso.uiuc.edu/pub/lang/python</A>/
        <A HREF="ftp://ftp.pht.com/mirrors/python/python">ftp://ftp.pht.com/mirrors/python/python</A>/
	<A HREF="ftp://ftp.cdrom.com/pub/python">ftp://ftp.cdrom.com/pub/python</A>/
</PRE>
Europe:
<P>
<PRE>
        <A HREF="ftp://ftp.cwi.nl/pub/python">ftp://ftp.cwi.nl/pub/python</A>/
        <A HREF="ftp://ftp.funet.fi/pub/languages/python">ftp://ftp.funet.fi/pub/languages/python</A>/
        <A HREF="ftp://ftp.sunet.se/pub/lang/python">ftp://ftp.sunet.se/pub/lang/python</A>/
        <A HREF="ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python">ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python</A>/
        <A HREF="ftp://ftp.lip6.fr/pub/python">ftp://ftp.lip6.fr/pub/python</A>/
        <A HREF="ftp://sunsite.cnlab-switch.ch/mirror/python">ftp://sunsite.cnlab-switch.ch/mirror/python</A>/
        <A HREF="ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python">ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python</A>/
</PRE>
Australia:
<P>
<PRE>
        <A HREF="ftp://ftp.dstc.edu.au/pub/python">ftp://ftp.dstc.edu.au/pub/python</A>/
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.005.htp">Log info</A>

/ Last changed on Wed Mar 24 09:20:49 1999 by
<A HREF="mailto:akuchlin@cnri.reston.va.us">A.M. Kuchling</A>
<P>

<HR>
<H2><A NAME="1.6">1.6. Is there a newsgroup or mailing list devoted to Python?</A></H2>
There is a newsgroup, comp.lang.python,
and a mailing list.  The newsgroup and mailing list are gatewayed into
each other -- if you can read news it's unnecessary to subscribe to
the mailing list.  To subscribe to the mailing list
(<A HREF="mailto:python-list@python.org">python-list@python.org</A>) visit its Mailman webpage at
<A HREF="http://www.python.org/mailman/listinfo/python-list">http://www.python.org/mailman/listinfo/python-list</A>
<P>
More info about the newsgroup and mailing list, and about other lists,
can be found at
<A HREF="http://www.python.org/psa/MailingLists.html">http://www.python.org/psa/MailingLists.html</A>.
<P>
Archives of the newsgroup are kept by Deja News and accessible
through the "Python newsgroup search" web page,
<A HREF="http://www.python.org/search/search_news.html">http://www.python.org/search/search_news.html</A>.
This page also contains pointer to other archival collections.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.006.htp">Log info</A>

/ Last changed on Wed Jun 23 09:29:36 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="1.7">1.7. Is there a WWW page devoted to Python?</A></H2>
Yes, <A HREF="http://www.python.org">http://www.python.org</A>/ is the official Python home page.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.007.htp">Log info</A>

/ Last changed on Fri May 23 14:42:59 1997 by
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
<P>

<HR>
<H2><A NAME="1.8">1.8. Is the Python documentation available on the WWW?</A></H2>
Yes. Python 2.0 documentation is available from
<A HREF="http://www.pythonlabs.com/tech/python2.0/doc">http://www.pythonlabs.com/tech/python2.0/doc</A>/ and from
<A HREF="http://www.python.org/doc">http://www.python.org/doc</A>/.  Note that most documentation
is available for on-line browsing as well as for downloading.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.008.htp">Log info</A>

/ Last changed on Tue Jan  2 03:14:08 2001 by
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
<P>

<HR>
<H2><A NAME="1.9">1.9. Are there any books on Python?</A></H2>
Yes, many, and more are being published.  See
the python.org Wiki at <A HREF="http://www.python.org/cgi-bin/moinmoin/PythonBooks">http://www.python.org/cgi-bin/moinmoin/PythonBooks</A> for a list.
<P>
You can also search online bookstores for "Python"
(and filter out the Monty Python references; or
perhaps search for "Python" and "language").
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.009.htp">Log info</A>

/ Last changed on Mon Aug  5 19:08:49 2002 by
<A HREF="mailto:akuchlin@mems-exchange.org">amk</A>
<P>

<HR>
<H2><A NAME="1.10">1.10. Are there any published articles about Python that I can reference?</A></H2>
If you can't reference the web site, and you don't want to reference the books
(see previous question), there are several articles on Python that you could
reference.
<P>
Most publications about Python are collected on the Python web site:
<P>
<PRE>
    <A HREF="http://www.python.org/doc/Publications.html">http://www.python.org/doc/Publications.html</A>
</PRE>
It is no longer recommended to reference this
very old article by Python's author:
<P>
<PRE>
    Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
    Servers Using the Python Programming Language", CWI Quarterly, Volume
    4, Issue 4 (December 1991), Amsterdam, pp 283-303.
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.010.htp">Log info</A>

/ Last changed on Sat Jul  4 20:52:31 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="1.11">1.11. Are there short introductory papers or talks on Python?</A></H2>
There are several - you can find links to some of them collected at
<A HREF="http://www.python.org/doc/Hints.html#intros">http://www.python.org/doc/Hints.html#intros</A>.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.011.htp">Log info</A>

/ Last changed on Fri May 23 15:04:05 1997 by
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
<P>

<HR>
<H2><A NAME="1.12">1.12. How does the Python version numbering scheme work?</A></H2>
Python versions are numbered A.B.C or A.B.  A is the major version
number -- it is only incremented for really major changes in the
language.  B is the minor version number, incremented for less
earth-shattering changes.  C is the micro-level -- it is
incremented for each bugfix release.  See PEP 6 for more information
about bugfix releases.
<P>
Not all releases have bugfix releases.
Note that in the past (ending with 1.5.2),
micro releases have added significant changes;
in fact the changeover from 0.9.9 to 1.0.0 was the first time
that either A or B changed!
<P>
Alpha, beta and release candidate versions have an additional suffixes.
The suffix for an alpha version is "aN" for some small number N, the
suffix for a beta version is "bN" for some small number N, and the
suffix for a release candidate version is "cN" for some small number N.
<P>
Note that (for instance) all versions labeled 2.0aN precede the
versions labeled 2.0bN, which precede versions labeled 2.0cN, and
<I>those</I> precede 2.0.
<P>
As a rule, no changes are made between release candidates and the final
release unless there are show-stopper bugs.
<P>
You may also find version numbers with a "+" suffix, e.g. "2.2+".
These are unreleased versions, built directly from the CVS trunk.
<P>
See also the documentation for sys.version, sys.hexversion, and
sys.version_info.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.012.htp">Log info</A>

/ Last changed on Mon Jan 14 06:34:17 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="1.13">1.13. How do I get a beta test version of Python?</A></H2>
All releases, including alphas, betas and release candidates, are announced on
comp.lang.python and comp.lang.python.announce newsgroups,
which are gatewayed into the <A HREF="mailto:python-list@python.org">python-list@python.org</A> and
<A HREF="mailto:python-announce@python.org">python-announce@python.org</A>. In addition, all these announcements appear on
the Python home page, at <A HREF="http://www.python.org">http://www.python.org</A>.
<P>
You can also access the development version of Python through CVS.  See <A HREF="http://sourceforge.net/cvs/?group_id=5470">http://sourceforge.net/cvs/?group_id=5470</A> for details.  If you're not familiar with CVS, documents like <A HREF="http://linux.oreillynet.com/pub/a/linux/2002/01/03/cvs_intro.html">http://linux.oreillynet.com/pub/a/linux/2002/01/03/cvs_intro.html</A>
provide an introduction.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.013.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.013.htp">Log info</A>

/ Last changed on Mon Jun  3 00:57:08 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="1.14">1.14. Are there copyright restrictions on the use of Python?</A></H2>
Hardly.  You can do anything you want with the source, as long as
you leave the copyrights in, and display those copyrights in any
documentation about Python that you produce.  Also, don't use the
author's institute's name in publicity without prior written
permission, and don't hold them responsible for anything (read the
actual copyright for a precise legal wording).
<P>
In particular, if you honor the copyright rules, it's OK to use Python
for commercial use, to sell copies of Python in source or binary form,
or to sell products that enhance Python or incorporate Python (or part
of it) in some form.  I would still like to know about all commercial
use of Python!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.014.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.014.htp">Log info</A>
<P>

<HR>
<H2><A NAME="1.15">1.15. Why was Python created in the first place?</A></H2>
Here's a <I>very</I> brief summary of what got me started:
<P>
I had extensive experience with implementing an interpreted language
in the ABC group at CWI, and from working with this group I had
learned a lot about language design.  This is the origin of many
Python features, including the use of indentation for statement
grouping and the inclusion of very-high-level data types (although the
details are all different in Python).
<P>
I had a number of gripes about the ABC language, but also liked many
of its features.  It was impossible to extend the ABC language (or its
implementation) to remedy my complaints -- in fact its lack of
extensibility was one of its biggest problems.
I had some experience with using Modula-2+ and talked with the
designers of Modula-3 (and read the M3 report).  M3 is the origin of
the syntax and semantics used for exceptions, and some other Python
features.
<P>
I was working in the Amoeba distributed operating system group at
CWI.  We needed a better way to do system administration than by
writing either C programs or Bourne shell scripts, since Amoeba had
its own system call interface which wasn't easily accessible from the
Bourne shell.  My experience with error handling in Amoeba made me
acutely aware of the importance of exceptions as a programming
language feature.
<P>
It occurred to me that a scripting language with a syntax like ABC
but with access to the Amoeba system calls would fill the need.  I
realized that it would be foolish to write an Amoeba-specific
language, so I decided that I needed a language that was generally
extensible.
<P>
During the 1989 Christmas holidays, I had a lot of time on my hand,
so I decided to give it a try.  During the next year, while still
mostly working on it in my own time, Python was used in the Amoeba
project with increasing success, and the feedback from colleagues made
me add many early improvements.
<P>
In February 1991, after just over a year of development, I decided
to post to USENET.  The rest is in the Misc/HISTORY file.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.015.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.015.htp">Log info</A>

/ Last changed on Fri May 23 00:06:23 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="1.16">1.16. Do I have to like &quot;Monty Python's Flying Circus&quot;?</A></H2>
No, but it helps.  Pythonistas like the occasional reference to SPAM,
and of course, nobody expects the Spanish Inquisition
<P>
The two main reasons to use Python are:
<P>
<PRE>
 - Portable
 - Easy to learn
</PRE>
The <I>three</I> main reasons to use Python are:
<P>
<PRE>
 - Portable
 - Easy to learn
 - Powerful standard library
</PRE>
(And nice red uniforms.)
<P>
And remember, there is <I>no</I> rule six.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.016.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.016.htp">Log info</A>

/ Last changed on Wed May 28 10:39:21 1997 by
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
<P>

<HR>
<H2><A NAME="1.17">1.17. What is Python good for?</A></H2>
Python is used in many situations where a great deal of dynamism,
ease of use, power, and flexibility are required.  
<P>
In the area of basic text
manipulation core Python (without any non-core extensions) is easier
to use and is roughly as fast as just  about any language, and this makes Python
good for many system administration type tasks and for CGI programming
and other application areas that manipulate text and strings and such.
<P>
When augmented with
standard extensions (such as PIL, COM, Numeric, oracledb, kjbuckets, 
tkinter, win32api, etc.)
or special purpose extensions (that you write, perhaps using helper tools such
as SWIG, or using object protocols such as ILU/CORBA or COM) Python 
becomes a very convenient "glue" or "steering"
language that helps make heterogeneous collections of unrelated
software packages work together.
For example by combining Numeric with oracledb you can help your
SQL database do statistical analysis, or even Fourier transforms.
One of the features that makes Python excel in the "glue language" role
is Python's simple, usable, and powerful C language runtime API.
<P>
Many developers also use Python extensively as a graphical user
interface development aide.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.017.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.017.htp">Log info</A>

/ Last changed on Sat May 24 10:13:11 1997 by
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
<P>

<HR>
<H2><A NAME="1.18">1.18. Can I use the FAQ Wizard software to maintain my own FAQ?</A></H2>
Sure.  It's in Tools/faqwiz/ of the python source tree.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.018.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.018.htp">Log info</A>

/ Last changed on Fri Mar 29 06:50:32 2002 by
<A HREF="mailto:aahz@pythoncraft.com">Aahz</A>
<P>

<HR>
<H2><A NAME="1.19">1.19. Which editor has good support for editing Python source code?</A></H2>
On Unix, the first choice is Emacs/XEmacs.  There's an elaborate
mode for editing Python code, which is available from the Python
source distribution (Misc/python-mode.el).  It's also bundled
with XEmacs (we're still working on legal details to make it possible
to bundle it with FSF Emacs).  And it has its own web page:
<P>
<PRE>
    <A HREF="http://www.python.org/emacs/python-mode/index.html">http://www.python.org/emacs/python-mode/index.html</A>
</PRE>
There are many other choices, for Unix, Windows or Macintosh.
Richard Jones compiled a table from postings on the Python newsgroup:
<P>
<PRE>
    <A HREF="http://www.bofh.asn.au/~richard/editors.html">http://www.bofh.asn.au/~richard/editors.html</A>
</PRE>
See also FAQ question 7.10 for some more Mac and Win options.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.019.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.019.htp">Log info</A>

/ Last changed on Mon Jun 15 23:21:04 1998 by
<A HREF="mailto:guido@python.org">Gvr</A>
<P>

<HR>
<H2><A NAME="1.20">1.20. I've never programmed before. Is there a Python tutorial?</A></H2>
There are several, and at least one book.  
All information for beginning Python programmers is collected here:
<P>
<PRE>
    <A HREF="http://www.python.org/doc/Newbies.html">http://www.python.org/doc/Newbies.html</A>
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.020.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.020.htp">Log info</A>

/ Last changed on Wed Sep  5 05:34:07 2001 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="1.21">1.21. Where in the world is www.python.org located?</A></H2>
It's currently in Amsterdam, graciously hosted by XS4ALL:
<P>
<PRE>
    <A HREF="http://www.xs4all.nl">http://www.xs4all.nl</A>
</PRE>
Thanks to Thomas Wouters for setting this up!!!!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq01.021.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq01.021.htp">Log info</A>

/ Last changed on Fri Aug  3 21:49:27 2001 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H1>2. Python in the real world</H1>

<HR>
<H2><A NAME="2.1">2.1. How many people are using Python?</A></H2>
Certainly thousands, and quite probably tens of thousands of users.
More are seeing the light each day.  The comp.lang.python newsgroup is
very active, but overall there is no accurate estimate of the number of subscribers or Python users.
<P>
Jacek Artymiak has created a Python Users Counter; you can see the 
current count by visiting
<A HREF="http://www.wszechnica.safenet.pl/cgi-bin/checkpythonuserscounter.py">http://www.wszechnica.safenet.pl/cgi-bin/checkpythonuserscounter.py</A>
(this will not increment the counter; use the link there if you haven't
added yourself already). Most Python users appear not to have registered themselves.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.001.htp">Log info</A>

/ Last changed on Thu Feb 21 23:29:18 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="2.2">2.2. Have any significant projects been done in Python?</A></H2>
At CWI (the former home of Python), we have written a 20,000 line
authoring environment for transportable hypermedia presentations, a
5,000 line multimedia teleconferencing tool, as well as many many
smaller programs.
<P>
At CNRI (Python's new home), we have written two large applications:
Grail, a fully featured web browser (see
<A HREF="http://grail.cnri.reston.va.us">http://grail.cnri.reston.va.us</A>),
and the Knowbot Operating Environment,
a distributed environment for mobile code.
<P>
The University of Virginia uses Python to control a virtual reality
engine.  See <A HREF="http://alice.cs.cmu.edu">http://alice.cs.cmu.edu</A>.
<P>
The ILU project at Xerox PARC can generate Python glue for ILU
interfaces.  See <A HREF="ftp://ftp.parc.xerox.com/pub/ilu/ilu.html">ftp://ftp.parc.xerox.com/pub/ilu/ilu.html</A>.  ILU
is a free CORBA compliant ORB which supplies distributed object
connectivity to a host of platforms using a host of languages.
<P>
Mark Hammond and Greg Stein and others are interfacing Python to
Microsoft's COM and ActiveX architectures.  This means, among other
things, that Python may be used in active server pages or as a COM
controller (for example to automatically extract from or insert information
into Excel or MSAccess or any other COM aware application).
Mark claims Python can even be a ActiveX scripting host (which
means you could embed JScript inside a Python application, if you
had a strange sense of humor).  Python/AX/COM is distributed as part
of the PythonWin distribution.
<P>
The University of California, Irvine uses a student administration
system called TELE-Vision written entirely in Python.  Contact: Ray
Price <A HREF="mailto:rlprice@uci.edu">rlprice@uci.edu</A>.
<P>
The Melbourne Cricket Ground (MCG) in Australia (a 100,000+ person venue)
has it's scoreboard system written largely in Python on MS Windows.
Python expressions are used to create almost every scoring entry that
appears on the board.  The move to Python/C++ away from exclusive C++
has provided a level of functionality that would simply not have been
viable otherwise.
<P>
See also the next question.
<P>
Note: this FAQ entry is really old.
See <A HREF="http://www.python.org/psa/Users.html">http://www.python.org/psa/Users.html</A> for a more recent list.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.002.htp">Log info</A>

/ Last changed on Wed Oct 25 13:24:15 2000 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="2.3">2.3. Are there any commercial projects going on using Python?</A></H2>
Yes, there's lots of commercial activity using Python.  See
<A HREF="http://www.python.org/psa/Users.html">http://www.python.org/psa/Users.html</A> for a list.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.003.htp">Log info</A>

/ Last changed on Wed Oct 14 18:17:33 1998 by
<A HREF="mailto:klm@python.org">ken</A>
<P>

<HR>
<H2><A NAME="2.4">2.4. How stable is Python?</A></H2>
Very stable.  New, stable releases have been coming out roughly every 3 to 12 months since 1991, and this seems likely to continue.
<P>
With the introduction of retrospective "bugfix" releases the stability of the language implementations can be, and is being, improved independently of the new features offered by more recent major or minor releases. Bugfix releases, indicated by a third component of the version number, only fix known problems and do not gratuitously introduce new and possibly incompatible features or modified library functionality.
<P>
Release 2.2 got its first bugfix on April 10, 2002. The new version
number is now 2.2.1. The 2.1 release, at 2.1.3, can probably be
considered the "most stable" platform because it has been bugfixed
twice.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.004.htp">Log info</A>

/ Last changed on Tue Jul 23 10:20:04 2002 by
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
<P>

<HR>
<H2><A NAME="2.5">2.5. What new developments are expected for Python in the future?</A></H2>
See <A HREF="http://www.python.org/peps">http://www.python.org/peps</A>/ for the Python Enhancement 
Proposals (PEPs). PEPs are design
documents 
describing a  suggested new feature for Python, providing
a concise technical specification and a rationale.
<P>
Also, follow the discussions on the python-dev mailing list.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.005.htp">Log info</A>

/ Last changed on Tue Apr  9 17:09:51 2002 by
<A HREF="mailto:akuchlin@mems-exchange.org">A.M. Kuchling</A>
<P>

<HR>
<H2><A NAME="2.6">2.6. Is it reasonable to propose incompatible changes to Python?</A></H2>
In general, no.  There are already millions of lines of Python code
around the world, so any changes in the language that invalidates more
than a very small fraction of existing programs has to be frowned
upon.  Even if you can provide a conversion program, there still is
the problem of updating all documentation.  Providing a gradual
upgrade path is the only way if a feature has to be changed.
<P>
See <A HREF="http://www.python.org/peps/pep-0005.html">http://www.python.org/peps/pep-0005.html</A> for the proposed
mechanism for creating backwards-incompatibilities.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.006.htp">Log info</A>

/ Last changed on Mon Apr  1 22:13:47 2002 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="2.7">2.7. What is the future of Python?</A></H2>
Please see <A HREF="http://www.python.org/peps">http://www.python.org/peps</A>/ for proposals of future
activities. One of the PEPs (Python Enhancement Proposals) deals
with the PEP process and PEP format -- see
<A HREF="http://www.python.org/peps/pep-0001.html">http://www.python.org/peps/pep-0001.html</A> if you want to
submit a PEP. In <A HREF="http://www.python.org/peps/pep-0042.html">http://www.python.org/peps/pep-0042.html</A> there
is a list of wishlists the Python Development team plans to tackle.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.007.htp">Log info</A>

/ Last changed on Mon Apr  1 22:15:46 2002 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="2.8">2.8. What was the PSA, anyway?</A></H2>
The Python Software Activity was
created by a number of Python aficionados who want Python to be more
than the product and responsibility of a single individual.
The PSA was not an independent organization, but lived
under the umbrealla of CNRI.
<P>
The PSA has been superseded by the Python Software Foundation,
an independent non-profit organization.  The PSF's home page
is at <A HREF="http://www.python.org/psf">http://www.python.org/psf</A>/.
<P>
Some pages created by the PSA still live at
<A HREF="http://www.python.org/psa">http://www.python.org/psa</A>/
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.008.htp">Log info</A>

/ Last changed on Thu Jul 25 18:19:44 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="2.9">2.9. Deleted</A></H2>
<P>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.009.htp">Log info</A>

/ Last changed on Tue Jan  2 02:51:30 2001 by
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
<P>

<HR>
<H2><A NAME="2.10">2.10. Deleted</A></H2>
<P>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.010.htp">Log info</A>

/ Last changed on Tue Jan  2 02:52:19 2001 by
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
<P>

<HR>
<H2><A NAME="2.11">2.11. Is Python Y2K (Year 2000) Compliant?</A></H2>
As of January, 2001 no major problems have been reported and Y2K
compliance seems to be a non-issue.
<P>
Since Python is available free of charge, there are no absolute
guarantees.  If there <I>are</I> unforeseen problems, liability is the
user's rather than the developers', and there is nobody you can sue for damages.
<P>
Python does few 
date manipulations, and what it does is all based on the Unix
representation for time (even on non-Unix systems) which uses seconds
since 1970 and won't overflow until 2038.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.011.htp">Log info</A>

/ Last changed on Mon Jan  8 17:19:32 2001 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="2.12">2.12. Is Python a good language in a class for beginning programmers?</A></H2>
Yes.  This long answer attempts to address any concerns you might
have with teaching Python as a programmer's first language.  
(If you want to discuss Python's use in education, then 
you may be interested in joining the edu-sig mailinglist.
See <A HREF="http://www.python.org/sigs/edu-sig">http://www.python.org/sigs/edu-sig</A>/ )
<P>
It is still common to start students with a procedural
(subset of a) statically typed language such as Pascal, C, or
a subset of C++ or Java.  I think that students may be better
served by learning Python as their first language.  Python has
a very simple and consistent syntax and a large standard library.
Most importantly, using Python in a beginning programming course
permits students to concentrate on important programming skills,
such as problem decomposition and data type design.
<P>
With Python, students can be quickly introduced to basic concepts
such as loops and procedures.  They can even probably work with
user-defined objects in their very first course.  They could
implement a tree structure as nested Python lists, for example.
They could be introduced to objects in their first course if
desired.  For a student who has never programmed before, using
a statically typed language seems unnatural.  It presents
additional complexity that the student must master and slows
the pace of the course.  The students are trying to learn to
think like a computer, decompose problems, design consistent
interfaces, and encapsulate data.  While learning to use a
statically typed language is important, it is not necessarily the
best topic to address in the students' first programming course.
<P>
Many other aspects of Python make it a good first language.
Python has a large standard library (like Java) so that
students can be assigned programming projects very early in the
course that <I>do</I> something.  Assignments aren't restricted to the
standard four-function calculator and check balancing programs.
By using the standard library, students can gain the satisfaction
of working on realistic applications as they learn the fundamentals
of programming.  Using the standard library also teaches students
about code reuse.
<P>
Python's interactive interpreter also enables students to
test language features while they're programming.  They can keep
a window with the interpreter running while they enter their
programs' source in another window.  If they can't remember the
methods for a list, they can do something like this:
<P>
<PRE>
 >>> L = []
 >>> dir(L)
 ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
 'reverse', 'sort']
 >>> print L.append.__doc__
 L.append(object) -- append object to end
 >>> L.append(1)
 >>> L
 [1]
</PRE>
With the interpreter, documentation is never far from the
student as he's programming.
<P>
There are also good IDEs for Python.  Guido van Rossum's IDLE
is a cross-platform IDE for Python that is written in Python
using Tk.  There is also a Windows specific IDE called PythonWin.
Emacs users will be happy to know that there is a very good Python
mode for Emacs.  All of these programming environments provide
syntax highlighting, auto-indenting, and access to the interactive
interpreter while coding.  For more information about IDEs, see XXX.
<P>
If your department is currently using Pascal because it was
designed to be a teaching language, then you'll be happy to
know that Guido van Rossum designed Python to be simple to
teach to everyone but powerful enough to implement real world
applications.  Python makes a good language for first time
programmers because that was one of Python's design goals.
There are papers at <A HREF="http://www.python.org/doc/essays">http://www.python.org/doc/essays</A>/ on the Python website 
by Python's creator explaining his objectives for the language.
One that may interest you is titled "Computer Programming for Everybody" 
<A HREF="http://www.python.org/doc/essays/cp4e.html">http://www.python.org/doc/essays/cp4e.html</A>
<P>
If you're seriously considering Python as a language for your
school,  Guido van Rossum may even be willing to correspond with
you about how the language would fit in your curriculum.
See <A HREF="http://www.python.org/doc/FAQ.html#2.2">http://www.python.org/doc/FAQ.html#2.2</A> for examples of
Python's use in the "real world."
<P>
While Python, its source code, and its IDEs are freely
available, this consideration should not rule 
out other languages.  There are other free languages (Java, 
free C compilers), and many companies are willing to waive some
or all of their fees for student programming tools if it
guarantees that a whole graduating class will know how to
use their tools.  That is, if one of the requirements for 
the language that will be taught is that it be freely 
available, then Python qualifies, but this requirement 
does not preclude other languages.
<P>
While Python jobs may not be as prevalent as C/C++/Java jobs,
teachers should not worry about teaching students critical job
skills in their first course.  The skills that win students a
job are those they learn in their senior classes and internships.
Their first programming courses are there to lay a solid
foundation in programming fundamentals.  The primary question
in choosing the language for such a course should be which
language permits the students to learn this material without
hindering or limiting them.
<P>
Another argument for Python is that there are many tasks for
which something like C++ is overkill.  That's where languages
like Python, Perl, Tcl, and Visual Basic thrive.  It's critical
for students to know something about these languages.   (Every
employer for whom I've worked used at least one such language.)
Of the languages listed above, Python probably makes the best
language in a programming curriculum since its syntax is simple,
consistent, and not unlike other languages (C/C++/Java) that
are probably in the curriculum.  By starting students with
Python, a department simultaneously lays the foundations for
other programming courses and introduces students to the type
of language that is often used as a "glue" language.  As an
added bonus, Python can be used to interface with Microsoft's
COM components (thanks to Mark Hammond).  There is also Jython, 
a Java implementation of the Python interpreter, that can be 
used to connect Java components.
<P>
If you currently start students with Pascal or C/C++ or Java,
you may be worried they will have trouble learning a statically
typed language after starting with Python.  I think that this
fear most often stems from the fact that the teacher started
with a statically typed language, and we tend to like to teach
others in the same way we were taught.  In reality, the
transition from Python to one of these other languages is
quite simple.
<P>
To motivate a statically typed language such as C++, begin the
course by explaining that unlike Python, their first language,
C++ is compiled to a machine dependent executable.  Explain
that the point is to make a very fast executable.  To permit
the compiler to make optimizations, programmers must help it
by specifying the "types" of variables.  By restricting each
variable to a specific type, the compiler can reduce the
book-keeping it has to do to permit dynamic types.  The compiler
also has to resolve references at compile time.  Thus, the
language gains speed by sacrificing some of Python's dynamic
features.  Then again, the C++ compiler provides type safety
and catches many bugs at compile time instead of run time (a
critical consideration for many commercial applications).  C++
is also designed for very large programs where one may want to
guarantee that others don't touch an object's implementation.
C++ provides very strong language features to separate an object's
implementation from its interface.  Explain why this separation
is a good thing.
<P>
The first day of a C++ course could then be a whirlwind introduction
to what C++ requires and provides.  The point here is that after
a semester or two of Python, students are hopefully competent
programmers.  They know how to handle loops and write procedures.
They've also worked with objects, thought about the benefits of
consistent interfaces, and used the technique of subclassing to
specialize behavior.  Thus, a whirlwind introduction to C++ could
show them how objects and subclassing looks in C++.  The
potentially difficult concepts of object-oriented design were
taught without the additional obstacles presented by a language
such as C++ or Java.  When learning one of these languages,
the students would already understand the "road map."  They
understand objects; they would just be learning how objects
fit in a statically typed languages.  Language requirements
and compiler errors that seem unnatural to beginning programmers
make sense in this new context.  Many students will find it
helpful to be able to write a fast prototype of their algorithms
in Python.  Thus, they can test and debug their ideas before
they attempt to write the code in the new language, saving the
effort of working with C++ types for when they've discovered a
working solution for their assignments.  When they get annoyed
with the rigidity of types, they'll be happy to learn about
containers and templates to regain some of the lost flexibility
Python afforded them.  Students may also gain an appreciation
for the fact that no language is best for every task.  They'll
see that C++ is faster, but they'll know that they can gain
flexibility and development speed with a Python when execution
speed isn't critical.
<P>
If you have any concerns that weren't addressed here, try
posting to the Python newsgroup.  Others there have done some
work with using Python as an instructional tool.  Good luck.
We'd love to hear about it if you choose Python for your course.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq02.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq02.012.htp">Log info</A>

/ Last changed on Mon Dec  2 19:32:35 2002 by
<A HREF="mailto:sconce@in-spec-inc.com">Bill Sconce</A>
<P>

<HR>
<H1>3. Building Python and Other Known Bugs</H1>

<HR>
<H2><A NAME="3.1">3.1. Is there a test set?</A></H2>
Sure.  You can run it after building with "make test", or you can
run it manually with this command at the Python prompt:
<P>
<PRE>
 import test.autotest
</PRE>
In Python 1.4 or earlier, use
<P>
<PRE>
 import autotest
</PRE>
The test set doesn't test <I>all</I> features of Python,
but it goes a long way to confirm that Python is actually working.
<P>
NOTE: if "make test" fails, don't just mail the output to the
newsgroup -- this doesn't give enough information to debug the
problem.  Instead, find out which test fails, and run that test
manually from an interactive interpreter.  For example, if
"make test" reports that test_spam fails, try this interactively:
<P>
<PRE>
 import test.test_spam
</PRE>
This generally produces more verbose output which can be diagnosed
to debug the problem.  If you find a bug in Python or the libraries, or in the tests, please report this in the Python bug tracker at SourceForge:
<P>
<A HREF="http://sourceforge.net/tracker/?func=add&amp;group_id=5470&amp;atid=105470">http://sourceforge.net/tracker/?func=add&amp;group_id=5470&amp;atid=105470</A>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.001.htp">Log info</A>

/ Last changed on Fri Apr 27 10:29:36 2001 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="3.2">3.2. When running the test set, I get complaints about floating point operations, but when playing with floating point operations I cannot find anything wrong with them.</A></H2>
The test set makes occasional unwarranted assumptions about the
semantics of C floating point operations.  Until someone donates a
better floating point test set, you will have to comment out the
offending floating point tests and execute similar tests manually.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.002.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.3">3.3. Link errors after rerunning the configure script.</A></H2>
It is generally necessary to run "make clean" after a configuration
change.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.003.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.4">3.4. The python interpreter complains about options passed to a script (after the script name).</A></H2>
You are probably linking with GNU getopt, e.g. through -liberty.
Don't.  The reason for the complaint is that GNU getopt, unlike System
V getopt and other getopt implementations, doesn't consider a
non-option to be the end of the option list.  A quick (and compatible)
fix for scripts is to add "--" to the interpreter, like this:
<P>
<PRE>
        #! /usr/local/bin/python --
</PRE>
You can also use this interactively:
<P>
<PRE>
        python -- script.py [options]
</PRE>
Note that a working getopt implementation is provided in the Python
distribution (in Python/getopt.c) but not automatically used.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.004.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.5">3.5. When building on the SGI, make tries to run python to create glmodule.c, but python hasn't been built or installed yet.</A></H2>
Comment out the line mentioning glmodule.c in Setup and build a
python without gl first; install it or make sure it is in your $PATH,
then edit the Setup file again to turn on the gl module, and make
again.  You don't need to do "make clean"; you do need to run "make
Makefile" in the Modules subdirectory (or just run "make" at the
toplevel).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.005.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.6">3.6. I use VPATH but some targets are built in the source directory.</A></H2>
On some systems (e.g. Sun), if the target already exists in the
source directory, it is created there instead of in the build
directory.  This is usually because you have previously built without
VPATH.  Try running "make clobber" in the source directory.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.006.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.7">3.7. Trouble building or linking with the GNU readline library.</A></H2>
You can use the GNU readline library to improve the interactive user
interface: this gives you line editing and command history when
calling python interactively. Its sources are distributed with 
Python (at least for 2.0).  Uncomment the line 
<P>
#readline readline.c -lreadline -ltermcap
<P>
in Modules/Setup.  The configuration option --with-readline 
is no longer supported, at least in Python 2.0.  Some hints on 
building and using the readline library:
On SGI IRIX 5, you may have to add the following
to rldefs.h:
<P>
<PRE>
        #ifndef sigmask
        #define sigmask(sig) (1L &lt;&lt; ((sig)-1))
        #endif
</PRE>
On some systems, you will have to add #include "rldefs.h" to the
top of several source files, and if you use the VPATH feature, you
will have to add dependencies of the form foo.o: foo.c to the
Makefile for several values of foo.
The readline library requires use of the termcap library. A
known problem with this is that it contains entry points which
cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
conflict can be solved by adding a line saying '#define werase w_erase' to the
stdwin.h file (in the STDWIN distribution, subdirectory H). The
GL conflict has been solved in the Python configure script by a
hack that forces use of the static version of the termcap library.
Check the newsgroup gnu.bash.bug news:gnu.bash.bug for
specific problems with the readline library (I don't read this group
but I've been told that it is the place for readline bugs).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.007.htp">Log info</A>

/ Last changed on Sat Dec  2 18:23:48 2000 by
<A HREF="mailto:trotts@llnl.gov">Issac Trotts</A>
<P>

<HR>
<H2><A NAME="3.8">3.8. Trouble with socket I/O on older Linux 1.x versions.</A></H2>
Once you've built Python, use it to run the regen script in the
Lib/plat-linux2 directory.  Apparently the files as distributed don't match the system headers on some Linux versions.
<P>
Note that this FAQ entry only applies to Linux kernel versions 1.x.y;
these are hardly around any more.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.008.htp">Log info</A>

/ Last changed on Tue Jul 30 20:05:52 2002 by
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
<P>

<HR>
<H2><A NAME="3.9">3.9. Trouble with prototypes on Ultrix.</A></H2>
Ultrix cc seems broken -- use gcc, or edit config.h to #undef
HAVE_PROTOTYPES.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.009.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.10">3.10. Other trouble building Python on platform X.</A></H2>
Please submit the details to the SourceForge bug tracker:
<P>
<PRE>
  <A HREF="http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470">http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470</A>
</PRE>
and we'll look
into it.  Please provide as many details as possible.  In particular,
if you don't tell us what type of computer and what operating system
(and version) you are using it will be difficult for us to figure out
what is the matter.  If you have compilation output logs,
please use file uploads -- don't paste everything in the message box.
<P>
In many cases, we won't have access to the same hardware or operating system version, so <I>please</I>, if you have a SourceForge account, log in before filing your report, or if you don't have an account, include an email address at which we can reach you for further questions.  Logging in to SourceForge first will also cause SourceForge to send you updates as we act on your report.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.010.htp">Log info</A>

/ Last changed on Fri Apr 27 10:53:18 2001 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="3.11">3.11. How to configure dynamic loading on Linux.</A></H2>
This is now automatic as long as your Linux version uses the ELF
object format (all recent Linuxes do).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.011.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.12">3.12. I can't get shared modules to work on Linux 2.0 (Slackware96)?</A></H2>
This is a bug in the Slackware96 release. The fix is simple: Make sure
that there is a link from /lib/libdl.so to /lib/libdl.so.1 so that the
following links are setup: /lib/libdl.so -&gt; /lib/libdl.so.1
/lib/libdl.so.1 -&gt; /lib/libdl.so.1.7.14 You may have to rerun the
configure script, after rm'ing the config.cache file, before you
attempt to rebuild python after this fix.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.012.htp">Log info</A>

/ Last changed on Wed May 21 15:45:03 1997 by
<A HREF="mailto:guido2@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="3.13">3.13. Trouble when making modules shared on Linux.</A></H2>
This happens when you have built Python for static linking and then
enable
<PRE>
  *shared*
</PRE>
in the Setup file.  Shared library code must be
compiled with "-fpic".  If a .o file for the module already exist that
was compiled for static linking, you must remove it or do "make clean"
in the Modules directory.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.013.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.013.htp">Log info</A>

/ Last changed on Fri May 23 13:42:30 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="3.14">3.14. [deleted]</A></H2>
[ancient information on threads on linux (when thread support
was not standard) used to be here]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.014.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.014.htp">Log info</A>

/ Last changed on Sun Jun  2 17:27:13 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="3.15">3.15. Errors when linking with a shared library containing C++ code.</A></H2>
Link the main Python binary with C++.  Change the definition of
LINKCC in Modules/Makefile to be your C++ compiler.  You may have to
edit config.c slightly to make it compilable with C++.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.015.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.015.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.16">3.16. Deleted</A></H2>
<P>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.016.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.016.htp">Log info</A>

/ Last changed on Tue Sep 11 16:02:22 2001 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="3.17">3.17. Deleted.</A></H2>
<P>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.017.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.017.htp">Log info</A>

/ Last changed on Tue Sep 11 15:54:57 2001 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="3.18">3.18. Compilation or link errors for the _tkinter module</A></H2>
Most likely, there's a version mismatch between the Tcl/Tk header
files (tcl.h and tk.h) and the Tcl/Tk libraries you are using e.g.
"-ltk8.0" and "-ltcl8.0" arguments for _tkinter in the Setup file).
It is possible to install several versions of the Tcl/Tk libraries,
but there can only be one version of the tcl.h and tk.h header
files.  If the library doesn't match the header, you'll get
problems, either when linking the module, or when importing it.
Fortunately, the version number is clearly stated in each file,
so this is easy to find.  Reinstalling and using the latest
version usually fixes the problem.
<P>
(Also note that when compiling unpatched Python 1.5.1 against
Tcl/Tk 7.6/4.2 or older, you get an error on Tcl_Finalize.  See
the 1.5.1 patch page at <A HREF="http://www.python.org/1.5/patches-1.5.1">http://www.python.org/1.5/patches-1.5.1</A>/.)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.018.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.018.htp">Log info</A>

/ Last changed on Thu Jun 11 00:49:14 1998 by
<A HREF="mailto:guido@python.org">Gvr</A>
<P>

<HR>
<H2><A NAME="3.19">3.19. I configured and built Python for Tcl/Tk but &quot;import Tkinter&quot; fails.</A></H2>
Most likely, you forgot to enable the line in Setup that says
"TKPATH=:$(DESTLIB)/tkinter".
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.019.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.019.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.20">3.20. [deleted]</A></H2>
[ancient information on a gcc+tkinter bug on alpha was here]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.020.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.020.htp">Log info</A>

/ Last changed on Mon Jun  3 16:46:23 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="3.21">3.21. Several common system calls are missing from the posix module.</A></H2>
Most likely, <I>all</I> test compilations run by the configure script
are failing for some reason or another.  Have a look in config.log to
see what could be the reason.  A common reason is specifying a
directory to the --with-readline option that doesn't contain the
libreadline.a file.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.021.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.021.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.22">3.22. ImportError: No module named string, on MS Windows.</A></H2>
Most likely, your PYTHONPATH environment variable should be set to
something like:
<P>
set PYTHONPATH=c:\python;c:\python\lib;c:\python\scripts
<P>
(assuming Python was installed in c:\python)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.022.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.022.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.23">3.23. Core dump on SGI when using the gl module.</A></H2>
There are conflicts between entry points in the termcap and curses
libraries and an entry point in the GL library.  There's a hack of a
fix for the termcap library if it's needed for the GNU readline
library, but it doesn't work when you're using curses.  Concluding,
you can't build a Python binary containing both the curses and gl
modules.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.023.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.023.htp">Log info</A>
<P>

<HR>
<H2><A NAME="3.24">3.24. &quot;Initializer not a constant&quot; while building DLL on MS-Windows</A></H2>
Static type object initializers in extension modules may cause compiles to
fail with an error message like "initializer not a constant".  
Fredrik Lundh &lt;<A HREF="mailto:Fredrik.Lundh@image.combitech.se">Fredrik.Lundh@image.combitech.se</A>&gt; explains:
<P>
This shows up when building DLL under MSVC.  There's two ways to
address this: either compile the module as C++, or change your code to
something like:
<P>
<PRE>
  statichere PyTypeObject bstreamtype = {
      PyObject_HEAD_INIT(NULL) /* must be set by init function */
      0,
      "bstream",
      sizeof(bstreamobject),
</PRE>
<PRE>
  ...
</PRE>
<PRE>
  void
  initbstream()
  {
      /* Patch object type */
      bstreamtype.ob_type = &amp;PyType_Type;
      Py_InitModule("bstream", functions);
      ...
  }
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.024.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.024.htp">Log info</A>

/ Last changed on Sun May 25 14:58:05 1997 by
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
<P>

<HR>
<H2><A NAME="3.25">3.25. Output directed to a pipe or file disappears on Linux.</A></H2>
Some people have reported that when they run their script
interactively, it runs great, but that when they redirect it
to a pipe or file, no output appears.
<P>
<PRE>
    % python script.py
    ...some output...
    % python script.py >file
    % cat file
    % # no output
    % python script.py | cat
    % # no output
    %
</PRE>
This was a bug in Linux kernel. It is fixed and should not appear anymore. So most Linux users are <I>not</I> affected by this.
<P>
If redirection doesn't work on your Linux system, check what shell you are using. Shells like (t)csh doesn't support redirection.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.025.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.025.htp">Log info</A>

/ Last changed on Thu Jan 16 13:38:30 2003 by
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
<P>

<HR>
<H2><A NAME="3.26">3.26. [deleted]</A></H2>
[ancient libc/linux problem was here]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.026.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.026.htp">Log info</A>

/ Last changed on Mon Jun  3 16:48:08 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="3.27">3.27. [deleted]</A></H2>
[ancient linux + threads + tk problem was described here]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.027.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.027.htp">Log info</A>

/ Last changed on Mon Jun  3 16:49:08 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="3.28">3.28. How can I test if Tkinter is working?</A></H2>
Try the following:
<P>
<PRE>
  python
  >>> import _tkinter
  >>> import Tkinter
  >>> Tkinter._test()
</PRE>
This should pop up a window with two buttons,
one "Click me" and one "Quit".
<P>
If the first statement (import _tkinter) fails, your Python
installation probably has not been configured to support Tcl/Tk.
On Unix, if you have installed Tcl/Tk, you have to rebuild Python
after editing the Modules/Setup file to enable the _tkinter module
and the TKPATH environment variable.
<P>
It is also possible to get complaints about Tcl/Tk version
number mismatches or missing TCL_LIBRARY or TK_LIBRARY
environment variables.  These have to do with Tcl/Tk installation
problems.
<P>
A common problem is to have installed versions of tcl.h and tk.h
that don't match the installed version of the Tcl/Tk libraries;
this usually results in linker errors or (when using dynamic
loading) complaints about missing symbols during loading
the shared library.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.028.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.028.htp">Log info</A>

/ Last changed on Thu Aug 28 17:01:46 1997 by
<A HREF="mailto:guido@python.org">Guido van Rossum</A>
<P>

<HR>
<H2><A NAME="3.29">3.29. Is there a way to get the interactive mode of the python interpreter to perform function/variable name completion?</A></H2>
(From a posting by Guido van Rossum)
<P>
On Unix, if you have enabled the readline module (i.e. if Emacs-style
command line editing and bash-style history works for you), you can
add this by importing the undocumented standard library module
"rlcompleter".  When completing a simple identifier, it
completes keywords, built-ins and globals in __main__; when completing
NAME.NAME..., it evaluates (!) the expression up to the last dot and
completes its attributes.
<P>
This way, you can do "import string", type "string.", hit the
completion key twice, and see the list of names defined by the
string module.
<P>
Tip: to use the tab key as the completion key, call
<P>
<PRE>
    readline.parse_and_bind("tab: complete")
</PRE>
You can put this in a ~/.pythonrc file, and set the PYTHONSTARTUP
environment variable to ~/.pythonrc.  This will cause the completion to be enabled
whenever you run Python interactively.  
<P>
Notes (see the docstring for rlcompleter.py for more information):
<P>
* The evaluation of the NAME.NAME... form may cause arbitrary 
application defined code to be executed if an object with a
__getattr__ hook is found.  Since it is the responsibility of the
application (or the user) to enable this feature, I consider this an
acceptable risk.  More complicated expressions (e.g. function calls or
indexing operations) are <I>not</I> evaluated.
<P>
* GNU readline is also used by the built-in functions input() and
raw_input(), and thus these also benefit/suffer from the complete
features.  Clearly an interactive application can benefit by
specifying its own completer function and using raw_input() for all
its input.
<P>
* When stdin is not a tty device, GNU readline is never
used, and this module (and the readline module) are silently inactive.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.029.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.029.htp">Log info</A>

/ Last changed on Fri Jun 12 09:55:24 1998 by
<A HREF="mailto:akuchlin@cnri.reston.va.us">A.M. Kuchling</A>
<P>

<HR>
<H2><A NAME="3.30">3.30. Why is the Python interpreter not built as a shared library?</A></H2>
(This is a Unix question; on Mac and Windows, it <I>is</I> a shared
library.)
<P>
It's just a nightmare to get this to work on all different platforms.
Shared library portability is a pain.  And yes, I know about GNU libtool
-- but it requires me to use its conventions for filenames etc, and it 
would require a complete and utter rewrite of all the makefile and
config tools I'm currently using.
<P>
In practice, few applications embed Python -- it's much more common to
have Python extensions, which already are shared libraries.  Also,
serious embedders often want total control over which Python version
and configuration they use so they wouldn't want to use a standard
shared library anyway.  So while the motivation of saving space
when lots of apps embed Python is nice in theory, I
doubt that it will save much in practice.  (Hence the low priority I
give to making a shared library.)
<P>
For Linux systems, the simplest method of producing libpython1.5.so seems to 
be (originally from the Minotaur project web page,  
<A HREF="http://www.equi4.com/minotaur/minotaur.html">http://www.equi4.com/minotaur/minotaur.html</A>): 
<P>
<PRE>
  make distclean 
  ./configure 
  make OPT="-fpic -O2" 
  mkdir .extract 
  (cd .extract; ar xv ../libpython1.5.a) 
  gcc -shared -o libpython1.5.so .extract/*.o 
  rm -rf .extract
</PRE>
In Python 2.3 this will be supported by the standard build routine
(at least on Linux) with --enable-shared.  Note however that there
is little advantage, and it slows down Python because of the need
for PIC code and the extra cost at startup time to find the library.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.030.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.030.htp">Log info</A>

/ Last changed on Thu May 30 13:36:55 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="3.31">3.31. Build with GCC on Solaris 2.6 (SunOS 5.6) fails</A></H2>
If you have upgraded Solaris 2.5 or 2.5.1 to Solaris 2.6,
but you have not upgraded
your GCC installation, the compile may fail, e.g. like this:
<P>
<PRE>
 In file included from /usr/include/sys/stream.h:26,
                  from /usr/include/netinet/in.h:38,
                  from /usr/include/netdb.h:96,
                  from ./socketmodule.c:121:
 /usr/include/sys/model.h:32: #error "No DATAMODEL_NATIVE specified"
</PRE>
Solution: rebuild GCC for Solaris 2.6. 
You might be able to simply re-run fixincludes, but
people have had mixed success with doing that.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.031.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.031.htp">Log info</A>

/ Last changed on Wed Oct 21 11:18:46 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="3.32">3.32. Running &quot;make clean&quot; seems to leave problematic files that cause subsequent builds to fail.</A></H2>
Use "make clobber" instead.  
<P>
Use "make clean" to reduce the size of the source/build directory
after you're happy with your build and installation.
If you have already tried to build python and you'd like to start 
over, you should use "make clobber".  It does a "make clean" and also 
removes files such as the partially built Python library from a previous build.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.032.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.032.htp">Log info</A>

/ Last changed on Thu Jun 24 20:39:26 1999 by
<A HREF="mailto:tbryan@python.net">TAB</A>
<P>

<HR>
<H2><A NAME="3.33">3.33. Submitting bug reports and patches</A></H2>
To report a bug or submit a patch, please use the relevant service
from the Python project at SourceForge.
<P>
Bugs: <A HREF="http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470">http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470</A>
<P>
Patches: <A HREF="http://sourceforge.net/tracker/?group_id=5470&amp;atid=305470">http://sourceforge.net/tracker/?group_id=5470&amp;atid=305470</A>
<P>
If you have a SourceForge account, please log in before submitting your bug report; this will make it easier for us to contact you regarding your report in the event we have follow-up questions.  It will also enable SourceForge to send you update information as we act on your bug.  If you do not have a SourceForge account, please consider leaving your name and email address as part of the report.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.033.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.033.htp">Log info</A>

/ Last changed on Fri Apr 27 10:58:26 2001 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="3.34">3.34. I can't load shared libraries under Python 1.5.2, Solaris 7, and gcc 2.95.2</A></H2>
When trying to load shared libraries, you may see errors like:
ImportError: ld.so.1: python: fatal: relocation error: file /usr/local/lib/python1.5/site-packages/Perp/util/du_SweepUtilc.so:
<PRE>
 symbol PyExc_RuntimeError: referenced symbol not found
</PRE>
<P>
There is a problem with the configure script for Python 1.5.2
under Solaris 7 with gcc 2.95 .  configure should set the make variable
LINKFORSHARED=-Xlinker -export-dynamic
<P>
<P>
in Modules/Makefile, 
<P>
Manually add this line to the Modules/Makefile.
This builds a Python executable that can load shared library extensions (xxx.so) .
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.034.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.034.htp">Log info</A>

/ Last changed on Mon Feb 19 10:37:05 2001 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="3.35">3.35. In the regression test, test___all__ fails for the profile module. What's wrong?</A></H2>
If you have been using the profile module, and have properly calibrated a copy of the module as described in the documentation for the profiler:
<P>
<A HREF="http://www.python.org/doc/current/lib/profile-calibration.html">http://www.python.org/doc/current/lib/profile-calibration.html</A>
<P>
then it is possible that the regression test "test___all__" will fail if you run the regression test manually rather than using "make test" in the Python source directory.  This will happen if you have set your PYTHONPATH environment variable to include the directory containing your calibrated profile module.  You have probably calibrated the profiler using an older version of the profile module which does not define the __all__ value, added to the module as of Python 2.1.
<P>
The problem can be fixed by removing the old calibrated version of the profile module and using the latest version to do a fresh calibration. In general, you will need to re-calibrate for each version of Python anyway, since the performance characteristics can change in subtle ways that impact profiling.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.035.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.035.htp">Log info</A>

/ Last changed on Fri Apr 27 10:44:10 2001 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="3.36">3.36. relocations remain against allocatable but non-writable sections</A></H2>
This linker error occurs on Solaris if you attempt to build an extension module which incorporates position-dependent (non-PIC) code. A common source of problems is that a static library (.a file), such as libreadline.a or libcrypto.a is linked with the extension module. The error specifically occurs when using gcc as the compiler, but /usr/ccs/bin/ld as the linker.
<P>
The following solutions and work-arounds are known:
<P>
1. Rebuild the libraries (libreadline, libcrypto) with -fPIC (-KPIC if using the system compiler). This is recommended; all object files in a shared library should be position-independent.
<P>
2. Statically link the extension module and its libraries into the Python interpreter, by editing Modules/Setup.
<P>
3. Use GNU ld instead of /usr/ccs/bin/ld; GNU ld will accept non-PIC code in shared libraries (and mark the section writable)
<P>
4. Pass -mimpure-text to GCC when linking the module. This will force gcc to not pass -z text to ld; in turn, ld will make all text sections writable.
<P>
Options 3 and 4 are not recommended, since the ability to share code across processes is lost.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq03.036.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq03.036.htp">Log info</A>

/ Last changed on Tue Jan 29 12:05:11 2002 by
<A HREF="mailto:loewis@informatik.hu-berlin.de">Martin v. Lwis</A>
<P>

<HR>
<H1>4. Programming in Python</H1>

<HR>
<H2><A NAME="4.1">4.1. Is there a source code level debugger with breakpoints, step, etc.?</A></H2>
Yes.  
<P>
Module pdb is a rudimentary but adequate console-mode debugger for Python. It is part of the standard Python library, and is documented in the Library Reference Manual. (You can also write your own debugger by using the code for pdb as an example.)
<P>
The IDLE interactive development environment, which is part of the standard Python distribution (normally available in Tools/idle), includes a graphical debugger.  There is documentation for the IDLE debugger at <A HREF="http://www.python.org/idle/doc/idle2.html#Debugger">http://www.python.org/idle/doc/idle2.html#Debugger</A>
<P>
Pythonwin is a Python IDE that includes a GUI debugger based on bdb.  The Pythonwin debugger colors breakpoints and has quite a few cool features (including debugging non-Pythonwin programs).  A reference can be found at <A HREF="http://www.python.org/ftp/python/pythonwin/pwindex.html">http://www.python.org/ftp/python/pythonwin/pwindex.html</A>
More recent versions of PythonWin are available as a part of the ActivePython distribution (see   <A HREF="http://www.activestate.com/Products/ActivePython/index.html">http://www.activestate.com/Products/ActivePython/index.html</A>).
<P>
Pydb is a version of the standard Python debugger pdb, modified for use with DDD (Data Display Debugger), a popular graphical debugger front end.  Pydb can be found at  <A HREF="http://packages.debian.org/unstable/devel/pydb.html">http://packages.debian.org/unstable/devel/pydb.html</A>
and DDD can be found at <A HREF="http://www.gnu.org/software/ddd">http://www.gnu.org/software/ddd</A>/
<P>
There are a number of commmercial Python IDEs that include graphical debuggers.  They include:
<P>
<PRE>
 * Wing IDE (<A HREF="http://wingide.com">http://wingide.com</A>/) 
 * Komodo IDE (<A HREF="http://www.activestate.com/Products/Komodo">http://www.activestate.com/Products/Komodo</A>/)
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.001.htp">Log info</A>

/ Last changed on Tue Jan 28 01:43:41 2003 by
<A HREF="mailto:steve@ferg.org">Stephen Ferg</A>
<P>

<HR>
<H2><A NAME="4.2">4.2. Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)? (Also phrased as: Can I use a built-in type as base class?)</A></H2>
In Python 2.2, you can inherit from builtin classes such as int, list, dict, etc.
<P>
In previous versions of Python, you can easily create a Python class which serves as a wrapper around a built-in object, e.g. (for dictionaries):
<P>
<PRE>
        # A user-defined class behaving almost identical
        # to a built-in dictionary.
        class UserDict:
                def __init__(self): self.data = {}
                def __repr__(self): return repr(self.data)
                def __cmp__(self, dict):
                        if type(dict) == type(self.data):
                                return cmp(self.data, dict)
                        else:
                                return cmp(self.data, dict.data)
                def __len__(self): return len(self.data)
                def __getitem__(self, key): return self.data[key]
                def __setitem__(self, key, item): self.data[key] = item
                def __delitem__(self, key): del self.data[key]
                def keys(self): return self.data.keys()
                def items(self): return self.data.items()
                def values(self): return self.data.values()
                def has_key(self, key): return self.data.has_key(key)
</PRE>
A2. See Jim Fulton's ExtensionClass for an example of a mechanism
which allows you to have superclasses which you can inherit from in
Python -- that way you can have some methods from a C superclass (call
it a mixin) and some methods from either a Python superclass or your
subclass.  ExtensionClass is distributed as a part of Zope (see
<A HREF="http://www.zope.org">http://www.zope.org</A>), but will be phased out with Zope 3, since
Zope 3 uses Python 2.2 or later which supports direct inheritance
from built-in types.  Here's a link to the original paper about
ExtensionClass:
<A HREF="http://debian.acm.ndsu.nodak.edu/doc/python-extclass/ExtensionClass.html">http://debian.acm.ndsu.nodak.edu/doc/python-extclass/ExtensionClass.html</A>
<P>
A3. The Boost Python Library (BPL, <A HREF="http://www.boost.org/libs/python/doc/index.html">http://www.boost.org/libs/python/doc/index.html</A>)
provides a way of doing this from C++ (i.e. you can inherit from an
extension class written in C++ using the BPL).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.002.htp">Log info</A>

/ Last changed on Tue May 28 21:09:52 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.3">4.3. Is there a curses/termcap package for Python?</A></H2>
The standard Python source distribution comes with a curses module in
the Modules/ subdirectory, though it's not compiled by default (note
that this is not available in the Windows distribution -- there is
no curses module for Windows).
<P>
In Python versions before 2.0 the module only supported plain curses; 
you couldn't use ncurses features like colors with it (though it would 
link with ncurses).
<P>
In Python 2.0, the curses module has been greatly extended, starting 
from Oliver Andrich's enhanced version, to provide many additional 
functions from ncurses and SYSV curses, such as colour, alternative
character set support, pads, and mouse support. This means the
module is no longer compatible with operating systems that only
have BSD curses, but there don't seem to be any currently
maintained OSes that fall into this category.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.003.htp">Log info</A>

/ Last changed on Sun Jun 23 20:24:06 2002 by
<A HREF="mailto:tim.one@comcast.net">Tim Peters</A>
<P>

<HR>
<H2><A NAME="4.4">4.4. Is there an equivalent to C's onexit() in Python?</A></H2>
For Python 2.0: The new atexit module provides a register function that
is similar to C's onexit. See the Library Reference for details. For
2.0 you should <I>not</I> assign to sys.exitfunc!
<P>
For Python 1.5.2: You need to import sys and assign a function to 
sys.exitfunc, it will be called when your program exits, is 
killed by an unhandled exception, or (on UNIX) receives a 
SIGHUP or SIGTERM signal.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.004.htp">Log info</A>

/ Last changed on Thu Dec 28 12:14:55 2000 by
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
<P>

<HR>
<H2><A NAME="4.5">4.5. [deleted]</A></H2>
[python used to lack nested scopes, it was explained here]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.005.htp">Log info</A>

/ Last changed on Thu Mar 21 05:18:22 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="4.6">4.6. How do I iterate over a sequence in reverse order?</A></H2>
If it is a list, the fastest solution is
<P>
<PRE>
        list.reverse()
        try:
                for x in list:
                        "do something with x"
        finally:
                list.reverse()
</PRE>
This has the disadvantage that while you are in the loop, the list
is temporarily reversed.  If you don't like this, you can make a copy.
This appears expensive but is actually faster than other solutions:
<P>
<PRE>
        rev = list[:]
        rev.reverse()
        for x in rev:
                &lt;do something with x&gt;
</PRE>
If it's not a list, a more general but slower solution is:
<P>
<PRE>
        for i in range(len(sequence)-1, -1, -1):
                x = sequence[i]
                &lt;do something with x&gt;
</PRE>
A more elegant solution, is to define a class which acts as a sequence
and yields the elements in reverse order (solution due to Steve
Majewski):
<P>
<PRE>
        class Rev:
                def __init__(self, seq):
                        self.forw = seq
                def __len__(self):
                        return len(self.forw)
                def __getitem__(self, i):
                        return self.forw[-(i + 1)]
</PRE>
You can now simply write:
<P>
<PRE>
        for x in Rev(list):
                &lt;do something with x&gt;
</PRE>
Unfortunately, this solution is slowest of all, due to the method
call overhead...
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.006.htp">Log info</A>

/ Last changed on Sun May 25 21:10:50 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.7">4.7. My program is too slow. How do I speed it up?</A></H2>
That's a tough one, in general.  There are many tricks to speed up
Python code; I would consider rewriting parts in C only as a last
resort.  One thing to notice is that function and (especially) method
calls are rather expensive; if you have designed a purely OO interface
with lots of tiny functions that don't do much more than get or set an
instance variable or call another method, you may consider using a
more direct way, e.g. directly accessing instance variables.  Also see
the standard module "profile" (described in the Library Reference
manual) which makes it possible to find out where
your program is spending most of its time (if you have some patience
-- the profiling itself can slow your program down by an order of
magnitude).
<P>
Remember that many standard optimization heuristics you
may know from other programming experience may well apply
to Python.  For example it may be faster to send output to output
devices using larger writes rather than smaller ones in order to
avoid the overhead of kernel system calls.  Thus CGI scripts
that write all output in "one shot" may be notably faster than
those that write lots of small pieces of output.
<P>
Also, be sure to use "aggregate" operations where appropriate.
For example the "slicing" feature allows programs to chop up
lists and other sequence objects in a single tick of the interpreter
mainloop using highly optimized C implementations.  Thus to 
get the same effect as
<P>
<PRE>
  L2 = []
  for i in range[3]:
       L2.append(L1[i])
</PRE>
it is much shorter and far faster to use
<P>
<PRE>
  L2 = list(L1[:3]) # "list" is redundant if L1 is a list.
</PRE>
Note that the map() function, particularly used with
builtin methods or builtin functions can be a convenient
accelerator.  For example to pair the elements of two
lists together:
<P>
<PRE>
  >>> map(None, [1,2,3], [4,5,6])
  [(1, 4), (2, 5), (3, 6)]
</PRE>
or to compute a number of sines:
<P>
<PRE>
  >>> map( math.sin, (1,2,3,4))
  [0.841470984808, 0.909297426826, 0.14112000806,   -0.756802495308]
</PRE>
The map operation completes very quickly in such cases.
<P>
Other examples of aggregate operations include the join and split
methods of string objects.  For example if s1..s7 are large (10K+) strings then
"".join([s1,s2,s3,s4,s5,s6,s7]) may be far faster than
the more obvious s1+s2+s3+s4+s5+s6+s7, since the "summation"
will compute many subexpressions, whereas join does all
copying in one pass.  For manipulating strings also consider the 
regular expression libraries and the "substitution" operations
String % tuple and String % dictionary.  Also be sure to use
the list.sort builtin method to do sorting, and see FAQ's 4.51
and 4.59 for examples of moderately advanced usage -- list.sort beats
other techniques for sorting in all but the most extreme
circumstances.
<P>
There are many other aggregate operations
available in the standard libraries and in contributed libraries
and extensions.
<P>
Another common trick is to "push loops into functions or methods."
For example suppose you have a program that runs slowly and you
use the profiler (profile.run) to determine that a Python function ff
is being called lots of times.  If you notice that ff 
<P>
<PRE>
   def ff(x):
       ...do something with x computing result...
       return result
</PRE>
tends to be called in loops like (A)
<P>
<PRE>
   list = map(ff, oldlist)
</PRE>
or (B)
<P>
<PRE>
   for x in sequence:
       value = ff(x)
       ...do something with value...
</PRE>
then you can often eliminate function call overhead by rewriting
ff to
<P>
<PRE>
   def ffseq(seq):
       resultseq = []
       for x in seq:
           ...do something with x computing result...
           resultseq.append(result)
       return resultseq
</PRE>
and rewrite (A) to
<P>
<PRE>
    list = ffseq(oldlist)
</PRE>
and (B) to
<P>
<PRE>
    for value in ffseq(sequence):
        ...do something with value...
</PRE>
Other single calls ff(x) translate to ffseq([x])[0] with little
penalty.  Of course this technique is not always appropriate
and there are other variants, which you can figure out.
<P>
You can gain some performance by explicitly storing the results of
a function or method lookup into a local variable.  A loop like
<P>
<PRE>
    for key in token:
        dict[key] = dict.get(key, 0) + 1
</PRE>
resolves dict.get every iteration.  If the method isn't going to
change, a faster implementation is
<P>
<PRE>
    dict_get = dict.get  # look up the method once
    for key in token:
        dict[key] = dict_get(key, 0) + 1
</PRE>
Default arguments can be used to determine values once, at
compile time instead of at run time.  This can only be done for
functions or objects which will not be changed during program
execution, such as replacing
<P>
<PRE>
    def degree_sin(deg):
        return math.sin(deg * math.pi / 180.0)
</PRE>
with
<P>
<PRE>
    def degree_sin(deg, factor = math.pi/180.0, sin = math.sin):
        return sin(deg * factor)
</PRE>
Because this trick uses default arguments for terms which should
not be changed, it should only be used when you are not concerned
with presenting a possibly confusing API to your users.
<P>
<P>
For an anecdote related to optimization, see
<P>
<PRE>
	<A HREF="http://www.python.org/doc/essays/list2str.html">http://www.python.org/doc/essays/list2str.html</A>
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.007.htp">Log info</A>

/ Last changed on Mon Jun  3 01:03:54 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="4.8">4.8. When I have imported a module, then edit it, and import it again (into the same Python process), the changes don't seem to take place. What is going on?</A></H2>
For reasons of efficiency as well as consistency, Python only reads
the module file on the first time a module is imported.  (Otherwise a
program consisting of many modules, each of which imports the same
basic module, would read the basic module over and over again.)  To
force rereading of a changed module, do this:
<P>
<PRE>
        import modname
        reload(modname)
</PRE>
Warning: this technique is not 100% fool-proof.  In particular,
modules containing statements like
<P>
<PRE>
        from modname import some_objects
</PRE>
will continue to work with the old version of the imported objects.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.008.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.9">4.9. How do I find the current module name?</A></H2>
A module can find out its own module name by looking at the
(predefined) global variable __name__.  If this has the value
'__main__' you are running as a script.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.009.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.10">4.10. I have a module in which I want to execute some extra code when it is run as a script. How do I find out whether I am running as a script?</A></H2>
See the previous question.  E.g. if you put the following on the
last line of your module, main() is called only when your module is
running as a script:
<P>
<PRE>
        if __name__ == '__main__': main()
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.010.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.11">4.11. I try to run a program from the Demo directory but it fails with ImportError: No module named ...; what gives?</A></H2>
This is probably an optional module (written in C!) which hasn't
been configured on your system.  This especially happens with modules
like "Tkinter", "stdwin", "gl", "Xt" or "Xm".  For Tkinter, STDWIN and
many other modules, see Modules/Setup.in for info on how to add these
modules to your Python, if it is possible at all.  Sometimes you will
have to ftp and build another package first (e.g. Tcl and Tk for Tkinter).
Sometimes the module only works on specific platforms (e.g. gl only works
on SGI machines).
<P>
NOTE: if the complaint is about "Tkinter" (upper case T) and you have
already configured module "tkinter" (lower case t), the solution is
<I>not</I> to rename tkinter to Tkinter or vice versa.  There is probably
something wrong with your module search path.  Check out the value of
sys.path.
<P>
For X-related modules (Xt and Xm) you will have to do more work: they
are currently not part of the standard Python distribution.  You will
have to ftp the Extensions tar file, i.e.
<A HREF="ftp://ftp.python.org/pub/python/src/X-extension.tar.gz">ftp://ftp.python.org/pub/python/src/X-extension.tar.gz</A> and follow
the instructions there.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.011.htp">Log info</A>

/ Last changed on Wed Feb 12 21:31:08 2003 by
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
<P>

<HR>
<H2><A NAME="4.12">4.12. [deleted]</A></H2>
[stdwin (long dead windowing library) entry deleted]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.012.htp">Log info</A>

/ Last changed on Thu Mar 21 08:30:13 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="4.13">4.13. What GUI toolkits exist for Python?</A></H2>
Depending on what platform(s) you are aiming at, there are several.
<P>
Currently supported solutions:
<P>
Cross-platform:
<P>
Tk:
<P>
There's a neat object-oriented interface to the Tcl/Tk widget set,
called Tkinter.  It is part of the standard Python distribution and
well-supported -- all you need to do is build and install Tcl/Tk and
enable the _tkinter module and the TKPATH definition in Modules/Setup
when building Python.  This is probably the easiest to install and
use, and the most complete widget set.  It is also very likely that in
the future the standard Python GUI API will be based on or at least
look very much like the Tkinter interface.  For more info about Tk,
including pointers to the source, see the Tcl/Tk home page at
<A HREF="http://www.scriptics.com">http://www.scriptics.com</A>.  Tcl/Tk is now fully
portable to the Mac and Windows platforms (NT and 95 only); you need
Python 1.4beta3 or later and Tk 4.1patch1 or later.
<P>
wxWindows:
<P>
There's an interface to wxWindows called wxPython.  wxWindows is a 
portable GUI class library written in C++.  It supports GTK, Motif, 
MS-Windows and Mac as targets.  Ports to other platforms are being 
contemplated or have already had some work done on them.  wxWindows 
preserves the look and feel of the underlying graphics toolkit, and 
there is quite a rich widget set and collection of GDI classes.  
See the wxWindows page at <A HREF="http://www.wxwindows.org">http://www.wxwindows.org</A>/ for more details.  
wxPython is a python extension module that wraps many of the wxWindows 
C++ classes, and is quickly gaining popularity amongst Python 
developers.  You can get wxPython as part of the source or CVS 
distribution of wxWindows, or directly from its home page at 
<A HREF="http://alldunn.com/wxPython">http://alldunn.com/wxPython</A>/.
<P>
Gtk+:
<P>
PyGtk bindings for the Gtk+ Toolkit by James Henstridge exist; see <A HREF="ftp://ftp.daa.com.au/pub/james/python">ftp://ftp.daa.com.au/pub/james/python</A>/.  Note that there are two incompatible bindings.  If you are using Gtk+ 1.2.x you should get the 0.6.x PyGtk bindings from
<P>
<PRE>
    <A HREF="ftp://ftp.gtk.org/pub/python/v1.2">ftp://ftp.gtk.org/pub/python/v1.2</A>
</PRE>
If you plan to use Gtk+ 2.0 with Python (highly recommended if you are just starting with Gtk), get the most recent distribution from
<P>
<PRE>
    <A HREF="ftp://ftp.gtk.org/pub/python/v2.0">ftp://ftp.gtk.org/pub/python/v2.0</A>
</PRE>
If you are adventurous, you can also check out the source from the Gnome CVS repository.  Set your CVS directory to :pserver:<A HREF="mailto:anonymous@anoncvs.gnome.org">anonymous@anoncvs.gnome.org</A>:/cvs/gnome and check the gnome-python module out from the repository.
<P>
Other:
<P>
There are also bindings available for the Qt toolkit (PyQt), and for KDE (PyKDE); see <A HREF="http://www.thekompany.com/projects/pykde">http://www.thekompany.com/projects/pykde</A>/.
<P>
For OpenGL bindings, see <A HREF="http://starship.python.net/~da/PyOpenGL">http://starship.python.net/~da/PyOpenGL</A>.
<P>
Platform specific:
<P>
The Mac port has a rich and ever-growing set of modules that support
the native Mac toolbox calls.  See the documentation that comes with
the Mac port.  See <A HREF="ftp://ftp.python.org/pub/python/mac">ftp://ftp.python.org/pub/python/mac</A>.  Support
by Jack Jansen <A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>.
<P>
Pythonwin by Mark Hammond (<A HREF="mailto:MHammond@skippinet.com.au">MHammond@skippinet.com.au</A>)
includes an interface to the Microsoft Foundation
Classes and a Python programming environment using it that's written
mostly in Python.  See <A HREF="http://www.python.org/windows">http://www.python.org/windows</A>/.
<P>
There's an object-oriented GUI based on the Microsoft Foundation
Classes model called WPY, supported by Jim Ahlstrom <A HREF="mailto:jim@interet.com">jim@interet.com</A>.
Programs written in WPY run unchanged and with native look and feel on
Windows NT/95, Windows 3.1 (using win32s), and on Unix (using Tk).
Source and binaries for Windows and Linux are available in
<A HREF="ftp://ftp.python.org/pub/python/wpy">ftp://ftp.python.org/pub/python/wpy</A>/.
<P>
Obsolete or minority solutions:
<P>
There's an interface to X11, including the Athena and Motif widget
sets (and a few individual widgets, like Mosaic's HTML widget and
SGI's GL widget) available from
<A HREF="ftp://ftp.python.org/pub/python/src/X-extension.tar.gz">ftp://ftp.python.org/pub/python/src/X-extension.tar.gz</A>.
Support by Sjoerd Mullender <A HREF="mailto:sjoerd@cwi.nl">sjoerd@cwi.nl</A>.
<P>
On top of the X11 interface there's the vpApp
toolkit by Per Spilling, now also maintained by Sjoerd Mullender
<A HREF="mailto:sjoerd@cwi.nl">sjoerd@cwi.nl</A>.  See <A HREF="ftp://ftp.cwi.nl/pub/sjoerd/vpApp.tar.gz">ftp://ftp.cwi.nl/pub/sjoerd/vpApp.tar.gz</A>.
<P>
For SGI IRIX only, there are unsupported interfaces to the complete
GL (Graphics Library -- low level but very good 3D capabilities) as
well as to FORMS (a buttons-and-sliders-etc package built on top of GL
by Mark Overmars -- ftp'able from
<A HREF="ftp://ftp.cs.ruu.nl/pub/SGI/FORMS">ftp://ftp.cs.ruu.nl/pub/SGI/FORMS</A>/).  This is probably also
becoming obsolete, as OpenGL takes over (see above).
<P>
There's an interface to STDWIN, a platform-independent low-level
windowing interface for Mac and X11.  This is totally unsupported and
rapidly becoming obsolete.  The STDWIN sources are at
<A HREF="ftp://ftp.cwi.nl/pub/stdwin">ftp://ftp.cwi.nl/pub/stdwin</A>/.
<P>
There is an interface to WAFE, a Tcl interface to the X11
Motif and Athena widget sets.  WAFE is at
<A HREF="http://www.wu-wien.ac.at/wafe/wafe.html">http://www.wu-wien.ac.at/wafe/wafe.html</A>.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.013.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.013.htp">Log info</A>

/ Last changed on Mon May 13 21:40:39 2002 by
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
<P>

<HR>
<H2><A NAME="4.14">4.14. Are there any interfaces to database packages in Python?</A></H2>
Yes!  See the Database Topic Guide at 
<A HREF="http://www.python.org/topics/database">http://www.python.org/topics/database</A>/ for details.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.014.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.014.htp">Log info</A>

/ Last changed on Tue Jan  4 20:12:19 2000 by
<A HREF="mailto:bwarsaw@python.org">Barney Warplug</A>
<P>

<HR>
<H2><A NAME="4.15">4.15. Is it possible to write obfuscated one-liners in Python?</A></H2>
Yes.  See the following three examples, due to Ulf Bartelt:
<P>
<PRE>
        # Primes &lt; 1000
        print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
        map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
</PRE>
<PRE>
        # First 10 Fibonacci numbers
        print map(lambda x,f=lambda x,f:(x&lt;=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
        range(10))
</PRE>
<PRE>
        # Mandelbrot set
        print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
        Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
        Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
        i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k&lt;=0)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):f(xc,yc,x,y,k,f):chr(
        64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
        ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
        #    \___ ___/  \___ ___/  |   |   |__ lines on screen
        #        V          V      |   |______ columns on screen
        #        |          |      |__________ maximum of "iterations"
        #        |          |_________________ range on y axis
        #        |____________________________ range on x axis
</PRE>
Don't try this at home, kids!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.015.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.015.htp">Log info</A>

/ Last changed on Wed May 21 15:48:33 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.16">4.16. Is there an equivalent of C's &quot;?:&quot; ternary operator?</A></H2>
Not directly.  In many cases you can mimic a?b:c with "a and b or
c", but there's a flaw: if b is zero (or empty, or None -- anything
that tests false) then c will be selected instead.  In many cases you
can prove by looking at the code that this can't happen (e.g. because
b is a constant or has a type that can never be false), but in general
this can be a problem.
<P>
Tim Peters (who wishes it was Steve Majewski) suggested the following
solution: (a and [b] or [c])[0].  Because [b] is a singleton list it
is never false, so the wrong path is never taken; then applying [0] to
the whole thing gets the b or c that you really wanted.  Ugly, but it
gets you there in the rare cases where it is really inconvenient to
rewrite your code using 'if'.
<P>
As a last resort it is possible to implement the "?:" operator as a function:
<P>
<PRE>
    def q(cond,on_true,on_false):
        from inspect import isfunction
</PRE>
<PRE>
        if cond:
            if not isfunction(on_true): return on_true
            else: return apply(on_true)
        else:
            if not isfunction(on_false): return on_false 
            else: return apply(on_false)
</PRE>
In most cases you'll pass b and c directly: q(a,b,c).  To avoid evaluating b
or c when they shouldn't be, encapsulate them
within a lambda function, e.g.: q(a,lambda: b, lambda: c).
<P>
<P>
<P>
It has been asked <I>why</I> Python has no if-then-else expression,
since most language have one; it is a frequently requested feature.
<P>
There are several possible answers: just as many languages do
just fine without one; it can easily lead to less readable code;
no sufficiently "Pythonic" syntax has been discovered; a search
of the standard library found remarkably few places where using an
if-then-else expression would make the code more understandable.
<P>
Nevertheless, in an effort to decide once and for all whether
an if-then-else expression should be added to the language,
PEP 308 (<A HREF="http://www.python.org/peps/pep-0308.html">http://www.python.org/peps/pep-0308.html</A>) has been
put forward, proposing a specific syntax.  The community can
now vote on this issue.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.016.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.016.htp">Log info</A>

/ Last changed on Fri Feb  7 19:41:13 2003 by
<A HREF="mailto:goodger@python.org">David Goodger</A>
<P>

<HR>
<H2><A NAME="4.17">4.17. My class defines __del__ but it is not called when I delete the object.</A></H2>
There are several possible reasons for this.
<P>
The del statement does not necessarily call __del__ -- it simply
decrements the object's reference count, and if this reaches zero
__del__ is called.
<P>
If your data structures contain circular links (e.g. a tree where
each child has a parent pointer and each parent has a list of
children) the reference counts will never go back to zero.  You'll
have to define an explicit close() method which removes those
pointers.  Please don't ever call __del__ directly -- __del__ should
call close() and close() should make sure that it can be called more
than once for the same object.
<P>
If the object has ever been a local variable (or argument, which is
really the same thing) to a function that caught an expression in an
except clause, chances are that a reference to the object still exists
in that function's stack frame as contained in the stack trace.
Normally, deleting (better: assigning None to) sys.exc_traceback will
take care of this.  If a stack was printed for an unhandled
exception in an interactive interpreter, delete sys.last_traceback
instead.
<P>
There is code that deletes all objects when the interpreter exits,
but it is not called if your Python has been configured to support
threads (because other threads may still be active).  You can define
your own cleanup function using sys.exitfunc (see question 4.4).
<P>
Finally, if your __del__ method raises an exception, a warning message is printed to sys.stderr.
<P>
<P>
Starting with Python 2.0, a garbage collector periodically reclaims the space used by most cycles with no external references. (See the "gc" module documentation for details.) There <I>are</I>, however, pathological cases where it can be expected to fail. Moreover, the garbage collector runs some time after the last reference to your data structure vanishes, so your __del__ method may be called at an inconvenient and random time. This is inconvenient if you're trying to reproduce a problem. Worse, the order in which object's __del__ methods are executed is arbitrary.
<P>
Another way to avoid cyclical references is to use the "weakref" module, which allows you to point to objects without incrementing their reference count. Tree data structures, for instance, should use weak references for their parent  and sibling pointers (if they need them!).
<P>
Question 6.14 is intended to explain the new garbage collection algorithm.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.017.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.017.htp">Log info</A>

/ Last changed on Mon Jun 10 15:27:28 2002 by
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
<P>

<HR>
<H2><A NAME="4.18">4.18. How do I change the shell environment for programs called using os.popen() or os.system()? Changing os.environ doesn't work.</A></H2>
You must be using either a version of python before 1.4, or on a
(rare) system that doesn't have the putenv() library function.
<P>
Before Python 1.4, modifying the environment passed to subshells was
left out of the interpreter because there seemed to be no
well-established portable way to do it (in particular, some systems,
have putenv(), others have setenv(), and some have none at all).  As
of Python 1.4, almost all Unix systems <I>do</I> have putenv(), and so does
the Win32 API, and thus the os module was modified so that changes to
os.environ are trapped and the corresponding putenv() call is made.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.018.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.018.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.19">4.19. What is a class?</A></H2>
A class is the particular object type created by executing
a class statement.  Class objects are used as templates, to create
instance objects, which embody both the data structure
(attributes) and program routines (methods) specific to a datatype.
<P>
A class can be based on one or more other classes, called its base
class(es). It then inherits the attributes and methods of its base classes. This allows an object model to be successively refined
by inheritance.
<P>
The term "classic class" is used to refer to the original
class implementation in Python. One problem with classic
classes is their inability to use the built-in data types
(such as list and dictionary) as base classes. Starting
with Python 2.2 an attempt is in progress to unify user-defined
classes and built-in types. It is now possible to declare classes
that inherit from built-in types.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.019.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.019.htp">Log info</A>

/ Last changed on Mon May 27 01:31:21 2002 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="4.20">4.20. What is a method?</A></H2>
A method is a function that you normally call as
x.name(arguments...) for some object x.  The term is used for methods
of classes and class instances as well as for methods of built-in
objects.  (The latter have a completely different implementation and
only share the way their calls look in Python code.)  Methods of
classes (and class instances) are defined as functions inside the
class definition.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.020.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.020.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.21">4.21. What is self?</A></H2>
Self is merely a conventional name for the first argument of a
method -- i.e. a function defined inside a class definition.  A method
defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
some instance x of the class in which the definition occurs;
the called method will think it is called as meth(x, a, b, c).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.021.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.021.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.22">4.22. What is an unbound method?</A></H2>
An unbound method is a method defined in a class that is not yet
bound to an instance.  You get an unbound method if you ask for a
class attribute that happens to be a function. You get a bound method
if you ask for an instance attribute.  A bound method knows which
instance it belongs to and calling it supplies the instance automatically;
an unbound method only knows which class it wants for its first
argument (a derived class is also OK).  Calling an unbound method
doesn't "magically" derive the first argument from the context -- you
have to provide it explicitly.
<P>
Trivia note regarding bound methods:  each reference to a bound
method of a particular object creates a bound method object.  If you
have two such references (a = inst.meth; b = inst.meth), they will
compare equal (a == b) but are not the same (a is not b).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.022.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.022.htp">Log info</A>

/ Last changed on Wed May  6 18:07:25 1998 by
<A HREF="mailto:clarence@avtel.com">Clarence Gardner</A>
<P>

<HR>
<H2><A NAME="4.23">4.23. How do I call a method defined in a base class from a derived class that overrides it?</A></H2>
If your class definition starts with "class Derived(Base): ..."
then you can call method meth defined in Base (or one of Base's base
classes) as Base.meth(self, arguments...).  Here, Base.meth is an
unbound method (see previous question).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.023.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.023.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.24">4.24. How do I call a method from a base class without using the name of the base class?</A></H2>
DON'T DO THIS.  REALLY.  I MEAN IT.  It appears that you could call
self.__class__.__bases__[0].meth(self, arguments...) but this fails when
a doubly-derived method is derived from your class: for its instances,
self.__class__.__bases__[0] is your class, not its base class -- so
(assuming you are doing this from within Derived.meth) you would start
a recursive call.
<P>
Often when you want to do this you are forgetting that classes
are first class in Python.  You can "point to" the class you want
to delegate an operation to either at the instance or at the
subclass level.  For example if you want to use a "glorp"
operation of a superclass you can point to the right superclass
to use.
<P>
<PRE>
  class subclass(superclass1, superclass2, superclass3):
      delegate_glorp = superclass2
      ...
      def glorp(self, arg1, arg2):
            ... subclass specific stuff ...
            self.delegate_glorp.glorp(self, arg1, arg2)
       ...
</PRE>
<PRE>
  class subsubclass(subclass):
       delegate_glorp = superclass3
       ...
</PRE>
Note, however that setting delegate_glorp to subclass in
subsubclass would cause an infinite recursion on subclass.delegate_glorp.  Careful!  Maybe you are getting too fancy for your own good.  Consider simplifying the design (?).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.024.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.024.htp">Log info</A>

/ Last changed on Mon Jul 28 13:58:22 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="4.25">4.25. How can I organize my code to make it easier to change the base class?</A></H2>
You could define an alias for the base class, assign the real base
class to it before your class definition, and use the alias throughout
your class.  Then all you have to change is the value assigned to the
alias.  Incidentally, this trick is also handy if you want to decide
dynamically (e.g. depending on availability of resources) which base
class to use.  Example:
<P>
<PRE>
        BaseAlias = &lt;real base class&gt;
        class Derived(BaseAlias):
                def meth(self):
                        BaseAlias.meth(self)
                        ...
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.025.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.025.htp">Log info</A>

/ Last changed on Wed May 21 15:49:57 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.26">4.26. How can I find the methods or attributes of an object?</A></H2>
This depends on the object type.
<P>
For an instance x of a user-defined class, instance attributes are
found in the dictionary x.__dict__, and methods and attributes defined
by its class are found in x.__class__.__bases__[i].__dict__ (for i in
range(len(x.__class__.__bases__))).  You'll have to walk the tree of
base classes to find <I>all</I> class methods and attributes.
<P>
Many, but not all built-in types define a list of their method names
in x.__methods__, and if they have data attributes, their names may be
found in x.__members__.  However this is only a convention.
<P>
For more information, read the source of the standard (but
undocumented) module newdir.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.026.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.026.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.27">4.27. I can't seem to use os.read() on a pipe created with os.popen().</A></H2>
os.read() is a low-level function which takes a file descriptor (a
small integer).  os.popen() creates a high-level file object -- the
same type used for sys.std{in,out,err} and returned by the builtin
open() function.  Thus, to read n bytes from a pipe p created with
os.popen(), you need to use p.read(n).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.027.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.027.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.28">4.28. How can I create a stand-alone binary from a Python script?</A></H2>
Even though there are Python compilers being developed,
you probably don't need a <I>real</I> compiler, if all you want
is a stand-alone program. There are three solutions to that. 
<P>
One is to use the freeze tool, which is included in the Python
source tree as Tools/freeze. It converts Python byte
code to C arrays.  Using a C compiler, you can embed all
your modules into a new program, which is then linked
with the standard Python modules. 
<P>
It works by scanning your source recursively for import statements
(in both forms) and looking for the modules in the standard Python path
as well as in the source directory (for built-in modules).  It then
1 the modules written in Python to C code (array initializers
that can be turned into code objects using the marshal module) and
creates a custom-made config file that only contains those built-in
modules which are actually used in the program.  It then compiles the
generated C code and links it with the rest of the Python interpreter
to form a self-contained binary which acts exactly like your script.
<P>
(Hint: the freeze program only works if your script's filename ends in
".py".)
<P>
There are several utilities which may be helpful.  The first is Gordon McMillan's installer at
<P>
<PRE>
    <A HREF="http://www.mcmillan-inc.com/install1.html">http://www.mcmillan-inc.com/install1.html</A>
</PRE>
which works on Windows, Linux and at least some forms of Unix.
<P>
Another is Thomas Heller's py2exe (Windows only) at
<P>
<PRE>
    <A HREF="http://starship.python.net/crew/theller/py2exe">http://starship.python.net/crew/theller/py2exe</A>/
</PRE>
A third is Christian Tismer's SQFREEZE
(<A HREF="http://starship.python.net/crew/pirx">http://starship.python.net/crew/pirx</A>/) which appends the byte code
to a specially-prepared Python interpreter, which
will find the byte code in executable. 
<P>
A fourth is Fredrik Lundh's Squeeze
(<A HREF="http://www.pythonware.com/products/python/squeeze">http://www.pythonware.com/products/python/squeeze</A>/).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.028.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.028.htp">Log info</A>

/ Last changed on Wed Jun 19 14:01:30 2002 by
<A HREF="mailto:gmcm@hypernet.com">Gordon McMillan</A>
<P>

<HR>
<H2><A NAME="4.29">4.29. What WWW tools are there for Python?</A></H2>
See the chapters titled "Internet Protocols and Support" and
"Internet Data Handling" in the Library Reference
Manual. Python is full of good things which will help you build server-side and client-side web systems.
<P>
A summary of available frameworks is maintained by Paul Boddie at
<P>
<PRE>
    <A HREF="http://thor.prohosting.com/~pboddie/Python/web_modules.html">http://thor.prohosting.com/~pboddie/Python/web_modules.html</A>
</PRE>
Cameron Laird maintains a useful set of pages about Python web technologies at
<P>
<PRE>
   <A HREF="http://starbase.neosoft.com/~claird/comp.lang.python/web_python.html">http://starbase.neosoft.com/~claird/comp.lang.python/web_python.html</A>/
</PRE>
There was a web browser written in Python, called Grail --
see <A HREF="http://sourceforge.net/project/grail">http://sourceforge.net/project/grail</A>/. This project has been terminated; <A HREF="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/grail/grail/README">http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/grail/grail/README</A> gives more details.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.029.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.029.htp">Log info</A>

/ Last changed on Mon Nov 11 22:48:25 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.30">4.30. How do I run a subprocess with pipes connected to both input and output?</A></H2>
Use the standard popen2 module.  For example:
<P>
<PRE>
	import popen2
	fromchild, tochild = popen2.popen2("command")
	tochild.write("input\n")
	tochild.flush()
	output = fromchild.readline()
</PRE>
Warning: in general, it is unwise to
do this, because you can easily cause a deadlock where your
process is blocked waiting for output from the child, while the child
is blocked waiting for input from you.  This can be caused
because the parent expects the child to output more text than it does,
or it can be caused by data being stuck in stdio buffers due to lack
of flushing.  The Python parent can of course explicitly flush the data
it sends to the child before it reads any output, but if the child is
a naive C program it can easily have been written to never explicitly
flush its output, even if it is interactive, since flushing is
normally automatic.
<P>
Note that a deadlock is also possible if you use popen3 to read
stdout and stderr. If one of the two is too large for the internal
buffer (increasing the buffersize does not help) and you read()
the other one first, there is a deadlock, too.
<P>
Note on a bug in popen2: unless your program calls wait()
or waitpid(), finished child processes are never removed,
and eventually calls to popen2 will fail because of a limit on
the number of child processes.  Calling os.waitpid with the
os.WNOHANG option can prevent this; a good place to insert such
a call would be before calling popen2 again.
<P>
Another way to produce a deadlock: Call a wait() and there is
still more output from the program than what fits into the 
internal buffers.
<P>
In many cases, all you really need is to run some data through a
command and get the result back.  Unless the data is infinite in size,
the easiest (and often the most efficient!) way to do this is to write
it to a temporary file and run the command with that temporary file as
input.  The standard module tempfile exports a function mktemp() which
generates unique temporary file names.
<P>
<PRE>
 import tempfile
 import os
 class Popen3:
    """
    This is a deadlock-save version of popen, that returns
    an object with errorlevel, out (a string) and err (a string).
    (capturestderr may not work under windows.)
    Example: print Popen3('grep spam','\n\nhere spam\n\n').out
    """
    def __init__(self,command,input=None,capturestderr=None):
        outfile=tempfile.mktemp()
        command="( %s ) > %s" % (command,outfile)
        if input:
            infile=tempfile.mktemp()
            open(infile,"w").write(input)
            command=command+" &lt;"+infile
        if capturestderr:
            errfile=tempfile.mktemp()
            command=command+" 2>"+errfile
        self.errorlevel=os.system(command) >> 8
        self.out=open(outfile,"r").read()
        os.remove(outfile)
        if input:
            os.remove(infile)
        if capturestderr:
            self.err=open(errfile,"r").read()
            os.remove(errfile)
</PRE>
Note that many interactive programs (e.g. vi) don't work well with
pipes substituted for standard input and output.  You will have to use
pseudo ttys ("ptys") instead of pipes.  There is some undocumented
code to use these in the library module pty.py -- I'm afraid you're on
your own here.
<P>
A different answer is a Python interface to Don Libes' "expect"
library.  A Python extension that interfaces to expect is called "expy"
and available from
<A HREF="http://expectpy.sourceforge.net">http://expectpy.sourceforge.net</A>/.
<P>
A pure Python solution that works like expect is pexpect of Noah Spurrier.
A beta version is available from
<A HREF="http://pexpect.sourceforge.net">http://pexpect.sourceforge.net</A>/
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.030.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.030.htp">Log info</A>

/ Last changed on Tue Sep  3 16:31:31 2002 by
<A HREF="mailto:polzin@gmx.de">Tobias Polzin</A>
<P>

<HR>
<H2><A NAME="4.31">4.31. How do I call a function if I have the arguments in a tuple?</A></H2>
Use the built-in function apply().  For instance,
<P>
<PRE>
    func(1, 2, 3)
</PRE>
is equivalent to
<P>
<PRE>
    args = (1, 2, 3)
    apply(func, args)
</PRE>
Note that func(args) is not the same -- it calls func() with exactly
one argument, the tuple args, instead of three arguments, the integers
1, 2 and 3.
<P>
In Python 2.0, you can also use extended call syntax:
<P>
f(*args) is equivalent to apply(f, args)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.031.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.031.htp">Log info</A>

/ Last changed on Tue Jan  2 03:42:50 2001 by
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
<P>

<HR>
<H2><A NAME="4.32">4.32. How do I enable font-lock-mode for Python in Emacs?</A></H2>
If you are using XEmacs 19.14 or later, any XEmacs 20, FSF Emacs 19.34
or any Emacs 20, font-lock should work automatically for you if you
are using the latest python-mode.el.
<P>
If you are using an older version of XEmacs or Emacs you will need 
to put this in your .emacs file:
<P>
<PRE>
        (defun my-python-mode-hook ()
          (setq font-lock-keywords python-font-lock-keywords)
          (font-lock-mode 1))
        (add-hook 'python-mode-hook 'my-python-mode-hook)
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.032.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.032.htp">Log info</A>

/ Last changed on Mon Apr  6 16:18:46 1998 by
<A HREF="mailto:bwarsaw@python.org">Barry Warsaw</A>
<P>

<HR>
<H2><A NAME="4.33">4.33. Is there a scanf() or sscanf() equivalent?</A></H2>
Not as such.
<P>
For simple input parsing, the easiest approach is usually to split
the line into whitespace-delimited words using string.split(), and to
convert decimal strings to numeric values using int(),
long() or float().  (Python's int() is 32-bit and its
long() is arbitrary precision.)  string.split supports an optional
"sep" parameter which is useful if the line uses something other
than whitespace as a delimiter.
<P>
For more complicated input parsing, regular expressions (see module re)
are better suited and more powerful than C's sscanf().
<P>
There's a contributed module that emulates sscanf(), by Steve Clift;
see contrib/Misc/sscanfmodule.c of the ftp site:
<P>
<PRE>
    <A HREF="http://www.python.org/ftp/python/contrib-09-Dec-1999/Misc">http://www.python.org/ftp/python/contrib-09-Dec-1999/Misc</A>/
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.033.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.033.htp">Log info</A>

/ Last changed on Mon Jun  3 01:07:51 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="4.34">4.34. Can I have Tk events handled while waiting for I/O?</A></H2>
Yes, and you don't even need threads!  But you'll have to
restructure your I/O code a bit.  Tk has the equivalent of Xt's
XtAddInput() call, which allows you to register a callback function
which will be called from the Tk mainloop when I/O is possible on a
file descriptor.  Here's what you need:
<P>
<PRE>
        from Tkinter import tkinter
        tkinter.createfilehandler(file, mask, callback)
</PRE>
The file may be a Python file or socket object (actually, anything
with a fileno() method), or an integer file descriptor.  The mask is
one of the constants tkinter.READABLE or tkinter.WRITABLE.  The
callback is called as follows:
<P>
<PRE>
        callback(file, mask)
</PRE>
You must unregister the callback when you're done, using
<P>
<PRE>
        tkinter.deletefilehandler(file)
</PRE>
Note: since you don't know *how many bytes* are available for reading,
you can't use the Python file object's read or readline methods, since
these will insist on reading a predefined number of bytes.  For
sockets, the recv() or recvfrom() methods will work fine; for other
files, use os.read(file.fileno(), maxbytecount).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.034.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.034.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.35">4.35. How do I write a function with output parameters (call by reference)?</A></H2>
[Mark Lutz] The thing to remember is that arguments are passed by
assignment in Python.  Since assignment just creates references to
objects, there's no alias between an argument name in the caller and
callee, and so no call-by-reference per se.  But you can simulate it
in a number of ways:
<P>
1) By using global variables; but you probably shouldn't :-)
<P>
2) By passing a mutable (changeable in-place) object:
<P>
<PRE>
      def func1(a):
          a[0] = 'new-value'     # 'a' references a mutable list
          a[1] = a[1] + 1        # changes a shared object
</PRE>
<PRE>
      args = ['old-value', 99]
      func1(args)
      print args[0], args[1]     # output: new-value 100
</PRE>
3) By returning a tuple, holding the final values of arguments:
<P>
<PRE>
      def func2(a, b):
          a = 'new-value'        # a and b are local names
          b = b + 1              # assigned to new objects
          return a, b            # return new values
</PRE>
<PRE>
      x, y = 'old-value', 99
      x, y = func2(x, y)
      print x, y                 # output: new-value 100
</PRE>
4) And other ideas that fall-out from Python's object model. For instance, it might be clearer to pass in a mutable dictionary:
<P>
<PRE>
      def func3(args):
          args['a'] = 'new-value'     # args is a mutable dictionary
          args['b'] = args['b'] + 1   # change it in-place
</PRE>
<PRE>
      args = {'a':' old-value', 'b': 99}
      func3(args)
      print args['a'], args['b']
</PRE>
5) Or bundle-up values in a class instance:
<P>
<PRE>
      class callByRef:
          def __init__(self, **args):
              for (key, value) in args.items():
                  setattr(self, key, value)
</PRE>
<PRE>
      def func4(args):
          args.a = 'new-value'        # args is a mutable callByRef
          args.b = args.b + 1         # change object in-place
</PRE>
<PRE>
      args = callByRef(a='old-value', b=99)
      func4(args)
      print args.a, args.b
</PRE>
<PRE>
   But there's probably no good reason to get this complicated :-).
</PRE>
[Python's author favors solution 3 in most cases.]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.035.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.035.htp">Log info</A>

/ Last changed on Sun Jun  8 23:49:46 1997 by
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
<P>

<HR>
<H2><A NAME="4.36">4.36. Please explain the rules for local and global variables in Python.</A></H2>
[Ken Manheimer] In Python, procedure variables are implicitly
global, unless they are assigned anywhere within the block.
In that case
they are implicitly local, and you need to explicitly declare them as
'global'.
<P>
Though a bit surprising at first, a moment's consideration explains
this.  On one hand, requirement of 'global' for assigned vars provides
a bar against unintended side-effects.  On the other hand, if global
were required for all global references, you'd be using global all the
time.  Eg, you'd have to declare as global every reference to a
builtin function, or to a component of an imported module.  This
clutter would defeat the usefulness of the 'global' declaration for
identifying side-effects.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.036.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.036.htp">Log info</A>

/ Last changed on Fri Aug 28 09:53:27 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.37">4.37. How can I have modules that mutually import each other?</A></H2>
Suppose you have the following modules:
<P>
foo.py:
<P>
<PRE>
	from bar import bar_var
	foo_var=1
</PRE>
bar.py:
<P>
<PRE>
	from foo import foo_var
	bar_var=2
</PRE>
The problem is that the above is processed by the interpreter thus:
<P>
<PRE>
	main imports foo
	Empty globals for foo are created
	foo is compiled and starts executing
	foo imports bar
	Empty globals for bar are created
	bar is compiled and starts executing
	bar imports foo (which is a no-op since there already is a module named foo)
	bar.foo_var = foo.foo_var
	...
</PRE>
The last step fails, because Python isn't done with interpreting foo yet and the global symbol dict for foo is still empty.
<P>
The same thing happens when you use "import foo", and then try to access "foo.one" in global code.
<P>
<P>
There are (at least) three possible workarounds for this problem. 
<P>
Guido van Rossum recommends to avoid all uses of "from &lt;module&gt; import ..." (so everything from an imported module is referenced as &lt;module&gt;.&lt;name&gt;) and to place all code inside functions. Initializations of global variables and class variables should use constants or built-in functions only.
<P>
<P>
Jim Roskind suggests the following order in each module:
<P>
<PRE>
 exports (globals, functions, and classes that don't need imported base classes)
 import statements
 active code (including globals that are initialized from imported values).
</PRE>
Python's author doesn't like this approach much because the imports
appear in a strange place, but has to admit that it works.
<P>
<P>
<P>
Matthias Urlichs recommends to restructure your code so that the recursive import is not necessary in the first place.
<P>
<P>
These solutions are not mutually exclusive.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.037.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.037.htp">Log info</A>

/ Last changed on Mon Jun  3 06:52:51 2002 by
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
<P>

<HR>
<H2><A NAME="4.38">4.38. How do I copy an object in Python?</A></H2>
Try copy.copy() or copy.deepcopy() for the general case. Not all objects can be copied, but most can.
<P>
Dictionaries have a copy method. Sequences can be copied by slicing:
<PRE>
 new_l = l[:]
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.038.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.038.htp">Log info</A>

/ Last changed on Thu Mar 21 05:40:26 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="4.39">4.39. How to implement persistent objects in Python? (Persistent == automatically saved to and restored from disk.)</A></H2>
The library module "pickle" now solves this in a very general way
(though you still can't store things like open files, sockets or
windows), and the library module "shelve" uses pickle and (g)dbm to
create persistent mappings containing arbitrary Python objects.
For possibly better performance also look for the latest version
of the relatively recent cPickle module.
<P>
A more awkward way of doing things is to use pickle's little sister,
marshal.  The marshal module provides very fast ways to store
noncircular basic Python types to files and strings, and back again.
Although marshal does not do fancy things like store instances or
handle shared references properly, it does run extremely fast.  For
example loading a half megabyte of data may take less than a
third of a second (on some machines).  This often beats doing
something more complex and general such as using gdbm with
pickle/shelve.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.039.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.039.htp">Log info</A>

/ Last changed on Sun Jun  8 22:59:00 1997 by
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
<P>

<HR>
<H2><A NAME="4.40">4.40. I try to use __spam and I get an error about _SomeClassName__spam.</A></H2>
Variables with double leading underscore are "mangled" to provide a
simple but effective way to define class private variables.  See the
chapter "New in Release 1.4" in the Python Tutorial.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.040.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.040.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.41">4.41. How do I delete a file? And other file questions.</A></H2>
Use os.remove(filename) or os.unlink(filename); for documentation,
see the posix section of the library manual.  They are the same,
unlink() is simply the Unix name for this function.  In earlier
versions of Python, only os.unlink() was available.
<P>
To remove a directory, use os.rmdir(); use os.mkdir() to create one.
<P>
To rename a file, use os.rename().
<P>
To truncate a file, open it using f = open(filename, "r+"), and use
f.truncate(offset); offset defaults to the current seek position.
(The "r+" mode opens the file for reading and writing.)
There's also os.ftruncate(fd, offset) for files opened with os.open()
-- for advanced Unix hacks only.
<P>
The shutil module also contains a number of functions to work on files
including copyfile, copytree, and rmtree amongst others.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.041.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.041.htp">Log info</A>

/ Last changed on Thu Dec 28 12:30:01 2000 by
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
<P>

<HR>
<H2><A NAME="4.42">4.42. How to modify urllib or httplib to support HTTP/1.1?</A></H2>
Recent versions of Python (2.0 and onwards) support HTTP/1.1 natively.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.042.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.042.htp">Log info</A>

/ Last changed on Tue Jan  2 02:56:56 2001 by
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
<P>

<HR>
<H2><A NAME="4.43">4.43. Unexplicable syntax errors in compile() or exec.</A></H2>
When a statement suite (as opposed to an expression) is compiled by
compile(), exec or execfile(), it <I>must</I> end in a newline.  In some
cases, when the source ends in an indented block it appears that at
least two newlines are required.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.043.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.043.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.44">4.44. How do I convert a string to a number?</A></H2>
For integers, use the built-in int() function, e.g. int('144') == 144.  Similarly, long() converts from string to long integer, e.g. long('144') == 144L; and float() to floating-point, e.g. float('144') == 144.0.
<P>
Note that these are restricted to decimal interpretation, so 
that int('0144') == 144 and int('0x144') raises ValueError. For Python
2.0 int takes the base to convert from as a second optional argument, so
int('0x144', 16) == 324.
<P>
For greater flexibility, or before Python 1.5, import the module 
string and use the string.atoi() function for integers, 
string.atol() for long integers, or string.atof() for 
floating-point.  E.g., 
string.atoi('100', 16) == string.atoi('0x100', 0) == 256.  
See the library reference manual section for the string module for 
more details.
<P>
While you could use the built-in function eval() instead of
any of those, this is not recommended, because someone could pass you
a Python expression that might have unwanted side effects (like
reformatting your disk). It also has the effect of interpreting numbers
as Python expressions, so that e.g. eval('09') gives a syntax error
since Python regards numbers starting with '0' as octal (base 8).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.044.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.044.htp">Log info</A>

/ Last changed on Thu Dec 28 12:37:34 2000 by
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
<P>

<HR>
<H2><A NAME="4.45">4.45. How do I convert a number to a string?</A></H2>
To convert, e.g., the number 144 to the string '144', use the
built-in function repr() or the backquote notation (these are
equivalent).  If you want a hexadecimal or octal representation, use
the built-in functions hex() or oct(), respectively.  For fancy
formatting, use the % operator on strings, just like C printf formats,
e.g. "%04d" % 144 yields '0144' and "%.3f" % (1/3.0) yields '0.333'.
See the library reference manual for details.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.045.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.045.htp">Log info</A>
<P>

<HR>
<H2><A NAME="4.46">4.46. How do I copy a file?</A></H2>
There's the shutil module which contains a copyfile()
function that implements a copy loop;
it isn't good enough for the Macintosh, though:
it doesn't copy the resource fork and Finder info.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.046.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.046.htp">Log info</A>

/ Last changed on Tue Jan  2 02:59:40 2001 by
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
<P>

<HR>
<H2><A NAME="4.47">4.47. How do I check if an object is an instance of a given class or of a subclass of it?</A></H2>
If you are developing the classes from scratch it might be better to
program in a more proper object-oriented style -- instead of doing a different
thing based on class membership, why not use a method and define the
method differently in different classes?
<P>
However, there are some legitimate situations
where you need to test for class membership.
<P>
In Python 1.5, you can use the built-in function isinstance(obj, cls).
<P>
The following approaches can be used with earlier Python versions:
<P>
An unobvious method is to raise the object
as an exception and to try to catch the exception with the class you're
testing for:
<P>
<PRE>
	def is_instance_of(the_instance, the_class):
	    try:
		raise the_instance
	    except the_class:
		return 1
	    except:
		return 0
</PRE>
This technique can be used to distinguish "subclassness"
from a collection of classes as well
<P>
<PRE>
                try:
                              raise the_instance
                except Audible:
                              the_instance.play(largo)
                except Visual:
                              the_instance.display(gaudy)
                except Olfactory:
                              sniff(the_instance)
                except:
                              raise ValueError, "dunno what to do with this!"
</PRE>
This uses the fact that exception catching tests for class or subclass
membership.
<P>
A different approach is to test for the presence of a class attribute that
is presumably unique for the given class.  For instance:
<P>
<PRE>
	class MyClass:
	    ThisIsMyClass = 1
	    ...
</PRE>
<PRE>
	def is_a_MyClass(the_instance):
	    return hasattr(the_instance, 'ThisIsMyClass')
</PRE>
This version is easier to inline, and probably faster (inlined it
is definitely faster).  The disadvantage is that someone else could cheat:
<P>
<PRE>
	class IntruderClass:
	    ThisIsMyClass = 1    # Masquerade as MyClass
	    ...
</PRE>
but this may be seen as a feature (anyway, there are plenty of other ways
to cheat in Python).  Another disadvantage is that the class must be
prepared for the membership test.  If you do not "control the
source code" for the class it may not be advisable to modify the
class to support testability.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.047.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.047.htp">Log info</A>

/ Last changed on Fri Jan  2 15:16:04 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.48">4.48. What is delegation?</A></H2>
Delegation refers to an object oriented technique Python programmers
may implement with particular ease.  Consider the following:
<P>
<PRE>
  from string import upper
</PRE>
<PRE>
  class UpperOut:
        def __init__(self, outfile):
              self.__outfile = outfile
        def write(self, str):
              self.__outfile.write( upper(str) )
        def __getattr__(self, name):
              return getattr(self.__outfile, name)
</PRE>
Here the UpperOut class redefines the write method
to convert the argument string to upper case before
calling the underlying self.__outfile.write method, but
all other methods are delegated to the underlying 
self.__outfile object.  The delegation is accomplished
via the "magic" __getattr__ method.  Please see the
language reference for more information on the use
of this method.
<P>
Note that for more general cases delegation can
get trickier. Particularly when attributes must be set
as well as gotten the class must define a __settattr__
method too, and it must do so carefully.
<P>
The basic implementation of __setattr__ is roughly
equivalent to the following:
<P>
<PRE>
   class X:
        ...
        def __setattr__(self, name, value):
             self.__dict__[name] = value
        ...
</PRE>
Most __setattr__ implementations must modify
self.__dict__ to store local state for self without
causing an infinite recursion.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.048.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.048.htp">Log info</A>

/ Last changed on Wed Aug 13 07:11:24 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="4.49">4.49. How do I test a Python program or component.</A></H2>
We presume for the purposes of this question you are interested
in standalone testing, rather than testing your components inside
a testing framework. The best-known testing framework for Python
is the PyUnit module, maintained at
<P>
<PRE>
    <A HREF="http://pyunit.sourceforge.net">http://pyunit.sourceforge.net</A>/
</PRE>
For standalone testing, it helps to write the program so that
it may be easily tested by using good modular design.
In particular your program
should have almost all functionality encapsulated in either functions
or class methods -- and this sometimes has the surprising and
delightful effect of making the program run faster (because
local variable accesses are faster than global accesses).
Furthermore the program should avoid depending on mutating
global variables, since this makes testing much more difficult to do.
<P>
The "global main logic" of your program may be as simple
as
<P>
<PRE>
  if __name__=="__main__":
       main_logic()
</PRE>
at the bottom of the main module of your program.
<P>
Once your program is organized as a tractable collection
of functions and class behaviours you should write test
functions that exercise the behaviours.  A test suite 
can be associated with each module which automates
a sequence of tests.  This sounds like a lot of work, but
since Python is so terse and flexible it's surprisingly easy.
You can make coding much more pleasant and fun by
writing your test functions in parallel with the "production
code", since this makes it easy to find bugs and even
design flaws earlier.
<P>
"Support modules" that are not intended to be the main
module of a program may include a "test script interpretation"
which invokes a self test of the module.
<P>
<PRE>
   if __name__ == "__main__":
      self_test()
</PRE>
Even programs that interact with complex external
interfaces may be tested when the external interfaces are
unavailable by using "fake" interfaces implemented in
Python.  For an example of a "fake" interface, the following
class defines (part of) a "fake" file interface:
<P>
<PRE>
 import string
 testdata = "just a random sequence of characters"
</PRE>
<PRE>
 class FakeInputFile:
   data = testdata
   position = 0
   closed = 0
</PRE>
<PRE>
   def read(self, n=None):
       self.testclosed()
       p = self.position
       if n is None:
          result= self.data[p:]
       else:
          result= self.data[p: p+n]
       self.position = p + len(result)
       return result
</PRE>
<PRE>
   def seek(self, n, m=0):
       self.testclosed()
       last = len(self.data)
       p = self.position
       if m==0: 
          final=n
       elif m==1:
          final=n+p
       elif m==2:
          final=len(self.data)+n
       else:
          raise ValueError, "bad m"
       if final&lt;0:
          raise IOError, "negative seek"
       self.position = final
</PRE>
<PRE>
   def isatty(self):
       return 0
</PRE>
<PRE>
   def tell(self):
       return self.position
</PRE>
<PRE>
   def close(self):
       self.closed = 1
</PRE>
<PRE>
   def testclosed(self):
       if self.closed:
          raise IOError, "file closed"
</PRE>
Try f=FakeInputFile() and test out its operations.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.049.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.049.htp">Log info</A>

/ Last changed on Mon Jun  3 01:12:10 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="4.50">4.50. My multidimensional list (array) is broken! What gives?</A></H2>
You probably tried to make a multidimensional array like this.
<P>
<PRE>
   A = [[None] * 2] * 3
</PRE>
This makes a list containing 3 references to the same list of length
two.  Changes to one row will show in all rows, which is probably not
what you want.  The following works much better:
<P>
<PRE>
   A = [None]*3
   for i in range(3):
        A[i] = [None] * 2
</PRE>
This generates a list containing 3 different lists of length two.
<P>
If you feel weird, you can also do it in the following way:
<P>
<PRE>
   w, h = 2, 3
   A = map(lambda i,w=w: [None] * w, range(h))
</PRE>
For Python 2.0 the above can be spelled using a list comprehension:
<P>
<PRE>
   w,h = 2,3
   A = [ [None]*w for i in range(h) ]
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.050.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.050.htp">Log info</A>

/ Last changed on Thu Dec 28 12:18:35 2000 by
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
<P>

<HR>
<H2><A NAME="4.51">4.51. I want to do a complicated sort: can you do a Schwartzian Transform in Python?</A></H2>
Yes, and in Python you only have to write it once:
<P>
<PRE>
 def st(List, Metric):
     def pairing(element, M = Metric):
           return (M(element), element)
     paired = map(pairing, List)
     paired.sort()
     return map(stripit, paired)
</PRE>
<PRE>
 def stripit(pair):
     return pair[1]
</PRE>
This technique, attributed to Randal Schwartz, sorts the elements
of a list by a metric which maps each element to its "sort value".
For example, if L is a list of string then
<P>
<PRE>
   import string
   Usorted = st(L, string.upper)
</PRE>
<PRE>
   def intfield(s):
         return string.atoi( string.strip(s[10:15] ) )
</PRE>
<PRE>
   Isorted = st(L, intfield)
</PRE>
Usorted gives the elements of L sorted as if they were upper
case, and Isorted gives the elements of L sorted by the integer
values that appear in the string slices starting at position 10
and ending at position 15. In Python 2.0 this can be done more
naturally with list comprehensions:
<P>
<PRE>
  tmp1 = [ (x.upper(), x) for x in L ] # Schwartzian transform
  tmp1.sort()
  Usorted = [ x[1] for x in tmp1 ]
</PRE>
<PRE>
  tmp2 = [ (int(s[10:15]), s) for s in L ] # Schwartzian transform
  tmp2.sort()
  Isorted = [ x[1] for x in tmp2 ]
</PRE>
<P>
Note that Isorted may also be computed by
<P>
<PRE>
   def Icmp(s1, s2):
         return cmp( intfield(s1), intfield(s2) )
</PRE>
<PRE>
   Isorted = L[:]
   Isorted.sort(Icmp)
</PRE>
but since this method computes intfield many times for each
element of L, it is slower than the Schwartzian Transform.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.051.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.051.htp">Log info</A>

/ Last changed on Sat Jun  1 19:18:46 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="4.52">4.52. How to convert between tuples and lists?</A></H2>
The function tuple(seq) converts any sequence into a tuple with
the same items in the same order.
For example, tuple([1, 2, 3]) yields (1, 2, 3) and tuple('abc')
yields ('a', 'b', 'c').  If the argument is
a tuple, it does not make a copy but returns the same object, so
it is cheap to call tuple() when you aren't sure that an object
is already a tuple.
<P>
The function list(seq) converts any sequence into a list with
the same items in the same order.
For example, list((1, 2, 3)) yields [1, 2, 3] and list('abc')
yields ['a', 'b', 'c'].  If the argument is a list,
it makes a copy just like seq[:] would.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.052.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.052.htp">Log info</A>

/ Last changed on Sun Jun 14 14:18:53 1998 by
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
<P>

<HR>
<H2><A NAME="4.53">4.53. Files retrieved with urllib contain leading garbage that looks like email headers.</A></H2>
<I>Extremely</I> old  versions of Python supplied libraries which
did not support HTTP/1.1; the vanilla httplib in Python 1.4
only recognized HTTP/1.0.  In Python 2.0 full HTTP/1.1 support is included.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.053.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.053.htp">Log info</A>

/ Last changed on Mon Jan  8 17:26:18 2001 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="4.54">4.54. How do I get a list of all instances of a given class?</A></H2>
Python does not keep track of all instances of a class (or of a
built-in type).
<P>
You can program the class's constructor to keep track of all
instances, but unless you're very clever, this has the disadvantage
that the instances never get deleted,because your list of all
instances keeps a reference to them.
<P>
(The trick is to regularly inspect the reference counts of the
instances you've retained, and if the reference count is below a
certain level, remove it from the list.  Determining that level is
tricky -- it's definitely larger than 1.)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.054.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.054.htp">Log info</A>

/ Last changed on Tue May 27 23:52:16 1997 by
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
<P>

<HR>
<H2><A NAME="4.55">4.55. A regular expression fails with regex.error: match failure.</A></H2>
This is usually caused by too much backtracking; the regular
expression engine has a fixed size stack which holds at most 4000
backtrack points.  Every character matched by e.g. ".*" accounts for a
backtrack point, so even a simple search like
<P>
<PRE>
  regex.match('.*x',"x"*5000)
</PRE>
will fail.
<P>
This is fixed in the re module introduced with 
Python 1.5; consult the Library Reference section on re for more information.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.055.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.055.htp">Log info</A>

/ Last changed on Thu Jul 30 12:35:49 1998 by
<A HREF="mailto:akuchlin@cnri.reston.va.us">A.M. Kuchling</A>
<P>

<HR>
<H2><A NAME="4.56">4.56. I can't get signal handlers to work.</A></H2>
The most common problem is that the signal handler is declared
with the wrong argument list.  It is called as
<P>
<PRE>
	handler(signum, frame)
</PRE>
so it should be declared with two arguments:
<P>
<PRE>
	def handler(signum, frame):
		...
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.056.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.056.htp">Log info</A>

/ Last changed on Wed May 28 09:29:08 1997 by
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
<P>

<HR>
<H2><A NAME="4.57">4.57. I can't use a global variable in a function? Help!</A></H2>
Did you do something like this?
<P>
<PRE>
   x = 1 # make a global
</PRE>
<PRE>
   def f():
         print x # try to print the global
         ...
         for j in range(100):
              if q>3:
                 x=4
</PRE>
Any variable assigned in a function is local to that function.
unless it is specifically declared global. Since a value is bound
to x as the last statement of the function body, the compiler
assumes that x is local. Consequently the "print x"
attempts to print an uninitialized local variable and will
trigger a NameError.
<P>
In such cases the solution is to insert an explicit global
declaration at the start of the function, making it
<P>
<P>
<PRE>
   def f():
         global x
         print x # try to print the global
         ...
         for j in range(100):
              if q>3:
                 x=4
</PRE>
<P>
In this case, all references to x are interpreted as references
to the x from the module namespace.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.057.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.057.htp">Log info</A>

/ Last changed on Mon Feb 12 15:52:12 2001 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="4.58">4.58. What's a negative index? Why doesn't list.insert() use them?</A></H2>
Python sequences are indexed with positive numbers and
negative numbers.  For positive numbers 0 is the first index
1 is the second index and so forth.  For negative indices -1
is the last index and -2 is the pentultimate (next to last) index
and so forth.  Think of seq[-n] as the same as seq[len(seq)-n].
<P>
Using negative indices can be very convenient.  For example
if the string Line ends in a newline then Line[:-1] is all of Line except
the newline.
<P>
Sadly the list builtin method L.insert does not observe negative
indices.  This feature could be considered a mistake but since
existing programs depend on this feature it may stay around 
forever.  L.insert for negative indices inserts at the start of the
list.  To get "proper" negative index behaviour use L[n:n] = [x]
in place of the insert method.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.058.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.058.htp">Log info</A>

/ Last changed on Wed Aug 13 07:03:18 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="4.59">4.59. How can I sort one list by values from another list?</A></H2>
You can sort lists of  tuples.
<P>
<PRE>
  >>> list1 = ["what", "I'm", "sorting", "by"]
  >>> list2 = ["something", "else", "to", "sort"]
  >>> pairs = map(None, list1, list2)
  >>> pairs
  [('what', 'something'), ("I'm", 'else'), ('sorting', 'to'), ('by', 'sort')]
  >>> pairs.sort()
  >>> pairs
  [("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]
  >>> result = pairs[:]
  >>> for i in xrange(len(result)): result[i] = result[i][1]
  ...
  >>> result
  ['else', 'sort', 'to', 'something']
</PRE>
And if you didn't understand the question, please see the
example above ;c).  Note that "I'm" sorts before "by" because
uppercase "I" comes before lowercase "b" in the ascii order.
Also see 4.51.
<P>
In Python 2.0 this can be done like:
<P>
<PRE>
 >>> list1 = ["what", "I'm", "sorting", "by"]
 >>> list2 = ["something", "else", "to", "sort"]
 >>> pairs = zip(list1, list2)
 >>> pairs
 [('what', 'something'), ("I'm", 'else'), ('sorting', 'to'), ('by', 'sort')]
 >>> pairs.sort()
 >>> result = [ x[1] for x in pairs ]
 >>> result
 ['else', 'sort', 'to', 'something']
</PRE>
[Followup]
<P>
Someone asked, why not this for the last steps:
<P>
<PRE>
  result = []
  for p in pairs: result.append(p[1])
</PRE>
This is much more legible.  However, a quick test shows that
it is almost twice as slow for long lists.  Why?  First of all,
the append() operation has to reallocate memory, and while it
uses some tricks to avoid doing that each time, it still has
to do it occasionally, and apparently that costs quite a bit.
Second, the expression "result.append" requires an extra
attribute lookup.  The attribute lookup could be done away
with by rewriting as follows:
<P>
<PRE>
  result = []
  append = result.append
  for p in pairs: append(p[1])
</PRE>
which gains back some speed, but is still considerably slower
than the original solution, and hardly less convoluted.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.059.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.059.htp">Log info</A>

/ Last changed on Thu Dec 28 12:56:35 2000 by
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
<P>

<HR>
<H2><A NAME="4.60">4.60. Why doesn't dir() work on builtin types like files and lists?</A></H2>
It does starting with Python 1.5.
<P>
Using 1.4, you can find out which methods a given object supports
by looking at its __methods__ attribute:
<P>
<PRE>
    >>> List = []
    >>> List.__methods__
    ['append', 'count', 'index', 'insert', 'remove', 'reverse', 'sort']
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.060.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.060.htp">Log info</A>

/ Last changed on Thu Sep 16 14:56:42 1999 by
<A HREF="mailto:skip@mojam.com">Skip Montanaro</A>
<P>

<HR>
<H2><A NAME="4.61">4.61. How can I mimic CGI form submission (METHOD=POST)?</A></H2>
I would like to retrieve web pages that are the result of POSTing a
form. Is there existing code that would let me do this easily?
<P>
Yes. Here's a simple example that uses httplib.  
<P>
<PRE>
    #!/usr/local/bin/python
</PRE>
<PRE>
    import httplib, sys, time
</PRE>
<PRE>
    ### build the query string
    qs = "First=Josephine&amp;MI=Q&amp;Last=Public"
</PRE>
<PRE>
    ### connect and send the server a path
    httpobj = httplib.HTTP('www.some-server.out-there', 80)
    httpobj.putrequest('POST', '/cgi-bin/some-cgi-script')
    ### now generate the rest of the HTTP headers...
    httpobj.putheader('Accept', '*/*')
    httpobj.putheader('Connection', 'Keep-Alive')
    httpobj.putheader('Content-type', 'application/x-www-form-urlencoded')
    httpobj.putheader('Content-length', '%d' % len(qs))
    httpobj.endheaders()
    httpobj.send(qs)
    ### find out what the server said in response...
    reply, msg, hdrs = httpobj.getreply()
    if reply != 200:
	sys.stdout.write(httpobj.getfile().read())
</PRE>
Note that in general for "url encoded posts" (the default) query strings must be "quoted" to, for example, change equals signs and spaces to an encoded form when they occur in name or value.  Use urllib.quote to perform this quoting.  For example to send name="Guy Steele, Jr.":
<P>
<PRE>
   >>> from urllib import quote
   >>> x = quote("Guy Steele, Jr.")
   >>> x
   'Guy%20Steele,%20Jr.'
   >>> query_string = "name="+x
   >>> query_string
   'name=Guy%20Steele,%20Jr.'
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.061.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.061.htp">Log info</A>

/ Last changed on Mon Jun 21 03:47:07 1999 by
<A HREF="mailto:tbryan@python.net">TAB</A>
<P>

<HR>
<H2><A NAME="4.62">4.62. If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?</A></H2>
Databases opened for write access with the bsddb module (and often by
the anydbm module, since it will preferentially use bsddb) must
explicitly be closed using the close method of the database.  The
underlying libdb package caches database contents which need to be
converted to on-disk form and written, unlike regular open files which
already have the on-disk bits in the kernel's write buffer, where they
can just be dumped by the kernel with the program exits.
<P>
If you have initialized a new bsddb database but not written anything to
it before the program crashes, you will often wind up with a zero-length
file and encounter an exception the next time the file is opened.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.062.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.062.htp">Log info</A>

/ Last changed on Mon Jun  3 01:15:01 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="4.63">4.63. How do I make a Python script executable on Unix?</A></H2>
You need to do two things: the script file's mode must be executable
(include the 'x' bit), and the first line must begin with #!
followed by the pathname for the Python interpreter.
<P>
The first is done by executing 'chmod +x scriptfile' or perhaps
'chmod 755 scriptfile'.
<P>
The second can be done in a number of way.  The most straightforward
way is to write
<P>
<PRE>
  #!/usr/local/bin/python
</PRE>
as the very first line of your file - or whatever the pathname is
where the python interpreter is installed on your platform.
<P>
If you would like the script to be independent of where the python
interpreter lives, you can use the "env" program.  On almost all
platforms, the following will work, assuming the python interpreter
is in a directory on the user's $PATH:
<P>
<PRE>
  #! /usr/bin/env python
</PRE>
Note -- *don't* do this for CGI scripts.  The $PATH variable for
CGI scripts is often very minimal, so you need to use the actual
absolute pathname of the interpreter.
<P>
Occasionally, a user's environment is so full that the /usr/bin/env
program fails; or there's no env program at all.
In that case, you can try the following hack (due to Alex Rezinsky):
<P>
<PRE>
  #! /bin/sh
  """:"
  exec python $0 ${1+"$@"}
  """
</PRE>
The disadvantage is that this defines the script's __doc__ string.
However, you can fix that by adding
<P>
<PRE>
  __doc__ = """...Whatever..."""
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.063.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.063.htp">Log info</A>

/ Last changed on Mon Jan 15 09:19:16 2001 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="4.64">4.64. How do you remove duplicates from a list?</A></H2>
See the Python Cookbook for a long discussion of many cool ways:
<P>
<PRE>
    <A HREF="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560</A>
</PRE>
Generally, if you don't mind reordering the List
<P>
<PRE>
   if List:
      List.sort()
      last = List[-1]
      for i in range(len(List)-2, -1, -1):
          if last==List[i]: del List[i]
          else: last=List[i]
</PRE>
If all elements of the list may be used as
dictionary keys (ie, they are all hashable)
this is often faster
<P>
<PRE>
   d = {}
   for x in List: d[x]=x
   List = d.values()
</PRE>
Also, for extremely large lists you might
consider more optimal alternatives to the first one.
The second one is pretty good whenever it can
be used.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.064.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.064.htp">Log info</A>

/ Last changed on Fri May 24 21:56:33 2002 by
<A HREF="mailto:tim.one@comcast.net">Tim Peters</A>
<P>

<HR>
<H2><A NAME="4.65">4.65. Are there any known year 2000 problems in Python?</A></H2>
I am not aware of year 2000 deficiencies in Python 1.5.  Python does
very few date calculations and for what it does, it relies on the C
library functions.  Python generally represent times either as seconds
since 1970 or as a tuple (year, month, day, ...) where the year is
expressed with four digits, which makes Y2K bugs unlikely.  So as long
as your C library is okay, Python should be okay.  Of course, I cannot
vouch for <I>your</I> Python code!
<P>
Given the nature of freely available software, I have to add that this statement is not
legally binding.  The Python copyright notice contains the following
disclaimer:
<P>
<PRE>
  STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  PERFORMANCE OF THIS SOFTWARE.
</PRE>
The good news is that <I>if</I> you encounter a problem, you have full
source available to track it down and fix it!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.065.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.065.htp">Log info</A>

/ Last changed on Fri Apr 10 14:59:31 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.66">4.66. I want a version of map that applies a method to a sequence of objects! Help!</A></H2>
Get fancy!
<P>
<PRE>
  def method_map(objects, method, arguments):
       """method_map([a,b], "flog", (1,2)) gives [a.flog(1,2), b.flog(1,2)]"""
       nobjects = len(objects)
       methods = map(getattr, objects, [method]*nobjects)
       return map(apply, methods, [arguments]*nobjects)
</PRE>
It's generally a good idea to get to know the mysteries of map and apply
and getattr and the other dynamic features of Python.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.066.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.066.htp">Log info</A>

/ Last changed on Mon Jan  5 14:21:14 1998 by
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
<P>

<HR>
<H2><A NAME="4.67">4.67. How do I generate random numbers in Python?</A></H2>
The standard library module "random" implements a random number
generator.  Usage is simple:
<P>
<PRE>
    import random
</PRE>
<PRE>
    random.random()
</PRE>
This returns a random floating point number in the range [0, 1).
<P>
There are also many other specialized generators in this module, such
as
<P>
<PRE>
    randrange(a, b) chooses an integer in the range [a, b)
    uniform(a, b) chooses a floating point number in the range [a, b)
    normalvariate(mean, sdev) sample from normal (Gaussian) distribution
</PRE>
Some higher-level functions operate on sequences directly, such as
<P>
<PRE>
    choice(S) chooses random element from a given sequence
    shuffle(L) shuffles a list in-place, i.e. permutes it randomly
</PRE>
There's also a class, Random, which you can instantiate
to create independent multiple random number generators.
<P>
All this is documented in the library reference manual.  Note that
the module "whrandom" is obsolete.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.067.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.067.htp">Log info</A>

/ Last changed on Mon Jun  3 01:16:51 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="4.68">4.68. How do I access the serial (RS232) port?</A></H2>
There's a Windows serial communication module (for communication
over RS 232 serial ports) at
<P>
<PRE>
  <A HREF="ftp://ftp.python.org/pub/python/contrib/sio-151.zip">ftp://ftp.python.org/pub/python/contrib/sio-151.zip</A>
  <A HREF="http://www.python.org/ftp/python/contrib/sio-151.zip">http://www.python.org/ftp/python/contrib/sio-151.zip</A>
</PRE>
For DOS, try Hans Nowak's Python-DX, which supports this, at:
<P>
<PRE>
  <A HREF="http://www.cuci.nl/~hnowak">http://www.cuci.nl/~hnowak</A>/
</PRE>
For Unix, see a usenet post by Mitch Chapman:
<P>
<PRE>
  <A HREF="http://groups.google.com/groups?selm=34A04430.CF9@ohioee.com">http://groups.google.com/groups?selm=34A04430.CF9@ohioee.com</A>
</PRE>
For Win32, POSIX(Linux, BSD, *), Jython, Chris':
<P>
<PRE>
  <A HREF="http://pyserial.sourceforge.net">http://pyserial.sourceforge.net</A>
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.068.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.068.htp">Log info</A>

/ Last changed on Tue Jul  2 21:11:07 2002 by
<A HREF="mailto:cliechti@gmx.net">Chris Liechti</A>
<P>

<HR>
<H2><A NAME="4.69">4.69. Images on Tk-Buttons don't work in Py15?</A></H2>
They <I>do</I> work, but you must keep your own <I>reference</I> to the image 
object now. More verbosely, you must make sure that, say, a global 
variable or a class attribute refers to the object.
<P>
Quoting Fredrik Lundh from the mailinglist:
<P>
<PRE>
  Well, the Tk button widget keeps a reference to the internal
  photoimage object, but Tkinter does not.  So when the last
  Python reference goes away, Tkinter tells Tk to release the
  photoimage.  But since the image is in use by a widget, Tk
  doesn't destroy it.  Not completely.  It just blanks the image,
  making it completely transparent...
</PRE>
<PRE>
  And yes, there was a bug in the keyword argument handling
  in 1.4 that kept an extra reference around in some cases.  And
  when Guido fixed that bug in 1.5, he broke quite a few Tkinter
  programs...
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.069.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.069.htp">Log info</A>

/ Last changed on Tue Feb  3 11:31:03 1998 by
<A HREF="mailto:cjr@euronet.nl">Case Roole</A>
<P>

<HR>
<H2><A NAME="4.70">4.70. Where is the math.py (socket.py, regex.py, etc.) source file?</A></H2>
If you can't find a source file for a module it may be a builtin
or dynamically loaded module implemented in C, C++ or other
compiled language.  In this case you may not have the source
file or it may be something like mathmodule.c, somewhere in
a C source directory (not on the Python Path).
<P>
Fredrik Lundh (<A HREF="mailto:fredrik@pythonware.com">fredrik@pythonware.com</A>) explains (on the python-list):
<P>
There are (at least) three kinds of modules in Python:
1) modules written in Python (.py);
2) modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);
3) modules written in C and linked with the interpreter; to get a list
of these, type:
<P>
<PRE>
    import sys
    print sys.builtin_module_names
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.070.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.070.htp">Log info</A>

/ Last changed on Tue Feb  3 13:55:33 1998 by
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
<P>

<HR>
<H2><A NAME="4.71">4.71. How do I send mail from a Python script?</A></H2>
The standard library module smtplib does this.
Here's a very simple interactive mail
sender that uses it.  This method will work on any host that
supports an SMTP listener.
<P>
<PRE>
    import sys, smtplib
</PRE>
<PRE>
    fromaddr = raw_input("From: ")
    toaddrs  = raw_input("To: ").split(',')
    print "Enter message, end with ^D:"
    msg = ''
    while 1:
        line = sys.stdin.readline()
        if not line:
            break
        msg = msg + line
</PRE>
<PRE>
    # The actual mail send
    server = smtplib.SMTP('localhost')
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()
</PRE>
If the local host doesn't have an SMTP listener, you need to find one. The simple method is to ask the user. Alternately, you can use the DNS system to find the mail gateway(s) responsible for the source address.
<P>
A Unix-only alternative uses sendmail.  The location of the
sendmail program varies between systems; sometimes it is
/usr/lib/sendmail, sometime /usr/sbin/sendmail.  The sendmail manual
page will help you out.  Here's some sample code:
<P>
<PRE>
  SENDMAIL = "/usr/sbin/sendmail" # sendmail location
  import os
  p = os.popen("%s -t -i" % SENDMAIL, "w")
  p.write("To: <A HREF="mailto:cary@ratatosk.org">cary@ratatosk.org</A>\n")
  p.write("Subject: test\n")
  p.write("\n") # blank line separating headers from body
  p.write("Some text\n")
  p.write("some more text\n")
  sts = p.close()
  if sts != 0:
      print "Sendmail exit status", sts
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.071.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.071.htp">Log info</A>

/ Last changed on Mon Jun  3 07:05:12 2002 by
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
<P>

<HR>
<H2><A NAME="4.72">4.72. How do I avoid blocking in connect() of a socket?</A></H2>
The select module is widely known to help with asynchronous
I/O on sockets once they are connected.  However, it is less
than common knowledge how to avoid blocking on the initial
connect() call.  Jeremy Hylton has the following advice (slightly
edited):
<P>
To prevent the TCP connect from blocking, you can set the socket to 
non-blocking mode.  Then when you do the connect(), you will either 
connect immediately (unlikely) or get an exception that contains the 
errno.  errno.EINPROGRESS indicates that the connection is in 
progress, but hasn't finished yet.  Different OSes will return 
different errnos, so you're going to have to check.  I can tell you 
that different versions of Solaris return different errno values. 
<P>
In Python 1.5 and later, you can use connect_ex() to avoid
creating an exception.  It will just return the errno value. 
<P>
To poll, you can call connect_ex() again later -- 0 or errno.EISCONN 
indicate that you're connected -- or you can pass this socket to 
select (checking to see if it is writeable).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.072.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.072.htp">Log info</A>

/ Last changed on Tue Feb 24 21:30:45 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.73">4.73. How do I specify hexadecimal and octal integers?</A></H2>
To specify an octal digit, precede the octal value with a zero.  For example,
to set the variable "a" to the octal value "10" (8 in decimal), type:
<P>
<PRE>
    >>> a = 010
</PRE>
To verify that this works, you can type "a" and hit enter while in the
interpreter, which will cause Python to spit out the current value of "a"
in decimal:
<P>
<PRE>
    >>> a
    8
</PRE>
Hexadecimal is just as easy.  Simply precede the hexadecimal number with a
zero, and then a lower or uppercase "x".  Hexadecimal digits can be specified
in lower or uppercase.  For example, in the Python interpreter:
<P>
<PRE>
    >>> a = 0xa5
    >>> a
    165
    >>> b = 0XB2
    >>> b
    178
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.073.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.073.htp">Log info</A>

/ Last changed on Tue Mar  3 12:53:16 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.74">4.74. How to get a single keypress at a time?</A></H2>
For Windows, see question 8.2.  Here is an answer for Unix (see also 4.94).
<P>
There are several solutions; some involve using curses, which is a
pretty big thing to learn.  Here's a solution without curses, due
to Andrew Kuchling (adapted from code to do a PGP-style
randomness pool):
<P>
<PRE>
        import termios, sys, os
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] &amp; ~termios.ICANON &amp; ~termios.ECHO
        new[6][termios.VMIN] = 1
        new[6][termios.VTIME] = 0
        termios.tcsetattr(fd, termios.TCSANOW, new)
        s = ''    # We'll save the characters typed and add them to the pool.
        try:
            while 1:
                c = os.read(fd, 1)
                print "Got character", `c`
                s = s+c
        finally:
            termios.tcsetattr(fd, termios.TCSAFLUSH, old)
</PRE>
You need the termios module for any of this to work, and I've only
tried it on Linux, though it should work elsewhere.  It turns off
stdin's echoing and disables canonical mode, and then reads a
character at a time from stdin, noting the time after each keystroke.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.074.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.074.htp">Log info</A>

/ Last changed on Thu Oct 24 00:36:56 2002 by
<A HREF="mailto:cliechti@gmx.net">chris</A>
<P>

<HR>
<H2><A NAME="4.75">4.75. How can I overload constructors (or methods) in Python?</A></H2>
(This actually applies to all methods, but somehow the question
usually comes up first in the context of constructors.)
<P>
Where in C++ you'd write
<P>
<PRE>
    class C {
        C() { cout &lt;&lt; "No arguments\n"; }
        C(int i) { cout &lt;&lt; "Argument is " &lt;&lt; i &lt;&lt; "\n"; }
    }
</PRE>
in Python you have to write a single constructor that catches all
cases using default arguments.  For example:
<P>
<PRE>
    class C:
        def __init__(self, i=None):
            if i is None:
                print "No arguments"
            else:
                print "Argument is", i
</PRE>
This is not entirely equivalent, but close enough in practice.
<P>
You could also try a variable-length argument list, e.g.
<P>
<PRE>
        def __init__(self, *args):
            ....
</PRE>
The same approach works for all method definitions.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.075.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.075.htp">Log info</A>

/ Last changed on Mon Apr 20 11:55:55 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.76">4.76. How do I pass keyword arguments from one method to another?</A></H2>
Use apply.  For example:
<P>
<PRE>
    class Account:
        def __init__(self, **kw):
            self.accountType = kw.get('accountType')
            self.balance = kw.get('balance')
</PRE>
<PRE>
    class CheckingAccount(Account):
        def __init__(self, **kw):
            kw['accountType'] = 'checking'
            apply(Account.__init__, (self,), kw)
</PRE>
<PRE>
    myAccount = CheckingAccount(balance=100.00)
</PRE>
In Python 2.0 you can call it directly using the new ** syntax:
<P>
<PRE>
    class CheckingAccount(Account):
        def __init__(self, **kw):
            kw['accountType'] = 'checking'
            Account.__init__(self, **kw)
</PRE>
or more generally:
<P>
<PRE>
 >>> def f(x, *y, **z):
 ...  print x,y,z
 ...
 >>> Y = [1,2,3]
 >>> Z = {'foo':3,'bar':None}
 >>> f('hello', *Y, **Z)
 hello (1, 2, 3) {'foo': 3, 'bar': None}
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.076.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.076.htp">Log info</A>

/ Last changed on Thu Dec 28 13:04:01 2000 by
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
<P>

<HR>
<H2><A NAME="4.77">4.77. What module should I use to help with generating HTML?</A></H2>
Check out HTMLgen written by Robin Friedrich. It's a class library
of objects corresponding to all the HTML 3.2 markup tags. It's used
when you are writing in Python and wish to synthesize HTML pages for
generating a web or for CGI forms, etc. 
<P>
It can be found in the FTP contrib area on python.org or on the 
Starship. Use the search engines there to locate the latest version.
<P>
It might also be useful to consider DocumentTemplate, which offers clear 
separation between Python code and HTML code.  DocumentTemplate is part 
of the Bobo objects publishing system (http:/www.digicool.com/releases) 
but can be used independantly of course!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.077.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.077.htp">Log info</A>

/ Last changed on Fri Aug 28 09:54:58 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.78">4.78. How do I create documentation from doc strings?</A></H2>
Use gendoc, by Daniel Larson.  See
<P>
<A HREF="http://starship.python.net/crew/danilo">http://starship.python.net/crew/danilo</A>/
<P>
It can create HTML from the doc strings in your Python source code.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.078.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.078.htp">Log info</A>

/ Last changed on Mon Oct  7 17:15:51 2002 by
<A HREF="mailto:phil@dspfactory.com">Phil Rittenhouse</A>
<P>

<HR>
<H2><A NAME="4.79">4.79. How do I read (or write) binary data?</A></H2>
For complex data formats, it's best to use
use the struct module.  It's documented in the library reference.
It allows you to take a string read from a file containing binary
data (usually numbers) and convert it to Python objects; and vice
versa.
<P>
For example, the following code reads two 2-byte integers
and one 4-byte integer in big-endian format from a file:
<P>
<PRE>
  import struct
</PRE>
<PRE>
  f = open(filename, "rb")  # Open in binary mode for portability
  s = f.read(8)
  x, y, z = struct.unpack(">hhl", s)
</PRE>
The '>' in the format string forces bin-endian data; the letter
'h' reads one "short integer" (2 bytes), and 'l' reads one
"long integer" (4 bytes) from the string.
<P>
For data that is more regular (e.g. a homogeneous list of ints or
floats), you can also use the array module, also documented
in the library reference.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.079.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.079.htp">Log info</A>

/ Last changed on Wed Oct  7 09:16:45 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.80">4.80. I can't get key bindings to work in Tkinter</A></H2>
An oft-heard complaint is that event handlers bound to events
with the bind() method don't get handled even when the appropriate
key is pressed.
<P>
The most common cause is that the widget to which the binding applies
doesn't have "keyboard focus".  Check out the Tk documentation
for the focus command.  Usually a widget is given the keyboard
focus by clicking in it (but not for labels; see the taketocus
option).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.080.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.080.htp">Log info</A>

/ Last changed on Fri Jun 12 09:37:33 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.81">4.81. &quot;import crypt&quot; fails</A></H2>
[Unix]
<P>
Starting with Python 1.5, the crypt module is disabled by default.
In order to enable it, you must go into the Python source tree and
edit the file Modules/Setup to enable it (remove a '#' sign in
front of the line starting with '#crypt').  Then rebuild.
You may also have to add the string '-lcrypt' to that same line.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.081.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.081.htp">Log info</A>

/ Last changed on Wed Aug  5 08:57:09 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.82">4.82. Are there coding standards or a style guide for Python programs?</A></H2>
Yes, Guido has written the "Python Style Guide".  See
<A HREF="http://www.python.org/doc/essays/styleguide.html">http://www.python.org/doc/essays/styleguide.html</A>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.082.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.082.htp">Log info</A>

/ Last changed on Tue Sep 29 09:50:27 1998 by
<A HREF="mailto:vanandel@ucar.edu">Joseph VanAndel</A>
<P>

<HR>
<H2><A NAME="4.83">4.83. How do I freeze Tkinter applications?</A></H2>
Freeze is a tool to create stand-alone applications (see 4.28).
<P>
When freezing Tkinter applications, the applications will not be
truly stand-alone, as the application will still need the tcl and
tk libraries.
<P>
One solution is to ship the application with the tcl and tk libraries,
and point to them at run-time using the TCL_LIBRARY and TK_LIBRARY
environment variables.
<P>
To get truly stand-alone applications, the Tcl scripts that form
the library have to be integrated into the application as well. One
tool supporting that is SAM (stand-alone modules), which is part
of the Tix distribution (<A HREF="http://tix.mne.com">http://tix.mne.com</A>). Build Tix with SAM 
enabled, perform the appropriate call to Tclsam_init etc inside 
Python's Modules/tkappinit.c, and link with libtclsam
and libtksam (you might include the Tix libraries as well).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.083.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.083.htp">Log info</A>

/ Last changed on Wed Jan 20 17:35:01 1999 by
<A HREF="mailto:loewis@informatik.hu-berlin.de">Martin v. Lwis</A>
<P>

<HR>
<H2><A NAME="4.84">4.84. How do I create static class data and static class methods?</A></H2>
[Tim Peters, <A HREF="mailto:tim_one@email.msn.com">tim_one@email.msn.com</A>]
<P>
Static data (in the sense of C++ or Java) is easy; static methods (again in the sense of C++ or Java) are not supported directly.
<P>
STATIC DATA
<P>
For example,
<P>
<PRE>
    class C:
        count = 0   # number of times C.__init__ called
</PRE>
<PRE>
        def __init__(self):
            C.count = C.count + 1
</PRE>
<PRE>
        def getcount(self):
            return C.count  # or return self.count
</PRE>
c.count also refers to C.count for any c such that isinstance(c, C) holds, unless overridden by c itself or by some class on the base-class search path from c.__class__ back to C.
<P>
Caution:  within a method of C,
<P>
<PRE>
    self.count = 42
</PRE>
creates a new and unrelated instance vrbl named "count" in self's own dict.  So rebinding of a class-static data name needs the
<P>
<PRE>
    C.count = 314
</PRE>
form whether inside a method or not.
<P>
<P>
STATIC METHODS
<P>
Static methods (as opposed to static data) are unnatural in Python, because
<P>
<PRE>
    C.getcount
</PRE>
returns an unbound method object, which can't be invoked without supplying an instance of C as the first argument.
<P>
The intended way to get the effect of a static method is via a module-level function:
<P>
<PRE>
    def getcount():
        return C.count
</PRE>
If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation.
<P>
Several tortured schemes for faking static methods can be found by searching DejaNews.  Most people feel such cures are worse than the disease.  Perhaps the least obnoxious is due to Pekka Pessi (mailto:<A HREF="mailto:ppessi@hut.fi">ppessi@hut.fi</A>):
<P>
<PRE>
    # helper class to disguise function objects
    class _static:
        def __init__(self, f):
            self.__call__ = f
</PRE>
<PRE>
    class C:
        count = 0
</PRE>
<PRE>
        def __init__(self):
            C.count = C.count + 1
</PRE>
<PRE>
        def getcount():
            return C.count
        getcount = _static(getcount)
</PRE>
<PRE>
        def sum(x, y):
            return x + y
        sum = _static(sum)
</PRE>
<PRE>
    C(); C()
    c = C()
    print C.getcount()  # prints 3
    print c.getcount()  # prints 3
    print C.sum(27, 15) # prints 42
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.084.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.084.htp">Log info</A>

/ Last changed on Thu Jan 21 21:35:38 1999 by
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
<P>

<HR>
<H2><A NAME="4.85">4.85. __import__('x.y.z') returns &lt;module 'x'&gt;; how do I get z?</A></H2>
Try
<P>
<PRE>
   __import__('x.y.z').y.z
</PRE>
For more realistic situations, you may have to do something like
<P>
<PRE>
   m = __import__(s)
   for i in string.split(s, ".")[1:]:
       m = getattr(m, i)
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.085.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.085.htp">Log info</A>

/ Last changed on Thu Jan 28 11:01:43 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.86">4.86. Basic thread wisdom</A></H2>
Please note that there is no way to take advantage of
multiprocessor hardware using the Python thread model. The interpreter
uses a global interpreter lock (GIL),
which does not allow multiple threads to be concurrently active.
<P>
If you write a simple test program like this:
<P>
<PRE>
  import thread
  def run(name, n):
      for i in range(n): print name, i
  for i in range(10):
      thread.start_new(run, (i, 100))
</PRE>
none of the threads seem to run!  The reason is that as soon as
the main thread exits, all threads are killed.
<P>
A simple fix is to add a sleep to the end of the program,
sufficiently long for all threads to finish:
<P>
<PRE>
  import thread, time
  def run(name, n):
      for i in range(n): print name, i
  for i in range(10):
      thread.start_new(run, (i, 100))
  time.sleep(10) # &lt;----------------------------!
</PRE>
But now (on many platforms) the threads don't run in parallel,
but appear to run sequentially, one at a time!  The reason is
that the OS thread scheduler doesn't start a new thread until
the previous thread is blocked.
<P>
A simple fix is to add a tiny sleep to the start of the run
function:
<P>
<PRE>
  import thread, time
  def run(name, n):
      time.sleep(0.001) # &lt;---------------------!
      for i in range(n): print name, i
  for i in range(10):
      thread.start_new(run, (i, 100))
  time.sleep(10)
</PRE>
Some more hints:
<P>
Instead of using a time.sleep() call at the end, it's
better to use some kind of semaphore mechanism.  One idea is to
use a the Queue module to create a queue object, let each thread
append a token to the queue when it finishes, and let the main
thread read as many tokens from the queue as there are threads.
<P>
Use the threading module instead of the thread module.  It's part
of Python since version 1.5.1.  It takes care of all these details,
and has many other nice features too!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.086.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.086.htp">Log info</A>

/ Last changed on Fri Feb  7 16:21:55 2003 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.87">4.87. Why doesn't closing sys.stdout (stdin, stderr) really close it?</A></H2>
Python file objects are a high-level layer of abstraction on top of C streams, which in turn are a medium-level layer of abstraction on top of (among other things) low-level C file descriptors.
<P>
For most file objects f you create in Python via the builtin "open" function, f.close() marks the Python file object as being closed from Python's point of view, and also arranges to close the underlying C stream.  This happens automatically too, in f's destructor, when f becomes garbage.
<P>
But stdin, stdout and stderr are treated specially by Python, because of the special status also given to them by C:  doing
<P>
<PRE>
    sys.stdout.close() # ditto for stdin and stderr
</PRE>
marks the Python-level file object as being closed, but does <I>not</I> close the associated C stream (provided sys.stdout is still bound to its default value, which is the stream C also calls "stdout").
<P>
To close the underlying C stream for one of these three, you should first be sure that's what you really want to do (e.g., you may confuse the heck out of extension modules trying to do I/O).  If it is, use os.close:
<P>
<PRE>
    os.close(0)   # close C's stdin stream
    os.close(1)   # close C's stdout stream
    os.close(2)   # close C's stderr stream
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.087.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.087.htp">Log info</A>

/ Last changed on Sat Apr 17 02:22:35 1999 by
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
<P>

<HR>
<H2><A NAME="4.88">4.88. What kinds of global value mutation are thread-safe?</A></H2>
[adapted from c.l.py responses by Gordon McMillan &amp; GvR]
<P>
A global interpreter lock (GIL) is used internally to ensure that only one thread runs in the Python VM at a time.  In general, Python offers to switch among threads only between bytecode instructions (how frequently it offers to switch can be set via sys.setcheckinterval).  Each bytecode instruction-- and all the C implementation code reached from it --is therefore atomic.
<P>
In theory, this means an exact accounting requires an exact understanding of the PVM bytecode implementation.  In practice, it means that operations on shared vrbls of builtin data types (ints, lists, dicts, etc) that "look atomic" really are.
<P>
For example, these are atomic (L, L1, L2 are lists, D, D1, D2 are dicts, x, y
are objects, i, j are ints):
<P>
<PRE>
    L.append(x)
    L1.extend(L2)
    x = L[i]
    x = L.pop()
    L1[i:j] = L2
    L.sort()
    x = y
    x.field = y
    D[x] = y
    D1.update(D2)
    D.keys()
</PRE>
These aren't:
<P>
<PRE>
    i = i+1
    L.append(L[-1])
    L[i] = L[j]
    D[x] = D[x] + 1
</PRE>
Note: operations that replace other objects may invoke those other objects' __del__ method when their reference count reaches zero, and that can affect things.  This is especially true for the mass updates to dictionaries and lists.  When in doubt, use a mutex!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.088.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.088.htp">Log info</A>

/ Last changed on Fri Feb  7 16:21:03 2003 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.89">4.89. How do I modify a string in place?</A></H2>
Strings are immutable (see question 6.2) so you cannot modify a string
directly.  If you need an object with this ability, try converting the
string to a list or take a look at the array module.
<P>
<PRE>
    >>> s = "Hello, world"
    >>> a = list(s)
    >>> print a
    ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
    >>> a[7:] = list("there!")
    >>> import string
    >>> print string.join(a, '')
    'Hello, there!'
</PRE>
<PRE>
    >>> import array
    >>> a = array.array('c', s)
    >>> print a
    array('c', 'Hello, world')
    >>> a[0] = 'y' ; print a
    array('c', 'yello world')
    >>> a.tostring()
    'yello, world'
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.089.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.089.htp">Log info</A>

/ Last changed on Tue May 18 01:22:47 1999 by
<A HREF="mailto:dalke@bioreason.com">Andrew Dalke</A>
<P>

<HR>
<H2><A NAME="4.90">4.90. How to pass on keyword/optional parameters/arguments</A></H2>
Q: How can I pass on optional or keyword parameters from one function to another?
<P>
<PRE>
	def f1(a, *b, **c):
		...
</PRE>
A: In Python 2.0 and above:
<P>
<PRE>
	def f2(x, *y, **z):
		...
		z['width']='14.3c'
		...
		f1(x, *y, **z)
</PRE>
<PRE>
   Note: y can be any sequence (e.g., list or tuple) and z must be a dict.
</PRE>
<P>
A: For versions prior to 2.0, use 'apply', like:
<P>
<PRE>
	def f2(x, *y, **z):
		...
		z['width']='14.3c'
		...
		apply(f1, (x,)+y, z)
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.090.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.090.htp">Log info</A>

/ Last changed on Mon Jun  3 07:20:56 2002 by
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
<P>

<HR>
<H2><A NAME="4.91">4.91. How can I get a dictionary to display its keys in a consistent order?</A></H2>
In general, dictionaries store their keys in an unpredictable order,
so the display order of a dictionary's elements will be similarly
unpredictable.
(See 
<a href="http://www.python.org/doc/FAQ.html#6.12">Question 6.12</a>
to understand why this is so.)
<P>
This can be frustrating if you want to save a printable version to a 
file, make some changes and then compare it with some other printed
dictionary.  If you have such needs you can subclass UserDict.UserDict
to create a SortedDict class that prints itself in a predictable order.
Here's one simpleminded implementation of such a class:
<P>
<PRE>
  import UserDict, string
</PRE>
<PRE>
  class SortedDict(UserDict.UserDict):
    def __repr__(self):
      result = []
      append = result.append
      keys = self.data.keys()
      keys.sort()
      for k in keys:
        append("%s: %s" % (`k`, `self.data[k]`))
      return "{%s}" % string.join(result, ", ")
</PRE>
<PRE>
    ___str__ = __repr__
</PRE>
<P>
This will work for many common situations you might encounter, though
it's far from a perfect solution. (It won't have any effect on the
pprint module and does not transparently handle values that are or
contain dictionaries.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.091.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.091.htp">Log info</A>

/ Last changed on Thu Sep 16 17:31:06 1999 by
<A HREF="mailto:skip@mojam.com">Skip Montanaro</A>
<P>

<HR>
<H2><A NAME="4.92">4.92. Is there a Python tutorial?</A></H2>
Yes.  See question 1.20 at 
<A HREF="http://www.python.org/doc/FAQ.html#1.20">http://www.python.org/doc/FAQ.html#1.20</A>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.092.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.092.htp">Log info</A>

/ Last changed on Sat Dec  4 16:04:00 1999 by
<A HREF="mailto:tbryan@python.net">TAB</A>
<P>

<HR>
<H2><A NAME="4.93">4.93. Deleted</A></H2>
See 4.28
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.093.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.093.htp">Log info</A>

/ Last changed on Tue May 28 20:40:37 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="4.94">4.94. How do I get a single keypress without blocking?</A></H2>
There are several solutions; some involve using curses, which is a
pretty big thing to learn.  Here's a solution without curses. (see also 4.74, for Windows, see question 8.2)
<P>
<PRE>
  import termios, fcntl, sys, os
  fd = sys.stdin.fileno()
</PRE>
<PRE>
  oldterm = termios.tcgetattr(fd)
  newattr = termios.tcgetattr(fd)
  newattr[3] = newattr[3] &amp; ~termios.ICANON &amp; ~termios.ECHO
  termios.tcsetattr(fd, termios.TCSANOW, newattr)
</PRE>
<PRE>
  oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
  fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
</PRE>
<PRE>
  try:
      while 1:
          try:
              c = sys.stdin.read(1)
              print "Got character", `c`
          except IOError: pass
  finally:
      termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
      fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
</PRE>
<P>
You need the termios and the fcntl module for any of this to work, 
and I've only tried it on Linux, though it should work elsewhere. 
<P>
In this code, characters are read and printed one at a time.
<P>
termios.tcsetattr() turns off stdin's echoing and disables canonical
mode.  fcntl.fnctl() is used to obtain stdin's file descriptor flags
and modify them for non-blocking mode.  Since reading stdin when it is
empty results in an IOError, this error is caught and ignored.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.094.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.094.htp">Log info</A>

/ Last changed on Thu Oct 24 00:39:06 2002 by
<A HREF="mailto:cliechti@gmx.net">chris</A>
<P>

<HR>
<H2><A NAME="4.95">4.95. Is there an equivalent to Perl chomp()? (Remove trailing newline from string)</A></H2>
There are two partial substitutes. If you want to remove all trailing
whitespace, use the method string.rstrip(). Otherwise, if there is only
one line in the string, use string.splitlines()[0].
<P>
<PRE>
 -----------------------------------------------------------------------
</PRE>
<PRE>
 rstrip() is too greedy, it strips all trailing white spaces.
 splitlines() takes ControlM as line boundary.
 Consider these strings as input:
   "python python    \r\n"
   "python\rpython\r\n"
   "python python   \r\r\r\n"
 The results from rstrip()/splitlines() are perhaps not what we want.
</PRE>
<PRE>
 It seems re can perform this task.
</PRE>
<P>
<PRE>
 #!/usr/bin/python 
 # requires python2                                                             
</PRE>
<PRE>
 import re, os, StringIO
</PRE>
<PRE>
 lines=StringIO.StringIO(
   "The Python Programming Language\r\n"
   "The Python Programming Language \r \r \r\r\n"
   "The\rProgramming\rLanguage\r\n"
   "The\rProgramming\rLanguage\r\r\r\r\n"
   "The\r\rProgramming\r\rLanguage\r\r\r\r\n"
 )
</PRE>
<PRE>
 ln=re.compile("(?:[\r]?\n|\r)$") # dos:\r\n, unix:\n, mac:\r, others: unknown
 # os.linesep does not work if someone ftps(in binary mode) a dos/mac text file
 # to your unix box
 #ln=re.compile(os.linesep + "$")
</PRE>
<PRE>
 while 1:
   s=lines.readline()
   if not s: break
   print "1.(%s)" % `s.rstrip()`
   print "2.(%s)" % `ln.sub( "", s, 1)`
   print "3.(%s)" % `s.splitlines()[0]`
   print "4.(%s)" % `s.splitlines()`
   print
</PRE>
<PRE>
 lines.close()
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.095.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.095.htp">Log info</A>

/ Last changed on Wed Aug  8 09:51:34 2001 by
<A HREF="mailto:serpent@ms5.hinet.net">Crystal</A>
<P>

<HR>
<H2><A NAME="4.96">4.96. Why is join() a string method when I'm really joining the elements of a (list, tuple, sequence)?</A></H2>
Strings became much more like other standard types starting in release 1.6, when methods were added which give the same functionality that has always been available using the functions of the string module.  These new methods have been widely accepted, but the one which appears to make (some) programmers feel uncomfortable is:
<P>
<PRE>
    ", ".join(['1', '2', '4', '8', '16'])
</PRE>
which gives the result
<P>
<PRE>
    "1, 2, 4, 8, 16"
</PRE>
There are two usual arguments against this usage.
<P>
The first runs along the lines of: "It looks really ugly using a method of a string literal (string constant)", to which the answer is that it might, but a string literal is just a fixed value. If the methods are to be allowed on names bound to strings there is no logical reason to make them unavailable on literals. Get over it!
<P>
The second objection is typically cast as: "I am really telling a sequence to join its members together with a string constant". Sadly, you aren't. For some reason there seems to be much less difficulty with having split() as a string method, since in that case it is easy to see that
<P>
<PRE>
    "1, 2, 4, 8, 16".split(", ")
</PRE>
is an instruction to a string literal to return the substrings delimited by the given separator (or, by default, arbitrary runs of white space). In this case a Unicode string returns a list of Unicode strings, an ASCII string returns a list of ASCII strings, and everyone is happy.
<P>
join() is a string method because in using it you are telling the separator string to iterate over an arbitrary sequence, forming string representations of each of the elements, and inserting itself between the elements' representations.  This method can be used with any argument which obeys the rules for sequence objects, inluding any new classes you might define yourself.
<P>
Because this is a string method it can work for Unicode strings as well as plain ASCII strings. If join() were a method of the sequence types then the sequence types would have to decide which type of string to return depending on the type of the separator.
<P>
If none of these arguments persuade you, then for the moment you can continue to use the join() function from the string module, which allows you to write
<P>
<PRE>
    string.join(['1', '2', '4', '8', '16'], ", ")
</PRE>
You will just have to try and forget that the string module actually uses the syntax you are compaining about to implement the syntax you prefer!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.096.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.096.htp">Log info</A>

/ Last changed on Fri Aug  2 15:51:58 2002 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="4.97">4.97. How can my code discover the name of an object?</A></H2>
Generally speaking, it can't, because objects don't really have names. The assignment statement does not store the assigned value in the name but a reference to it. Essentially, assignment creates a binding of a name to a value. The same is true of <I>def</I> and <I>class</I> statements, but in that case the value is a callable. Consider the following code:
<P>
<PRE>
    class A:
        pass
</PRE>
<PRE>
    B = A
</PRE>
<PRE>
    a = B()
    b = a
    print b
    &lt;__main__.A instance at 016D07CC&gt;
    print a
    &lt;__main__.A instance at 016D07CC&gt;
</PRE>
<P>
Arguably the class has a name: even though it is bound to two names and invoked through the name B the created instance is still reported as an instance of class A. However, it is impossible to say whether the instance's name is a or b, since both names are bound to the same value.
<P>
Generally speaking it should not be necessary for your code to "know the names" of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.097.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.097.htp">Log info</A>

/ Last changed on Thu Mar  8 03:53:39 2001 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="4.98">4.98. Why are floating point calculations so inaccurate?</A></H2>
The development version of the Python Tutorial now contains an Appendix with more info:
<PRE>
    <A HREF="http://www.python.org/doc/current/tut/node14.html">http://www.python.org/doc/current/tut/node14.html</A>
</PRE>
People are often very surprised by results like this:
<P>
<PRE>
 >>> 1.2-1.0
 0.199999999999999996
</PRE>
And think it is a bug in Python. It's not. It's a problem caused by
the internal representation of a floating point number. A floating point
number is stored as a fixed number of binary digits.
<P>
In decimal math, there are many numbers that can't be represented
with a fixed number of decimal digits, i.e.
1/3 = 0.3333333333.......
<P>
In the binary case, 1/2 = 0.1, 1/4 = 0.01, 1/8 = 0.001, etc. There are 
a lot of numbers that can't be represented. The digits are cut off at
some point.
<P>
Since Python 1.6, a floating point's repr() function prints as many
digits are necessary to make eval(repr(f)) == f true for any float f.
The str() function prints the more sensible number that was probably
intended:
<P>
<PRE>
 >>> 0.2
 0.20000000000000001
 >>> print 0.2
 0.2
</PRE>
Again, this has nothing to do with Python, but with the way the
underlying C platform handles floating points, and ultimately with
the inaccuracy you'll always have when writing down numbers of fixed
number of digit strings.
<P>
One of the consequences of this is that it is dangerous to compare 
the result of some computation to a float with == ! 
Tiny inaccuracies may mean that == fails.
<P>
Instead try something like this:
<P>
<PRE>
 epsilon = 0.0000000000001 # Tiny allowed error
 expected_result = 0.4
</PRE>
<PRE>
 if expected_result-epsilon &lt;= computation() &lt;= expected_result+epsilon:
    ...
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.098.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.098.htp">Log info</A>

/ Last changed on Mon Apr  1 22:18:47 2002 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="4.99">4.99. I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, 'Invalid argument'). Help! How can I restore my data?</A></H2>
Don't panic! Your data are probably intact. The most frequent cause
for the error is that you tried to open an earlier Berkeley DB file
with a later version of the Berkeley DB library.
<P>
Many Linux systems now have all three versions of Berkeley DB
available.  If you are migrating from version 1 to a newer version use
db_dump185 to dump a plain text version of the database.
If you are migrating from version 2 to version 3 use db2_dump to create
a plain text version of the database.  In either case, use db_load to
create a new native database for the latest version installed on your
computer.  If you have version 3 of Berkeley DB installed, you should
be able to use db2_load to create a native version 2 database.
<P>
You should probably move away from Berkeley DB version 1 files because
the hash file code contains known bugs that can corrupt your data.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.099.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.099.htp">Log info</A>

/ Last changed on Wed Aug 29 16:04:29 2001 by
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
<P>

<HR>
<H2><A NAME="4.100">4.100. What are the &quot;best practices&quot; for using import in a module?</A></H2>
First, the standard modules are great.  Use them!  The standard Python library is large and varied.  Using modules can save you time and effort and will reduce maintainenance cost of your code.  (Other programs are dedicated to supporting and fixing bugs in the standard Python modules.  Coworkers may also be familiar with themodules that you use, reducing the amount of time it takes them to understand your code.)
<P>
The rest of this answer is largely a matter of personal preference, but here's what some newsgroup posters said (thanks to all who responded)
<P>
In general, don't use 
<PRE>
 from modulename import *
</PRE>
Doing so clutters the importer's namespace.  Some avoid this idiom even with the few modules that were designed to be imported in this manner.  (Modules designed in this manner include Tkinter, thread, and wxPython.)
<P>
Import modules at the top of a file, one module per line.  Doing so makes it clear what other modules your code requires and avoids questions of whether the module name is in scope.  Using one import per line makes it easy to add and delete module imports.
<P>
Move imports into a local scope (such as at the top of a function definition) if there are a lot of imports, and you're trying to avoid the cost (lots of initialization time) of many imports.  This technique is especially helpful if many of the imports are unnecessary depending on how the program executes.  You may also want to move imports into a function if the modules are only ever used in that function.  Note that loading a module the first time may be expensive (because of the one time initialization of the module) but that loading a module multiple times is virtually free (a couple of dictionary lookups).  Even if the module name has gone out of scope, the module is probably available in sys.modules.  Thus, there isn't really anything wrong with putting no imports at the module level (if they aren't needed) and putting all of the imports at the function level.  
<P>
It is sometimes necessary to move imports to a function or class to avoid problems with circular imports.  Gordon says:
<PRE>
 Circular imports are fine where both modules use the "import &lt;module&gt;"
 form of import. They fail when the 2nd module wants to grab a name
 out of the first ("from module import name") and the import is at
 the top level. That's because names in the 1st are not yet available,
 (the first module is busy importing the 2nd).  
</PRE>
In this case, if the 2nd module is only used in one function, then the  import can easily be moved into that function.  By the time the import is called, the first module will have finished initializing, and the second module can do its import.
<P>
It may also be necessary to move imports out of the top level of code 
if some of the modules are platform-specific.  In that case, it may not even be possible to import all of the modules at the top of the file.  In this case, importing the correct modules in the corresponding platform-specific code is a good option.   
<P>
If only instances of a specific class uses a module, then it is reasonable to import the module in the class's __init__ method and then assign the module to an instance variable so that the module is always available (via that instance variable) during the life of the object.  Note that to delay an import until the class is instantiated, the import must be inside a method.  Putting the import inside the class but outside of any method still causes the import to occur when the module is initialized.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.100.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.100.htp">Log info</A>

/ Last changed on Sat Aug  4 04:44:47 2001 by
<A HREF="mailto:tbryan@python.net">TAB</A>
<P>

<HR>
<H2><A NAME="4.101">4.101. Is there a tool to help find bugs or perform static analysis?</A></H2>
Yes.  PyChecker is a static analysis tool for finding bugs 
in Python source code as well as warning about code complexity 
and style.
<P>
You can get PyChecker from:  <A HREF="http://pychecker.sf.net">http://pychecker.sf.net</A>.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.101.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.101.htp">Log info</A>

/ Last changed on Fri Aug 10 15:42:11 2001 by
<A HREF="mailto:neal@metaslash.com">Neal</A>
<P>

<HR>
<H2><A NAME="4.102">4.102. UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)</A></H2>
This error indicates that your Python installation can handle
only 7-bit ASCII strings.  There are a couple ways to fix or
workaround the problem.
<P>
If your programs must handle data in arbitary character set encodings, the environment the application runs in will generally identify the encoding of the data it is handing you.  You need to convert the input to Unicode data using that encoding.  For instance, a program that handles email or web input will typically find character set encoding information in Content-Type headers.  This can then be used to properly convert input data to Unicode. Assuming the string referred to by "value" is encoded as UTF-8:
<P>
<PRE>
    value = unicode(value, "utf-8")
</PRE>
will return a Unicode object.  If the data is not correctly encoded as UTF-8, the above call will raise a UnicodeError.
<P>
If you only want strings coverted to Unicode which have non-ASCII data, you can try converting them first assuming an ASCII encoding, and then generate Unicode objects if that fails:
<P>
<PRE>
    try:
        x = unicode(value, "ascii")
    except UnicodeError:
        value = unicode(value, "utf-8")
    else:
        # value was valid ASCII data
        pass
</PRE>
<P>
If you normally use a character set encoding other than US-ASCII and only need to handle data in that encoding, the simplest way to fix the problem may be simply to set the encoding in sitecustomize.py. The following code is just a modified version of the encoding setup code from site.py with the relevant lines uncommented.
<P>
<PRE>
    # Set the string encoding used by the Unicode implementation.
    # The default is 'ascii'
    encoding = "ascii" # &lt;= CHANGE THIS if you wish
</PRE>
<PRE>
    # Enable to support locale aware default string encodings.
    import locale
    loc = locale.getdefaultlocale()
    if loc[1]:
        encoding = loc[1]
    if encoding != "ascii":
        import sys
        sys.setdefaultencoding(encoding)
</PRE>
<P>
Also note that on Windows, there is an encoding known as "mbcs", which uses an encoding specific to your current locale.  In many cases, and particularly when working with COM, this may be an appropriate default encoding to use.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.102.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.102.htp">Log info</A>

/ Last changed on Sat Apr 13 04:45:41 2002 by
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
<P>

<HR>
<H2><A NAME="4.103">4.103. Using strings to call functions/methods</A></H2>
There are various techniques:
<P>
* Use a dictionary pre-loaded with strings and functions.  The primary
advantage of this technique is that the strings do not need to match the
names of the functions.  This is also the primary technique used to
emulate a case construct:
<P>
<PRE>
    def a():
        pass
</PRE>
<PRE>
    def b():
        pass
</PRE>
<PRE>
    dispatch = {'go': a, 'stop': b}  # Note lack of parens for funcs
</PRE>
<PRE>
    dispatch[get_input()]()  # Note trailing parens to call function
</PRE>
* Use the built-in function getattr():
<P>
<PRE>
    import foo
    getattr(foo, 'bar')()
</PRE>
Note that getattr() works on any object, including classes, class
instances, modules, and so on.
<P>
This is used in several places in the standard library, like
this:
<P>
<PRE>
    class Foo:
        def do_foo(self):
            ...
</PRE>
<PRE>
        def do_bar(self):
            ...
</PRE>
<PRE>
     f = getattr(foo_instance, 'do_' + opname)
     f()
</PRE>
<P>
* Use locals() or eval() to resolve the function name:
<P>
def myFunc():
<PRE>
    print "hello"
</PRE>
fname = "myFunc"
<P>
f = locals()[fname]
f()
<P>
f = eval(fname)
f()
<P>
Note: Using eval() can be dangerous. If you don't have absolute control
over the contents of the string, all sorts of things could happen...
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.103.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.103.htp">Log info</A>

/ Last changed on Thu Mar 21 08:14:58 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="4.104">4.104. How fast are exceptions?</A></H2>
A try/except block is extremely efficient.  Actually executing an
exception is expensive.  In older versions of Python (prior to 2.0), it
was common to code this idiom:
<P>
<PRE>
    try:
        value = dict[key]
    except KeyError:
        dict[key] = getvalue(key)
        value = dict[key]
</PRE>
This idiom only made sense when you expected the dict to have the key
95% of the time or more; other times, you coded it like this:
<P>
<PRE>
    if dict.has_key(key):
        value = dict[key]
    else:
        dict[key] = getvalue(key)
        value = dict[key]
</PRE>
In Python 2.0 and higher, of course, you can code this as
<P>
<PRE>
    value = dict.setdefault(key, getvalue(key))
</PRE>
However this evaluates getvalue(key) always, regardless of whether it's needed or not.  So if it's slow or has a side effect you should use one of the above variants.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.104.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.104.htp">Log info</A>

/ Last changed on Mon Dec  9 10:12:30 2002 by
<A HREF="mailto:yeti@physics.muni.cz">Yeti</A>
<P>

<HR>
<H2><A NAME="4.105">4.105. Sharing global variables across modules</A></H2>
The canonical way to share information across modules within a single
program is to create a special module (often called config or cfg).
Just import the config module in all modules of your application; the
module then becomes available as a global name.  Because there is only
one instance of each module, any changes made to the module object get
reflected everywhere.  For example:
<P>
config.py:
<P>
<PRE>
    pass
</PRE>
mod.py:
<P>
<PRE>
    import config
    config.x = 1
</PRE>
main.py:
<P>
<PRE>
    import config
    import mod
    print config.x
</PRE>
Note that using a module is also the basis for implementing the
Singleton design pattern, for the same reason.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.105.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.105.htp">Log info</A>

/ Last changed on Tue Apr 23 23:07:19 2002 by
<A HREF="mailto:aahz@pythoncraft.com">Aahz</A>
<P>

<HR>
<H2><A NAME="4.106">4.106. Why is cPickle so slow?</A></H2>
Use the binary option.  We'd like to make that the default, but it would
break backward compatibility:
<P>
<PRE>
    largeString = 'z' * (100 * 1024)
    myPickle = cPickle.dumps(largeString, 1)
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.106.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.106.htp">Log info</A>

/ Last changed on Thu Aug 22 19:54:25 2002 by
<A HREF="mailto:aahz@pythoncraft.com">Aahz</A>
<P>

<HR>
<H2><A NAME="4.107">4.107. When importing module XXX, why do I get &quot;undefined symbol: PyUnicodeUCS2_...&quot; ?</A></H2>
You are using a version of Python that uses a 4-byte representation for
Unicode characters, but the extension module you are importing (possibly
indirectly) was compiled using a Python that uses a 2-byte representation
for Unicode characters (the default).
<P>
If instead the name of the undefined symbol starts with PyUnicodeUCS4_,
the problem is the same by the relationship is reversed:  Python was
built using 2-byte Unicode characters, and the extension module was
compiled using a Python with 4-byte Unicode characters.
<P>
This can easily occur when using pre-built extension packages.  RedHat
Linux 7.x, in particular, provides a "python2" binary that is compiled
with 4-byte Unicode.  This only causes the link failure if the extension
uses any of the PyUnicode_*() functions.  It is also a problem if if an
extension uses any of the Unicode-related format specifiers for
Py_BuildValue (or similar) or parameter-specifications for
PyArg_ParseTuple().
<P>
You can check the size of the Unicode character a Python interpreter is
using by checking the value of sys.maxunicode:
<P>
<PRE>
  >>> import sys
  >>> if sys.maxunicode > 65535:
  ...     print 'UCS4 build'
  ... else:
  ...     print 'UCS2 build'
</PRE>
The only way to solve this problem is to use extension modules compiled
with a Python binary built using the same size for Unicode characters.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.107.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.107.htp">Log info</A>

/ Last changed on Tue Aug 27 15:00:17 2002 by
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
<P>

<HR>
<H2><A NAME="4.108">4.108. How do I create a .pyc file?</A></H2>
QUESTION:
<P>
I have a module and I wish to generate a .pyc file.  
How do I do it? Everything I read says that generation of a .pyc file is 
"automatic", but I'm not getting anywhere.  
<P>
<P>
ANSWER: 
<P>
When a module is imported for the first time (or when the source is more
recent than the current compiled file) a .pyc file containing the compiled code should be created in the
same directory as the .py file.
<P>
One reason that a .pyc file may not be created is permissions problems with the directory. This can happen, for example, if you develop as one user but run as another, such as if you are testing with a web server.
<P>
However, in most cases, that's not the problem.
<P>
Creation of a .pyc file is "automatic" if you are importing a module and Python has the
ability (permissions, free space, etc...) to write the compiled module
back to the directory. But note that running Python on a top level script is not considered an
import and so no .pyc will be created automatically.  For example, if you have a top-level module abc.py that imports another module xyz.py, when you run abc, xyz.pyc will be created since xyz is imported, but no abc.pyc file will be created since abc isn't imported.
<P>
If you need to create abc.pyc -- that is, to create a .pyc file for a
module that is not imported -- you can.  (Look up
the py_compile and compileall modules in the Library Reference.) 
<P>
You can manually compile any module using the "py_compile" module.  One
way is to use the compile() function in that module interactively:
<P>
<PRE>
    >>> import py_compile
    >>> py_compile.compile('abc.py')
</PRE>
This will write the .pyc to the same location as abc.py (or you
can override that with the optional parameter cfile).
<P>
You can also automatically compile all files in a directory or
directories using the "compileall" module, which can also be run
straight from the command line.
<P>
You can do it from the shell (or DOS) prompt by entering:
<PRE>
       python compile.py abc.py
</PRE>
or 
<PRE>
       python compile.py *
</PRE>
Or you can write a script to do it on a list of filenames that you enter.
<P>
<PRE>
     import sys
     from py_compile import compile
</PRE>
<PRE>
     if len(sys.argv) &lt;= 1:
        sys.exit(1)
</PRE>
<PRE>
     for file in sys.argv[1:]:
        compile(file)
</PRE>
ACKNOWLEDGMENTS:
<P>
Steve Holden, David Bolen, Rich Somerfield,  Oleg Broytmann, Steve Ferg
<P>

<A HREF="faqw.py?req=edit&amp;file=faq04.108.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq04.108.htp">Log info</A>

/ Last changed on Wed Feb 12 15:58:25 2003 by
<A HREF="mailto:steve@ferg.org">Stephen Ferg</A>
<P>

<HR>
<H1>5. Extending Python</H1>

<HR>
<H2><A NAME="5.1">5.1. Can I create my own functions in C?</A></H2>
Yes, you can create built-in modules containing functions,
variables, exceptions and even new types in C.  This is explained in
the document "Extending and Embedding the Python Interpreter" (<A HREF="http://www.python.org/doc/current/ext/ext.html">http://www.python.org/doc/current/ext/ext.html</A>).  Also read the chapter
on dynamic loading.
<P>
There's more information on this in each of the Python books:
Programming Python, Internet Programming with Python, and Das Python-Buch
(in German).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.001.htp">Log info</A>

/ Last changed on Mon Dec 10 05:18:57 2001 by
<A HREF="mailto:fdrake@acm.org">Fred L. Drake, Jr.</A>
<P>

<HR>
<H2><A NAME="5.2">5.2. Can I create my own functions in C++?</A></H2>
Yes, using the C-compatibility features found in C++.  Basically
you place extern "C" { ... } around the Python include files and put
extern "C" before each function that is going to be called by the
Python interpreter.  Global or static C++ objects with constructors
are probably not a good idea.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.002.htp">Log info</A>
<P>

<HR>
<H2><A NAME="5.3">5.3. How can I execute arbitrary Python statements from C?</A></H2>
The highest-level function to do this is PyRun_SimpleString() which takes
a single string argument which is executed in the context of module
__main__ and returns 0 for success and -1 when an exception occurred
(including SyntaxError).  If you want more control, use PyRun_String();
see the source for PyRun_SimpleString() in Python/pythonrun.c.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.003.htp">Log info</A>

/ Last changed on Fri May 23 20:08:14 1997 by
<A HREF="mailto:billtut@microsoft.com">Bill Tutt</A>
<P>

<HR>
<H2><A NAME="5.4">5.4. How can I evaluate an arbitrary Python expression from C?</A></H2>
Call the function PyRun_String() from the previous question with the
start symbol eval_input (Py_eval_input starting with 1.5a1); it 
parses an expression, evaluates it and returns its value.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.004.htp">Log info</A>

/ Last changed on Wed May 21 22:23:18 1997 by
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
<P>

<HR>
<H2><A NAME="5.5">5.5. How do I extract C values from a Python object?</A></H2>
That depends on the object's type.  If it's a tuple,
PyTupleSize(o) returns its length and PyTuple_GetItem(o, i)
returns its i'th item; similar for lists with PyListSize(o)
and PyList_GetItem(o, i).  For strings, PyString_Size(o) returns
its length and PyString_AsString(o) a pointer to its value
(note that Python strings may contain null bytes so strlen()
is not safe).  To test which type an object is, first make sure
it isn't NULL, and then use PyString_Check(o), PyTuple_Check(o),
PyList_Check(o), etc.
<P>
There is also a high-level API to Python objects which is
provided by the so-called 'abstract' interface -- read
Include/abstract.h for further details.  It allows for example 
interfacing with any kind of Python sequence (e.g. lists and tuples)
using calls like PySequence_Length(), PySequence_GetItem(), etc.)
as well as many other useful protocols.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.005.htp">Log info</A>

/ Last changed on Wed May 21 22:34:20 1997 by
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
<P>

<HR>
<H2><A NAME="5.6">5.6. How do I use Py_BuildValue() to create a tuple of arbitrary length?</A></H2>
You can't.  Use t = PyTuple_New(n) instead, and fill it with
objects using PyTuple_SetItem(t, i, o) -- note that this "eats" a
reference count of o.  Similar for lists with PyList_New(n) and
PyList_SetItem(l, i, o).  Note that you <I>must</I> set all the tuple items to
some value before you pass the tuple to Python code --
PyTuple_New(n) initializes them to NULL, which isn't a valid Python
value.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.006.htp">Log info</A>

/ Last changed on Thu Jul 31 18:15:29 1997 by
<A HREF="mailto:guido@python.org">Guido van Rossum</A>
<P>

<HR>
<H2><A NAME="5.7">5.7. How do I call an object's method from C?</A></H2>
The PyObject_CallMethod() function can be used to call an arbitrary
method of an object.  The parameters are the object, the name of the
method to call, a format string like that used with Py_BuildValue(), and the argument values:
<P>
<PRE>
    PyObject *
    PyObject_CallMethod(PyObject *object, char *method_name,
                        char *arg_format, ...);
</PRE>
This works for any object that has methods -- whether built-in or
user-defined.  You are responsible for eventually DECREF'ing the
return value.
<P>
To call, e.g., a file object's "seek" method with arguments 10, 0
(assuming the file object pointer is "f"):
<P>
<PRE>
        res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0);
        if (res == NULL) {
                ... an exception occurred ...
        }
        else {
                Py_DECREF(res);
        }
</PRE>
Note that since PyObject_CallObject() <I>always</I> wants a tuple for the
argument list, to call a function without arguments, pass "()" for the
format, and to call a function with one argument, surround the argument
in parentheses, e.g. "(i)".
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.007.htp">Log info</A>

/ Last changed on Thu Jun  6 16:15:46 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="5.8">5.8. How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?</A></H2>
(Due to Mark Hammond):
<P>
In Python code, define an object that supports the "write()" method.
Redirect sys.stdout and sys.stderr to this object.
Call print_error, or just allow the standard traceback mechanism to
work. Then, the output will go wherever your write() method sends it.
<P>
The easiest way to do this is to use the StringIO class in the standard
library.
<P>
Sample code and use for catching stdout:
<PRE>
	>>> class StdoutCatcher:
	...  def __init__(self):
	...   self.data = ''
	...  def write(self, stuff):
	...   self.data = self.data + stuff
	...  
	>>> import sys
	>>> sys.stdout = StdoutCatcher()
	>>> print 'foo'
	>>> print 'hello world!'
	>>> sys.stderr.write(sys.stdout.data)
	foo
	hello world!
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.008.htp">Log info</A>

/ Last changed on Wed Dec 16 18:34:25 1998 by
<A HREF="mailto:richard@bofh.asn.au">Richard Jones</A>
<P>

<HR>
<H2><A NAME="5.9">5.9. How do I access a module written in Python from C?</A></H2>
You can get a pointer to the module object as follows:
<P>
<PRE>
        module = PyImport_ImportModule("&lt;modulename&gt;");
</PRE>
If the module hasn't been imported yet (i.e. it is not yet present in
sys.modules), this initializes the module; otherwise it simply returns
the value of sys.modules["&lt;modulename&gt;"].  Note that it doesn't enter
the module into any namespace -- it only ensures it has been
initialized and is stored in sys.modules.
<P>
You can then access the module's attributes (i.e. any name defined in
the module) as follows:
<P>
<PRE>
        attr = PyObject_GetAttrString(module, "&lt;attrname&gt;");
</PRE>
Calling PyObject_SetAttrString(), to assign to variables in the module, also works.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.009.htp">Log info</A>

/ Last changed on Wed May 21 22:56:40 1997 by
<A HREF="mailto:david_ascher@brown.edu">david ascher</A>
<P>

<HR>
<H2><A NAME="5.10">5.10. How do I interface to C++ objects from Python?</A></H2>
Depending on your requirements, there are many approaches.  To do
this manually, begin by reading the "Extending and Embedding" document
(Doc/ext.tex, see also <A HREF="http://www.python.org/doc">http://www.python.org/doc</A>/).  Realize
that for the Python run-time system, there isn't a whole lot of
difference between C and C++ -- so the strategy to build a new Python
type around a C structure (pointer) type will also work for C++
objects.
<P>
A useful automated approach (which also works for C) is SWIG:
<A HREF="http://www.swig.org">http://www.swig.org</A>/.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.010.htp">Log info</A>

/ Last changed on Fri Oct 15 05:14:01 1999 by
<A HREF="mailto:sjoerd.mullender@oratrix.nl">Sjoerd Mullender</A>
<P>

<HR>
<H2><A NAME="5.11">5.11. mSQLmodule (or other old module) won't build with Python 1.5 (or later)</A></H2>
Since python-1.4 "Python.h" will have the file includes needed in an 
extension module.
Backward compatibility is dropped after version 1.4 and therefore 
mSQLmodule.c will not build as "allobjects.h" cannot be found. 
The following change in mSQLmodule.c is harmless when building it with
1.4 and necessary when doing so for later python versions:
<P>
Remove lines:
<P>
<PRE>
	#include "allobjects.h"
	#include "modsupport.h"
</PRE>
And insert instead:
<P>
<PRE>
	#include "Python.h"
</PRE>
You may also need to add
<P>
<PRE>
                #include "rename2.h"
</PRE>
if the module uses "old names".
<P>
This may happen with other ancient python modules as well,
and the same fix applies.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.011.htp">Log info</A>

/ Last changed on Sun Dec 21 02:03:35 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="5.12">5.12. I added a module using the Setup file and the make fails! Huh?</A></H2>
Setup must end in a newline, if there is no newline there it gets
very sad.  Aside from this possibility, maybe you have other
non-Python-specific linkage problems.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.012.htp">Log info</A>

/ Last changed on Tue Jun 24 15:54:01 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="5.13">5.13. I want to compile a Python module on my Red Hat Linux system, but some files are missing.</A></H2>
Red Hat's RPM for Python doesn't include the 
/usr/lib/python1.x/config/ directory, which contains various files required 
for compiling Python extensions.
Install the python-devel RPM to get the necessary files.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.013.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.013.htp">Log info</A>

/ Last changed on Tue Jan 26 13:44:04 1999 by
<A HREF="mailto:akuchling@acm.org">A.M. Kuchling</A>
<P>

<HR>
<H2><A NAME="5.14">5.14. What does &quot;SystemError: _PyImport_FixupExtension: module yourmodule not loaded&quot; mean?</A></H2>
This means that you have created an extension module named "yourmodule", but your module init function does not initialize with that name.
<P>
Every module init function will have a line similar to:
<P>
<PRE>
  module = Py_InitModule("yourmodule", yourmodule_functions);
</PRE>
If the string passed to this function is not the same name as your extenion module, the SystemError will be raised.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.014.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.014.htp">Log info</A>

/ Last changed on Thu Mar 25 07:16:08 1999 by
<A HREF="mailto:mhammond@skippinet.com.au">Mark Hammond</A>
<P>

<HR>
<H2><A NAME="5.15">5.15. How to tell &quot;incomplete input&quot; from &quot;invalid input&quot;?</A></H2>
Sometimes you want to emulate the Python interactive interpreter's
behavior, where it gives you a continuation prompt when the input
is incomplete (e.g. you typed the start of an "if" statement
or you didn't close your parentheses or triple string quotes),
but it gives you a syntax error message immediately when the input
is invalid.
<P>
In Python you can use the codeop module, which approximates the
parser's behavior sufficiently.  IDLE uses this, for example.
<P>
The easiest way to do it in C is to call PyRun_InteractiveLoop()
(in a separate thread maybe) and let the Python interpreter handle
the input for you. You can also set the PyOS_ReadlineFunctionPointer
to point at your custom input function. See Modules/readline.c and
Parser/myreadline.c for more hints.
<P>
However sometimes you have to run the embedded Python interpreter
in the same thread as your rest application and you can't allow the
PyRun_InteractiveLoop() to stop while waiting for user input.
The one solution then is to call PyParser_ParseString()
and test for e.error equal to E_EOF (then the input is incomplete).
Sample code fragment, untested, inspired by code from  Alex Farber:
<P>
<PRE>
  #include &lt;Python.h&gt;
  #include &lt;node.h&gt;
  #include &lt;errcode.h&gt;
  #include &lt;grammar.h&gt;
  #include &lt;parsetok.h&gt;
  #include &lt;compile.h&gt;
</PRE>
<PRE>
  int testcomplete(char *code)
    /* code should end in \n */
    /* return -1 for error, 0 for incomplete, 1 for complete */
  {
    node *n;
    perrdetail e;
</PRE>
<PRE>
    n = PyParser_ParseString(code, &amp;_PyParser_Grammar,
                             Py_file_input, &amp;e);
    if (n == NULL) {
      if (e.error == E_EOF) 
        return 0;
      return -1;
    }
</PRE>
<PRE>
    PyNode_Free(n);
    return 1;
  }
</PRE>
Another solution is trying to compile the received string with 
Py_CompileString(). If it compiles fine - try to execute the returned 
code object by calling PyEval_EvalCode(). Otherwise save the input for 
later. If the compilation fails, find out if it's an error or just 
more input is required - by extracting the message string from the 
exception tuple and comparing it to the "unexpected EOF while parsing".
Here is a complete example using the GNU readline library (you may
want to ignore SIGINT while calling readline()):
<P>
<PRE>
  #include &lt;stdio.h&gt;
  #include &lt;readline.h&gt;
</PRE>
<PRE>
  #include &lt;Python.h&gt;
  #include &lt;object.h&gt;
  #include &lt;compile.h&gt;
  #include &lt;eval.h&gt;
</PRE>
<PRE>
  int main (int argc, char* argv[])
  {
    int i, j, done = 0;                          /* lengths of line, code */
    char ps1[] = ">>> ";
    char ps2[] = "... ";
    char *prompt = ps1;
    char *msg, *line, *code = NULL;
    PyObject *src, *glb, *loc;
    PyObject *exc, *val, *trb, *obj, *dum;
</PRE>
<PRE>
    Py_Initialize ();
    loc = PyDict_New ();
    glb = PyDict_New ();
    PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ());
</PRE>
<PRE>
    while (!done)
    {
      line = readline (prompt);
</PRE>
<PRE>
      if (NULL == line)                          /* CTRL-D pressed */
      {
        done = 1;
      }
      else
      {
        i = strlen (line);
</PRE>
<PRE>
        if (i > 0)
          add_history (line);                    /* save non-empty lines */
</PRE>
<PRE>
        if (NULL == code)                        /* nothing in code yet */
          j = 0;
        else
          j = strlen (code);
</PRE>
<PRE>
        code = realloc (code, i + j + 2);
        if (NULL == code)                        /* out of memory */
          exit (1);
</PRE>
<PRE>
        if (0 == j)                              /* code was empty, so */
          code[0] = '\0';                        /* keep strncat happy */
</PRE>
<PRE>
        strncat (code, line, i);                 /* append line to code */
        code[i + j] = '\n';                      /* append '\n' to code */
        code[i + j + 1] = '\0';
</PRE>
<PRE>
        src = Py_CompileString (code, "&lt;stdin&gt;", Py_single_input);       
</PRE>
<PRE>
        if (NULL != src)                         /* compiled just fine - */
        {
          if (ps1  == prompt ||                  /* "&gt;&gt;&gt; " or */
              '\n' == code[i + j - 1])           /* "... " and double '\n' */
          {                                               /* so execute it */
            dum = PyEval_EvalCode ((PyCodeObject *)src, glb, loc);
            Py_XDECREF (dum);
            Py_XDECREF (src);
            free (code);
            code = NULL;
            if (PyErr_Occurred ())
              PyErr_Print ();
            prompt = ps1;
          }
        }                                        /* syntax error or E_EOF? */
        else if (PyErr_ExceptionMatches (PyExc_SyntaxError))           
        {
          PyErr_Fetch (&amp;exc, &amp;val, &amp;trb);        /* clears exception! */
</PRE>
<PRE>
          if (PyArg_ParseTuple (val, "sO", &amp;msg, &amp;obj) &amp;&amp;
              !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */
          {
            Py_XDECREF (exc);
            Py_XDECREF (val);
            Py_XDECREF (trb);
            prompt = ps2;
          }
          else                                   /* some other syntax error */
          {
            PyErr_Restore (exc, val, trb);
            PyErr_Print ();
            free (code);
            code = NULL;
            prompt = ps1;
          }
        }
        else                                     /* some non-syntax error */
        {
          PyErr_Print ();
          free (code);
          code = NULL;
          prompt = ps1;
        }
</PRE>
<PRE>
        free (line);
      }
    }
</PRE>
<PRE>
    Py_XDECREF(glb);
    Py_XDECREF(loc);
    Py_Finalize();
    exit(0);
  }
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.015.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.015.htp">Log info</A>

/ Last changed on Wed Mar 15 09:47:24 2000 by
<A HREF="mailto:farber@cpan.org">Alex Farber</A>
<P>

<HR>
<H2><A NAME="5.16">5.16. How do I debug an extension?</A></H2>
When using gdb with dynamically loaded extensions, you can't set a 
breakpoint in your extension until your extension is loaded.
<P>
In your .gdbinit file (or interactively), add the command
<P>
br _PyImport_LoadDynamicModule
<P>
<P>
$ gdb /local/bin/python
<P>
gdb) run myscript.py
<P>
gdb) continue # repeat until your extension is loaded
<P>
gdb) finish   # so that your extension is loaded
<P>
gdb) br myfunction.c:50
<P>
gdb) continue
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.016.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.016.htp">Log info</A>

/ Last changed on Fri Oct 20 11:10:32 2000 by
<A HREF="mailto:vanandel@ucar.edu">Joe VanAndel</A>
<P>

<HR>
<H2><A NAME="5.17">5.17. How do I find undefined Linux g++ symbols, __builtin_new or __pure_virtural</A></H2>
To dynamically load g++ extension modules, you must recompile python, relink python using g++ (change LINKCC in the python Modules Makefile), and link your extension module using g++ (e.g., "g++ -shared -o mymodule.so mymodule.o").
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.017.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.017.htp">Log info</A>

/ Last changed on Sun Jan 14 18:03:51 2001 by
<A HREF="mailto:dbo@angryduck.com">douglas orr</A>
<P>

<HR>
<H2><A NAME="5.18">5.18. How do I define and create objects corresponding to built-in/extension types</A></H2>
Usually you would like to be able to inherit from a Python type when
you ask this question. The bottom line for Python 2.2 is: types and classes are miscible. You build instances by calling classes, and you can build subclasses to your heart's desire.
<P>
You need to be careful when instantiating immutable types like integers or strings. See <A HREF="http://www.amk.ca/python/2.2">http://www.amk.ca/python/2.2</A>/, section 2, for details.
<P>
Prior to version 2.2, Python (like Java) insisted that there are first-class and second-class objects (the former are types, the latter classes), and never the twain shall meet.
<P>
The library has, however, done a good job of providing class wrappers for the more commonly desired objects (see UserDict, UserList and UserString for examples), and more are always welcome if you happen to be in the mood to write code. These wrappers still exist in Python 2.2.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq05.018.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq05.018.htp">Log info</A>

/ Last changed on Mon Jun 10 15:14:07 2002 by
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
<P>

<HR>
<H1>6. Python's design</H1>

<HR>
<H2><A NAME="6.1">6.1. Why isn't there a switch or case statement in Python?</A></H2>
You can do this easily enough with a sequence of
if... elif... elif... else.  There have been some proposals for switch
statement syntax, but there is no consensus (yet) on whether and how
to do range tests.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.001.htp">Log info</A>
<P>

<HR>
<H2><A NAME="6.2">6.2. Why does Python use indentation for grouping of statements?</A></H2>
Basically I believe that using indentation for grouping is
extremely elegant and contributes a lot to the clarity of the average
Python program.  Most people learn to love this feature after a while.
Some arguments for it:
<P>
Since there are no begin/end brackets there cannot be a disagreement
between grouping perceived by the parser and the human reader.  I
remember long ago seeing a C fragment like this:
<P>
<PRE>
        if (x &lt;= y)
                x++;
                y--;
        z++;
</PRE>
and staring a long time at it wondering why y was being decremented
even for x > y...  (And I wasn't a C newbie then either.)
<P>
Since there are no begin/end brackets, Python is much less prone to
coding-style conflicts.  In C there are loads of different ways to
place the braces (including the choice whether to place braces around
single statements in certain cases, for consistency).  If you're used
to reading (and writing) code that uses one style, you will feel at
least slightly uneasy when reading (or being required to write)
another style.
Many coding styles place begin/end brackets on a line by themself.
This makes programs considerably longer and wastes valuable screen
space, making it harder to get a good overview over a program.
Ideally, a function should fit on one basic tty screen (say, 20
lines).  20 lines of Python are worth a LOT more than 20 lines of C.
This is not solely due to the lack of begin/end brackets (the lack of
declarations also helps, and the powerful operations of course), but
it certainly helps!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.002.htp">Log info</A>

/ Last changed on Wed May 21 16:00:15 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.3">6.3. Why are Python strings immutable?</A></H2>
There are two advantages.  One is performance: knowing that a
string is immutable makes it easy to lay it out at construction time
-- fixed and unchanging storage requirements.  (This is also one of
the reasons for the distinction between tuples and lists.)  The
other is that strings in Python are considered as "elemental" as
numbers.  No amount of activity will change the value 8 to anything
else, and in Python, no amount of activity will change the string
"eight" to anything else.  (Adapted from Jim Roskind)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.003.htp">Log info</A>
<P>

<HR>
<H2><A NAME="6.4">6.4. Delete</A></H2>
<P>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.004.htp">Log info</A>

/ Last changed on Tue Jan  2 03:05:25 2001 by
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
<P>

<HR>
<H2><A NAME="6.5">6.5. Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</A></H2>
The major reason is history. Functions were used for those
operations that were generic for a group of types and which
were intended to work even for objects that didn't have
methods at all (e.g. numbers before type/class unification
began, or tuples).
<P>
It is also convenient to have a function that can readily be applied
to an amorphous collection of objects when you use the functional features of Python (map(), apply() et al).
<P>
In fact, implementing len(), max(), min() as a built-in function is
actually less code than implementing them as methods for each type.
One can quibble about individual cases but it's a part of Python,
and it's too late to change such things fundamentally now. The
functions have to remain to avoid massive code breakage.
<P>
Note that for string operations Python has moved from external functions
(the string module) to methods.  However, len() is still a function.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.005.htp">Log info</A>

/ Last changed on Thu May 30 14:08:58 2002 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="6.6">6.6. Why can't I derive a class from built-in types (e.g. lists or files)?</A></H2>
As of Python 2.2, you can derive from built-in types.  For previous versions, the answer is:
<P>
This is caused by the relatively late addition of (user-defined)
classes to the language -- the implementation framework doesn't easily
allow it.  See the answer to question 4.2 for a work-around.  This
<I>may</I> be fixed in the (distant) future.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.006.htp">Log info</A>

/ Last changed on Thu May 23 02:53:22 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="6.7">6.7. Why must 'self' be declared and used explicitly in method definitions and calls?</A></H2>
So, is your current programming language C++ or Java? :-)
When classes were added to Python, this was (again) the simplest way of
implementing methods without too many changes to the interpreter.  The
idea was borrowed from Modula-3.  It turns out to be very useful, for
a variety of reasons.
<P>
First, it makes it more obvious that you are using a method or
instance attribute instead of a local variable.  Reading "self.x" or
"self.meth()" makes it absolutely clear that an instance variable or
method is used even if you don't know the class definition by heart.
In C++, you can sort of tell by the lack of a local variable
declaration (assuming globals are rare or easily recognizable) -- but
in Python, there are no local variable declarations, so you'd have to
look up the class definition to be sure.
<P>
Second, it means that no special syntax is necessary if you want to
explicitly reference or call the method from a particular class.  In
C++, if you want to use a method from base class that is overridden in
a derived class, you have to use the :: operator -- in Python you can
write baseclass.methodname(self, &lt;argument list&gt;).  This is
particularly useful for __init__() methods, and in general in cases
where a derived class method wants to extend the base class method of
the same name and thus has to call the base class method somehow.
<P>
Lastly, for instance variables, it solves a syntactic problem with
assignment: since local variables in Python are (by definition!) those
variables to which a value assigned in a function body (and that
aren't explicitly declared global), there has to be some way to tell
the interpreter that an assignment was meant to assign to an instance
variable instead of to a local variable, and it should preferably be
syntactic (for efficiency reasons).  C++ does this through
declarations, but Python doesn't have declarations and it would be a
pity having to introduce them just for this purpose.  Using the
explicit "self.var" solves this nicely.  Similarly, for using instance
variables, having to write "self.var" means that references to
unqualified names inside a method don't have to search the instance's
directories.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.007.htp">Log info</A>

/ Last changed on Fri Jan 12 08:01:50 2001 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="6.8">6.8. Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</A></H2>
Answer 1: Unfortunately, the interpreter pushes at least one C stack
frame for each Python stack frame.  Also, extensions can call back into
Python at almost random moments.  Therefore a complete threads
implementation requires thread support for C.
<P>
Answer 2: Fortunately, there is Stackless Python, which has a completely redesigned interpreter loop that avoids the C stack. It's still experimental but looks very promising. Although it is binary compatible with standard Python, it's still unclear whether Stackless will make it into the core -- maybe it's just too revolutionary. Stackless Python currently lives here: <A HREF="http://www.stackless.com">http://www.stackless.com</A>. A microthread implementation that uses it can be found here: <A HREF="http://world.std.com/~wware/uthread.html">http://world.std.com/~wware/uthread.html</A>.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.008.htp">Log info</A>

/ Last changed on Sat Apr 15 08:18:16 2000 by
<A HREF="mailto:just@letterror.com">Just van Rossum</A>
<P>

<HR>
<H2><A NAME="6.9">6.9. Why can't lambda forms contain statements?</A></H2>
Python lambda forms cannot contain statements because Python's
syntactic framework can't handle statements nested inside expressions.
<P>
However, in Python, this is not a serious problem.  Unlike lambda
forms in other languages, where they add functionality, Python lambdas
are only a shorthand notation if you're too lazy to define a function.
<P>
Functions are already first class objects in Python, and can be
declared in a local scope.  Therefore the only advantage of using a
lambda form instead of a locally-defined function is that you don't need to invent a name for the function -- but that's just a local variable to which the function object (which is exactly the same type of object that a lambda form yields) is assigned!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.009.htp">Log info</A>

/ Last changed on Sun Jun 14 14:15:17 1998 by
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
<P>

<HR>
<H2><A NAME="6.10">6.10. [deleted]</A></H2>
[lambda vs non-nested scopes used to be here]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.010.htp">Log info</A>

/ Last changed on Thu Mar 21 05:20:56 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="6.11">6.11. [deleted]</A></H2>
[recursive functions vs non-nested scopes used to be here]
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.011.htp">Log info</A>

/ Last changed on Thu Mar 21 05:22:04 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="6.12">6.12. Why is there no more efficient way of iterating over a dictionary than first constructing the list of keys()?</A></H2>
As of Python 2.2, you can now iterate over a dictionary directly,
using the new implied dictionary iterator:
<P>
<PRE>
    for k in d: ...
</PRE>
There are also methods returning iterators over the values and items:
<P>
<PRE>
    for k in d.iterkeys(): # same as above
    for v in d.itervalues(): # iterate over values
    for k, v in d.iteritems(): # iterate over items
</PRE>
All these require that you do not modify the dictionary during the loop.
<P>
For previous Python versions, the following defense should do:
<P>
Have you tried it?  I bet it's fast enough for your purposes!  In
most cases such a list takes only a few percent of the space occupied
by the dictionary.  Apart from the fixed header,
the list needs only 4 bytes (the size of a pointer) per
key.  A dictionary uses 12 bytes per key plus between 30 and 70
percent hash table overhead, plus the space for the keys and values.
By necessity, all keys are distinct objects, and a string object (the most
common key type) costs at least 20 bytes plus the length of the
string.  Add to that the values contained in the dictionary, and you
see that 4 bytes more per item really isn't that much more memory...
<P>
A call to dict.keys() makes one fast scan over the dictionary
(internally, the iteration function does exist) copying the pointers
to the key objects into a pre-allocated list object of the right size.
The iteration time isn't lost (since you'll have to iterate anyway --
unless in the majority of cases your loop terminates very prematurely
(which I doubt since you're getting the keys in random order).
<P>
I don't expose the dictionary iteration operation to Python
programmers because the dictionary shouldn't be modified during the
entire iteration -- if it is, there's a small chance that the
dictionary is reorganized because the hash table becomes too full, and
then the iteration may miss some items and see others twice.  Exactly
because this only occurs rarely, it would lead to hidden bugs in
programs: it's easy never to have it happen during test runs if you
only insert or delete a few items per iteration -- but your users will 
surely hit upon it sooner or later.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.012.htp">Log info</A>

/ Last changed on Fri May 24 21:24:08 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.13">6.13. Can Python be compiled to machine code, C or some other language?</A></H2>
Not easily.  Python's high level data types, dynamic typing of
objects and run-time invocation of the interpreter (using eval() or
exec) together mean that a "compiled" Python program would probably
consist mostly of calls into the Python run-time system, even for
seemingly simple operations like "x+1".
<P>
Several projects described in the Python newsgroup or at past
Python conferences have shown that this approach is feasible,
although the speedups reached so far are only modest (e.g. 2x).
JPython uses the same strategy for compiling to Java bytecode.
(Jim Hugunin has demonstrated that in combination with whole-program
analysis, speedups of 1000x are feasible for small demo programs.
See the website for the 1997 Python conference.)
<P>
Internally, Python source code is always translated into a "virtual
machine code" or "byte code" representation before it is interpreted
(by the "Python virtual machine" or "bytecode interpreter").  In order
to avoid the overhead of parsing and translating modules that rarely
change over and over again, this byte code is written on a file whose
name ends in ".pyc" whenever a module is parsed (from a file whose
name ends in ".py").  When the corresponding .py file is changed, it
is parsed and translated again and the .pyc file is rewritten.
<P>
There is no performance difference once the .pyc file has been loaded
(the bytecode read from the .pyc file is exactly the same as the bytecode
created by direct translation).  The only difference is that loading
code from a .pyc file is faster than parsing and translating a .py
file, so the presence of precompiled .pyc files will generally improve
start-up time of Python scripts.  If desired, the Lib/compileall.py
module/script can be used to force creation of valid .pyc files for a
given set of modules.
<P>
Note that the main script executed by Python, even if its filename
ends in .py, is not compiled to a .pyc file.  It is compiled to
bytecode, but the bytecode is not saved to a file.
<P>
If you are looking for a way to translate Python programs in order to
distribute them in binary form, without the need to distribute the
interpreter and library as well, have a look at the freeze.py script
in the Tools/freeze directory.  This creates a single binary file
incorporating your program, the Python interpreter, and those parts of
the Python library that are needed by your program.  Of course, the
resulting binary will only run on the same type of platform as that
used to create it.
<P>
Newsflash: there are now several programs that do this, to some extent.
Look for Psyco, Pyrex, PyInline, Py2Cmod, and Weave.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.013.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.013.htp">Log info</A>

/ Last changed on Fri May 24 21:26:19 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.14">6.14. How does Python manage memory?</A></H2>
The details of Python memory management depend on the implementation.
The standard Python implementation (the C implementation) uses reference
counting and another mechanism to collect reference cycles.
<P>
Jython relies on the Java runtime; so it uses
the JVM's garbage collector.  This difference can cause some subtle
porting problems if your Python code depends on the behavior of
the reference counting implementation.
<P>
The reference cycle collector was added in CPython 2.0. It
periodically executes a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. A new gc module provides functions to perform a garbage collection, obtain debugging statistics, and tuning the collector's parameters. 
<P>
The detection of cycles can be disabled when Python is compiled, if you can't afford even a tiny speed penalty or suspect that the cycle collection is buggy, by specifying the "--without-cycle-gc" switch when running the configure script. 
<P>
Sometimes objects get stuck in "tracebacks" temporarily and hence are not deallocated when you might expect.  Clear the tracebacks via
<P>
<PRE>
       import sys
       sys.exc_traceback = sys.last_traceback = None
</PRE>
Tracebacks are used for reporting errors and implementing debuggers and related things.  They contain a portion of the program state extracted during the handling of an exception (usually the most recent exception).
<P>
In the absence of circularities and modulo tracebacks, Python programs need not explicitly manage memory.
<P>
Why python doesn't use a more traditional garbage collection
scheme? For one thing, unless this were
added to C as a standard feature, it's a portability pain in the ass.
And yes, I know about the Xerox library.  It has bits of assembler
code for <I>most</I> <I>common</I> platforms.  Not for all.  And although it is
mostly transparent, it isn't completely transparent (when I once
linked Python with it, it dumped core).
<P>
Traditional GC also becomes a problem when Python gets embedded into
other applications.  While in a stand-alone Python it may be fine to
replace the standard malloc() and free() with versions provided by the
GC library, an application embedding Python may want to have its <I>own</I>
substitute for malloc() and free(), and may not want Python's.  Right
now, Python works with anything that implements malloc() and free()
properly.
<P>
In Jython, the following code (which is 
fine in C Python) will probably run out of file descriptors long before
it runs out of memory:
<P>
<PRE>
        for file in &lt;very long list of files&gt;:
                f = open(file)
                c = f.read(1)
</PRE>
Using the current reference counting and destructor scheme, each new
assignment to f closes the previous file.  Using GC, this is not
guaranteed.  Sure, you can think of ways to fix this.  But it's not
off-the-shelf technology.  If you want to write code that will
work with any Python implementation, you should explicitly close
the file; this will work regardless of GC:
<P>
<PRE>
       for file in &lt;very long list of files&gt;:
                f = open(file)
                c = f.read(1)
                f.close()
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.014.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.014.htp">Log info</A>

/ Last changed on Thu Mar 21 05:35:38 2002 by
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
<P>

<HR>
<H2><A NAME="6.15">6.15. Why are there separate tuple and list data types?</A></H2>
This is done so that tuples can be immutable while lists are mutable.
<P>
Immutable tuples are useful in situations where you need to pass a few
items to a function and don't want the function to modify the tuple;
for example,
<P>
<PRE>
	point1 = (120, 140)
	point2 = (200, 300)
	record(point1, point2)
	draw(point1, point2)
</PRE>
You don't want to have to think about what would happen if record()
changed the coordinates -- it can't, because the tuples are immutable.
<P>
On the other hand, when creating large lists dynamically, it is
absolutely crucial that they are mutable -- adding elements to a tuple
one by one requires using the concatenation operator, which makes it
quadratic in time.
<P>
As a general guideline, use tuples like you would use structs in C or
records in Pascal, use lists like (variable length) arrays.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.015.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.015.htp">Log info</A>

/ Last changed on Fri May 23 15:26:03 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.16">6.16. How are lists implemented?</A></H2>
Despite what a Lisper might think, Python's lists are really
variable-length arrays.  The implementation uses a contiguous
array of references to other objects, and keeps a pointer
to this array (as well as its length) in a list head structure.
<P>
This makes indexing a list (a[i]) an operation whose cost is
independent of the size of the list or the value of the index.
<P>
When items are appended or inserted, the array of references is resized.
Some cleverness is applied to improve the performance of appending
items repeatedly; when the array must be grown, some extra space
is allocated so the next few times don't require an actual resize.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.016.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.016.htp">Log info</A>

/ Last changed on Fri May 23 15:32:24 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.17">6.17. How are dictionaries implemented?</A></H2>
Python's dictionaries are implemented as resizable hash tables.
<P>
Compared to B-trees, this gives better performance for lookup
(the most common operation by far) under most circumstances,
and the implementation is simpler.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.017.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.017.htp">Log info</A>

/ Last changed on Fri May 23 23:51:14 1997 by
<A HREF="mailto:Vladimir.Marangozov@inrialpes.fr">Vladimir Marangozov</A>
<P>

<HR>
<H2><A NAME="6.18">6.18. Why must dictionary keys be immutable?</A></H2>
The hash table implementation of dictionaries uses a hash value
calculated from the key value to find the key.  If the key were
a mutable object, its value could change, and thus its hash could
change.  But since whoever changes the key object can't tell that
is incorporated in a dictionary, it can't move the entry around in
the dictionary.  Then, when you try to look up the same object
in the dictionary, it won't be found, since its hash value is different;
and if you try to look up the old value, it won't be found either,
since the value of the object found in that hash bin differs.
<P>
If you think you need to have a dictionary indexed with a list,
try to use a tuple instead.  The function tuple(l) creates a tuple
with the same entries as the list l.
<P>
Some unacceptable solutions that have been proposed:
<P>
- Hash lists by their address (object ID).  This doesn't work because
if you construct a new list with the same value it won't be found;
e.g.,
<P>
<PRE>
  d = {[1,2]: '12'}
  print d[[1,2]]
</PRE>
will raise a KeyError exception because the id of the [1,2] used
in the second line differs from that in the first line.
In other words, dictionary keys should be compared using '==', not using 'is'.
<P>
- Make a copy when using a list as a key.  This doesn't work because
the list (being a mutable object) could contain a reference to itself,
and then the copying code would run into an infinite loop.
<P>
- Allow lists as keys but tell the user not to modify them.  This would
allow a class of hard-to-track bugs in programs that I'd rather not see;
it invalidates an important invariant of dictionaries (every value in
d.keys() is usable as a key of the dictionary).
<P>
- Mark lists as read-only once they are used as a dictionary key.
The problem is that it's not just the top-level object that could change
its value; you could use a tuple containing a list as a key.  Entering
anything as a key into a dictionary would require marking all objects
reachable from there as read-only -- and again, self-referential objects
could cause an infinite loop again (and again and again).
<P>
There is a trick to get around this if you need to, but
use it at your own risk:  You
can wrap a mutable structure inside a class instance which
has both a __cmp__ and a __hash__ method.  
<P>
<PRE>
   class listwrapper:
        def __init__(self, the_list):
              self.the_list = the_list
        def __cmp__(self, other):
              return self.the_list == other.the_list
        def __hash__(self):
              l = self.the_list
              result = 98767 - len(l)*555
              for i in range(len(l)):
                   try:
                        result = result + (hash(l[i]) % 9999999) * 1001 + i
                   except:
                        result = (result % 7777777) + i * 333
              return result
</PRE>
Note that the hash computation is complicated by the
possibility that some members of the list may be unhashable
and also by the possibility of arithmetic overflow.
<P>
You must make
sure that the hash value for all such wrapper objects that reside in a
dictionary (or other hash based structure), remain fixed while
the object is in the dictionary (or other structure).
<P>
Furthermore it must always be the case that if
o1 == o2 (ie o1.__cmp__(o2)==0) then hash(o1)==hash(o2)
(ie, o1.__hash__() == o2.__hash__()), regardless of whether
the object is in a dictionary or not.
If you fail to meet these restrictions dictionaries and other
hash based structures may misbehave!
<P>
In the case of listwrapper above whenever the wrapper
object is in a dictionary the wrapped list must not change
to avoid anomalies.  Don't do this unless you are prepared
to think hard about the requirements and the consequences
of not meeting them correctly.  You've been warned!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.018.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.018.htp">Log info</A>

/ Last changed on Thu Jul 10 10:08:40 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="6.19">6.19. How the heck do you make an array in Python?</A></H2>
["this", 1, "is", "an", "array"]
<P>
Lists are arrays in the C or Pascal sense of the word (see question
6.16).  The array module also provides methods for creating arrays
of fixed types with compact representations (but they are slower to
index than lists).  Also note that the Numerics extensions and others
define array-like structures with various characteristics as well.
<P>
To get Lisp-like lists, emulate cons cells
<P>
<PRE>
    lisp_list = ("like",  ("this",  ("example", None) ) )
</PRE>
using tuples (or lists, if you want mutability).  Here the analogue
of lisp car is lisp_list[0] and the analogue of cdr is lisp_list[1].
Only do this if you're sure you really need to (it's usually a lot
slower than using Python lists).
<P>
Think of Python lists as mutable heterogeneous arrays of
Python objects (say that 10 times fast :) ).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.019.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.019.htp">Log info</A>

/ Last changed on Wed Aug 13 07:08:27 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="6.20">6.20. Why doesn't list.sort() return the sorted list?</A></H2>
In situations where performance matters, making a copy of the list
just to sort it would be wasteful.  Therefore, list.sort() sorts
the list in place.  In order to remind you of that fact, it does
not return the sorted list.  This way, you won't be fooled into
accidentally overwriting a list when you need a sorted copy but also
need to keep the unsorted version around.
<P>
As a result, here's the idiom to iterate over the keys of a dictionary
in sorted order:
<P>
<PRE>
	keys = dict.keys()
	keys.sort()
	for key in keys:
		...do whatever with dict[key]...
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.020.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.020.htp">Log info</A>

/ Last changed on Thu Dec  2 17:01:52 1999 by
<A HREF="mailto:fdrake@acm.org">Fred L. Drake, Jr.</A>
<P>

<HR>
<H2><A NAME="6.21">6.21. How do you specify and enforce an interface spec in Python?</A></H2>
An interfaces specification for a module as provided
by languages such as C++ and java describes the prototypes
for the methods and functions of the module.  Many feel
that compile time enforcement of interface specifications
help aid in the construction of large programs.  Python
does not support interface specifications directly, but many
of their advantages can be obtained by an appropriate
test discipline for components, which can often be very
easily accomplished in Python.  There is also a tool, PyChecker,
which can be used to find problems due to subclassing.
<P>
A good test suite for a module can at
once provide a regression test and serve as a module interface
specification (even better since it also gives example usage).  Look to
many of the standard libraries which often have a "script
interpretation" which provides a simple "self test."  Even
modules which use complex external interfaces can often
be tested in isolation using trivial "stub" emulations of the
external interface.
<P>
An appropriate testing discipline (if enforced) can help
build large complex applications in Python as well as having interface
specifications would do (or better).  Of course Python allows you 
to get sloppy and not do it.  Also you might want to design
your code with an eye to make it easily tested.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.021.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.021.htp">Log info</A>

/ Last changed on Thu May 23 03:05:29 2002 by
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
<P>

<HR>
<H2><A NAME="6.22">6.22. Why do all classes have the same type? Why do instances all have the same type?</A></H2>
The Pythonic use of the word "type" is quite different from
common usage in much of the rest of the programming language
world.  A "type" in Python is a description for an object's operations
as implemented in C.  All classes have the same operations
implemented in C which sometimes "call back" to differing program
fragments implemented in Python, and hence all classes have the
same type.  Similarly at the C level all class instances have the
same C implementation, and hence all instances have the same
type.
<P>
Remember that in Python usage "type" refers to a C implementation
of an object.  To distinguish among instances of different classes
use Instance.__class__, and also look to 4.47.  Sorry for the
terminological confusion, but at this point in Python's development
nothing can be done!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.022.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.022.htp">Log info</A>

/ Last changed on Tue Jul  1 12:35:47 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="6.23">6.23. Why isn't all memory freed when Python exits?</A></H2>
Objects referenced from Python module global name spaces are
not always deallocated when Python exits.
<P>
This may happen if there are circular references (see question
4.17).  There are also certain bits of memory that are allocated
by the C library that are impossible to free (e.g. a tool
like Purify will complain about these).
<P>
But in general, Python 1.5 and beyond
(in contrast with earlier versions) is quite agressive about
cleaning up memory on exit.
<P>
If you want to force Python to delete certain things on deallocation
use the sys.exitfunc hook to force those deletions.  For example
if you are debugging an extension module using a memory analysis
tool and you wish to make Python deallocate almost everything
you might use an exitfunc like this one:
<P>
<PRE>
  import sys
</PRE>
<PRE>
  def my_exitfunc():
       print "cleaning up"
       import sys
       # do order dependant deletions here
       ...
       # now delete everything else in arbitrary order
       for x in sys.modules.values():
            d = x.__dict__
            for name in d.keys():
                 del d[name]
</PRE>
<PRE>
  sys.exitfunc = my_exitfunc
</PRE>
Other exitfuncs can be less drastic, of course.
<P>
(In fact, this one just does what Python now already does itself;
but the example of using sys.exitfunc to force cleanups is still
useful.)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.023.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.023.htp">Log info</A>

/ Last changed on Tue Sep 29 09:46:26 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.24">6.24. Why no class methods or mutable class variables?</A></H2>
The notation
<P>
<PRE>
    instance.attribute(arg1, arg2)
</PRE>
usually translates to the equivalent of
<P>
<PRE>
    Class.attribute(instance, arg1, arg2)
</PRE>
where Class is a (super)class of instance.  Similarly
<P>
<PRE>
    instance.attribute = value
</PRE>
sets an attribute of an instance (overriding any attribute of a class
that instance inherits).  
<P>
Sometimes programmers want to have
different behaviours -- they want a method which does not bind
to the instance and a class attribute which changes in place.
Python does not preclude these behaviours, but you have to
adopt a convention to implement them.  One way to accomplish
this is to use "list wrappers" and global functions.
<P>
<PRE>
   def C_hello():
         print "hello"
</PRE>
<PRE>
   class C:
        hello = [C_hello]
        counter = [0]
</PRE>
<PRE>
    I = C()
</PRE>
Here I.hello[0]() acts very much like a "class method" and
I.counter[0] = 2 alters C.counter (and doesn't override it).
If you don't understand why you'd ever want to do this, that's
because you are pure of mind, and you probably never will
want to do it!  This is dangerous trickery, not recommended
when avoidable.  (Inspired by Tim Peter's discussion.)
<P>
In Python 2.2, you can do this using the new built-in operations
classmethod and staticmethod.
See <A HREF="http://www.python.org/2.2/descrintro.html#staticmethods">http://www.python.org/2.2/descrintro.html#staticmethods</A>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.024.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.024.htp">Log info</A>

/ Last changed on Tue Sep 11 15:59:37 2001 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.25">6.25. Why are default values sometimes shared between objects?</A></H2>
It is often expected that a function CALL creates new objects for default
values. This is not what happens. Default values are created when the
function is DEFINED, that is, there is only one such object that all 
functions refer to. If that object is changed, subsequent calls to the 
function will refer to this changed object. By definition, immutable objects
(like numbers, strings, tuples, None) are safe from change. Changes to mutable 
objects (like dictionaries, lists, class instances) is what causes the
confusion.
<P>
Because of this feature it is good programming practice not to use mutable
objects as default values, but to introduce them in the function.
Don't write:
<P>
<PRE>
	def foo(dict={}):  # XXX shared reference to one dict for all calls
	    ...
</PRE>
but:
<PRE>
	def foo(dict=None):
		if dict is None:
			dict = {} # create a new dict for local namespace
</PRE>
See page 182 of "Internet Programming with Python" for one discussion
of this feature. Or see the top of page 144 or bottom of page 277 in 
"Programming Python" for another discussion.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.025.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.025.htp">Log info</A>

/ Last changed on Sat Aug 16 07:03:35 1997 by
<A HREF="mailto:cjr@bound.xs4all.nl">Case Roole</A>
<P>

<HR>
<H2><A NAME="6.26">6.26. Why no goto?</A></H2>
Actually, you can use exceptions to provide a "structured goto"
that even works across function calls.  Many feel that exceptions
can conveniently emulate all reasonable uses of the "go" or "goto"
constructs of C, Fortran, and other languages.  For example:
<P>
<PRE>
   class label: pass # declare a label
   try:
        ...
        if (condition): raise label() # goto label
        ...
   except label: # where to goto
        pass
   ...
</PRE>
This doesn't allow you to jump into the middle of a loop, but
that's usually considered an abuse of goto anyway.  Use sparingly.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.026.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.026.htp">Log info</A>

/ Last changed on Wed Sep 10 07:16:44 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="6.27">6.27. How do you make a higher order function in Python?</A></H2>
You have two choices: you can use default arguments and override
them or you can use "callable objects."  For example suppose you
wanted to define linear(a,b) which returns a function f where f(x)
computes the value a*x+b.  Using default arguments:
<P>
<PRE>
     def linear(a,b):
         def result(x, a=a, b=b):
             return a*x + b
         return result
</PRE>
Or using callable objects:
<P>
<PRE>
     class linear:
        def __init__(self, a, b):
            self.a, self.b = a,b
        def __call__(self, x):
            return self.a * x + self.b
</PRE>
In both cases:
<P>
<PRE>
     taxes = linear(0.3,2)
</PRE>
gives a callable object where taxes(10e6) == 0.3 * 10e6 + 2.
<P>
The defaults strategy has the disadvantage that the default arguments
could be accidentally or maliciously overridden.  The callable objects
approach has the disadvantage that it is a bit slower and a bit
longer.  Note however that a collection of callables can share
their signature via inheritance.  EG
<P>
<PRE>
      class exponential(linear):
         # __init__ inherited
         def __call__(self, x):
             return self.a * (x ** self.b)
</PRE>
On comp.lang.python, <A HREF="mailto:zenin@bawdycaste.org">zenin@bawdycaste.org</A> points out that
an object can encapsulate state for several methods in order
to emulate the "closure" concept from functional programming
languages, for example:
<P>
<PRE>
    class counter:
        value = 0
        def set(self, x): self.value = x
        def up(self): self.value=self.value+1
        def down(self): self.value=self.value-1
</PRE>
<PRE>
    count = counter()
    inc, dec, reset = count.up, count.down, count.set
</PRE>
Here inc, dec and reset act like "functions which share the
same closure containing the variable count.value" (if you
like that way of thinking).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.027.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.027.htp">Log info</A>

/ Last changed on Fri Sep 25 08:38:35 1998 by
<A HREF="mailto:arw@pythonpros.com">Aaron Watters</A>
<P>

<HR>
<H2><A NAME="6.28">6.28. Why do I get a SyntaxError for a 'continue' inside a 'try'?</A></H2>
This is an implementation limitation,
caused by the extremely simple-minded
way Python generates bytecode.  The try block pushes something on the
"block stack" which the continue would have to pop off again.  The
current code generator doesn't have the data structures around so that 
'continue' can generate the right code.
<P>
Note that JPython doesn't have this restriction!
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.028.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.028.htp">Log info</A>

/ Last changed on Fri May 22 15:01:07 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="6.29">6.29. Why can't raw strings (r-strings) end with a backslash?</A></H2>
More precisely, they can't end with an odd number of backslashes:
the unpaired backslash at the end escapes the closing quote character,
leaving an unterminated string.
<P>
Raw strings were designed to ease creating input for processors (chiefly
regular expression engines) that want to do their own backslash escape processing. Such processors consider an unmatched trailing backslash to be an error anyway, so raw strings disallow that.  In return, they allow you to pass on the string quote character by escaping it with a backslash.  These rules work well when r-strings are used for their intended purpose.
<P>
If you're trying to build Windows pathnames, note that all Windows system calls accept forward slashes too:
<P>
<PRE>
    f = open("/mydir/file.txt") # works fine!
</PRE>
If you're trying to build a pathname for a DOS command, try e.g. one of
<P>
<PRE>
    dir = r"\this\is\my\dos\dir" "\\"
    dir = r"\this\is\my\dos\dir\ "[:-1]
    dir = "\\this\\is\\my\\dos\\dir\\"
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.029.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.029.htp">Log info</A>

/ Last changed on Mon Jul 13 20:50:20 1998 by
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
<P>

<HR>
<H2><A NAME="6.30">6.30. Why can't I use an assignment in an expression?</A></H2>
Many people used to C or Perl complain that they want to be able to
use e.g. this C idiom:
<P>
<PRE>
    while (line = readline(f)) {
        ...do something with line...
    }
</PRE>
where in Python you're forced to write this:
<P>
<PRE>
    while 1:
        line = f.readline()
        if not line:
            break
        ...do something with line...
</PRE>
This issue comes up in the Python newsgroup with alarming frequency
-- search Deja News for past messages about assignment expression.
The reason for not allowing assignment in Python expressions
is a common, hard-to-find bug in those other languages,
caused by this construct:
<P>
<PRE>
    if (x = 0) {
        ...error handling...
    }
    else {
        ...code that only works for nonzero x...
    }
</PRE>
Many alternatives have been proposed.  Most are hacks that save some
typing but use arbitrary or cryptic syntax or keywords,
and fail the simple criterion that I use for language change proposals:
it should intuitively suggest the proper meaning to a human reader
who has not yet been introduced with the construct.
<P>
The earliest time something can be done about this will be with
Python 2.0 -- if it is decided that it is worth fixing.
An interesting phenomenon is that most experienced Python programmers
recognize the "while 1" idiom and don't seem to be missing the
assignment in expression construct much; it's only the newcomers
who express a strong desire to add this to the language.
<P>
One fairly elegant solution would be to introduce a new operator
for assignment in expressions spelled ":=" -- this avoids the "="
instead of "==" problem.  It would have the same precedence
as comparison operators but the parser would flag combination with
other comparisons (without disambiguating parentheses) as an error.
<P>
Finally -- there's an alternative way of spelling this that seems
attractive but is generally less robust than the "while 1" solution:
<P>
<PRE>
    line = f.readline()
    while line:
        ...do something with line...
        line = f.readline()
</PRE>
The problem with this is that if you change your mind about exactly
how you get the next line (e.g. you want to change it into
sys.stdin.readline()) you have to remember to change two places
in your program -- the second one hidden at the bottom of the loop.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.030.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.030.htp">Log info</A>

/ Last changed on Tue May 18 00:57:41 1999 by
<A HREF="mailto:dalke@bioreason.com">Andrew Dalke</A>
<P>

<HR>
<H2><A NAME="6.31">6.31. Why doesn't Python have a &quot;with&quot; statement like some other languages?</A></H2>
Basically, because such a construct would be terribly ambiguous. Thanks to Carlos Ribeiro for the following remarks:
<P>
Some languages, such as Object Pascal, Delphi, and C++, use static types. So it is possible to know, in an unambiguous way, what member is being assigned in a "with" clause. This is the main point - the compiler <I>always</I>  knows the scope of every variable at compile time.
<P>
Python uses dynamic types. It is impossible to know in advance which 
attribute will be referenced at runtime. Member attributes may be added or removed from objects on the fly. This would make it impossible to know, from a simple reading, what attribute is being referenced - a local one, a global one, or a member attribute.
<P>
For instance, take the following snippet (it is incomplete btw, just to 
give you the idea):
<P>
<PRE>
   def with_is_broken(a):
      with a:
         print x
</PRE>
The snippet assumes that "a" must have a member attribute called "x". 
However, there is nothing in Python that guarantees that. What should 
happen if "a" is, let us say, an integer? And if I have a global variable named "x", will it end up being used inside the with block? As you see, the dynamic nature of Python makes such choices much harder.
<P>
The primary benefit of "with" and similar language features (reduction of code volume) can, however, easily be achieved in Python by assignment. Instead of:
<P>
<PRE>
    function(args).dict[index][index].a = 21
    function(args).dict[index][index].b = 42
    function(args).dict[index][index].c = 63
</PRE>
would become:
<P>
<PRE>
    ref = function(args).dict[index][index]
    ref.a = 21
    ref.b = 42
    ref.c = 63
</PRE>
This also has the happy side-effect of increasing execution speed, since name bindings are resolved at run-time in Python, and the second method only needs to perform the resolution once. If the referenced object does not have a, b and c attributes, of course, the end result is still a run-time exception.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.031.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.031.htp">Log info</A>

/ Last changed on Fri Jan 11 14:32:58 2002 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="6.32">6.32. Why are colons required for if/while/def/class?</A></H2>
The colon is required primarily to enhance readability (one of the
results of the experimental ABC language).  Consider this:
<P>
<PRE>
    if a==b
        print a
</PRE>
versus
<P>
<PRE>
    if a==b:
        print a
</PRE>
Notice how the second one is slightly easier to read.  Notice further how
a colon sets off the example in the second line of this FAQ answer; it's
a standard usage in English.  Finally, the colon makes it easier for
editors with syntax highlighting.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.032.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.032.htp">Log info</A>

/ Last changed on Mon Jun  3 07:22:57 2002 by
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
<P>

<HR>
<H2><A NAME="6.33">6.33. Can't we get rid of the Global Interpreter Lock?</A></H2>
The Global Interpreter Lock (GIL) is often seen as a hindrance to
Python's deployment on high-end multiprocessor server machines,
because a multi-threaded Python program effectively only uses
one CPU, due to the insistence that (almost) all Python code
can only run while the GIL is held.
<P>
Back in the days of Python 1.5, Greg Stein actually implemented
a comprehensive patch set ("free threading")
that removed the GIL, replacing it with
fine-grained locking.  Unfortunately, even on Windows (where locks
are very efficient) this ran ordinary Python code about twice as
slow as the interpreter using the GIL.  On Linux the performance
loss was even worse (pthread locks aren't as efficient).
<P>
Since then, the idea of getting rid of the GIL has occasionally
come up but nobody has found a way to deal with the expected slowdown;
Greg's free threading patch set has not been kept up-to-date for
later Python versions.
<P>
This doesn't mean that you can't make good use of Python on
multi-CPU machines!  You just have to be creative with dividing
the work up between multiple <I>processes</I> rather than multiple
<I>threads</I>.
<P>
<P>
It has been suggested that the GIL should be a per-interpreter-state
lock rather than truly global; interpreters then wouldn't be able
to share objects.  Unfortunately, this isn't likely to happen either.
<P>
It would be a tremendous amount of work, because many object
implementations currently have global state.  E.g. small ints and
small strings are cached; these caches would have to be moved to the
interpreter state.  Other object types have their own free list; these
free lists would have to be moved to the interpreter state.  And so
on.
<P>
And I doubt that it can even be done in finite time, because the same
problem exists for 3rd party extensions.  It is likely that 3rd party
extensions are being written at a faster rate than you can convert
them to store all their global state in the interpreter state.
<P>
And finally, once you have multiple interpreters not sharing any
state, what have you gained over running each interpreter
in a separate process?
<P>

<A HREF="faqw.py?req=edit&amp;file=faq06.033.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq06.033.htp">Log info</A>

/ Last changed on Fri Feb  7 16:34:01 2003 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H1>7. Using Python on non-UNIX platforms</H1>

<HR>
<H2><A NAME="7.1">7.1. Is there a Mac version of Python?</A></H2>
Yes, it is maintained by Jack Jansen.  See Jack's MacPython Page:
<P>
<PRE>
  <A HREF="http://www.cwi.nl/~jack/macpython.html">http://www.cwi.nl/~jack/macpython.html</A>
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.001.htp">Log info</A>

/ Last changed on Fri May  4 09:33:42 2001 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="7.2">7.2. Are there DOS and Windows versions of Python?</A></H2>
Yes.  The core windows binaries are available from <A HREF="http://www.python.org/windows">http://www.python.org/windows</A>/. There is a plethora of Windows extensions available, including a large number of not-always-compatible GUI toolkits.  The core binaries include the standard Tkinter GUI extension.
<P>
Most windows extensions can be found (or referenced) at <A HREF="http://www.python.org/windows">http://www.python.org/windows</A>/ 
<P>
Windows 3.1/DOS support seems to have dropped off recently.  You may need to settle for an old version of Python one these platforms.  One such port is WPY
<P>
WPY: Ports to DOS, Windows 3.1(1), Windows 95, Windows NT and OS/2.
Also contains a GUI package that offers portability between Windows 
(not DOS) and Unix, and native look and feel on both.
<A HREF="ftp://ftp.python.org/pub/python/wpy">ftp://ftp.python.org/pub/python/wpy</A>/.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.002.htp">Log info</A>

/ Last changed on Tue Jun  2 20:21:57 1998 by
<A HREF="mailto:MHammond@skippinet.com.au">Mark Hammond</A>
<P>

<HR>
<H2><A NAME="7.3">7.3. Is there an OS/2 version of Python?</A></H2>
Yes, see <A HREF="http://www.python.org/download/download_os2.html">http://www.python.org/download/download_os2.html</A>.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.003.htp">Log info</A>

/ Last changed on Tue Sep  7 11:33:16 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="7.4">7.4. Is there a VMS version of Python?</A></H2>
Jean-Franois Pironne has ported 2.1.3 to OpenVMS.  It can be found at
&lt;<A HREF="http://vmspython.dyndns.org">http://vmspython.dyndns.org</A>/&gt;.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.004.htp">Log info</A>

/ Last changed on Thu Sep 19 15:40:38 2002 by
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
<P>

<HR>
<H2><A NAME="7.5">7.5. What about IBM mainframes, or other non-UNIX platforms?</A></H2>
I haven't heard about these, except I remember hearing about an
OS/9 port and a port to Vxworks (both operating systems for embedded
systems).  If you're interested in any of this, go directly to the
newsgroup and ask there, you may find exactly what you need.  For
example, a port to MPE/iX 5.0 on HP3000 computers was just announced,
see <A HREF="http://www.allegro.com/software">http://www.allegro.com/software</A>/.
<P>
On the IBM mainframe side, for Z/OS there's a port of python 1.4 that goes with their open-unix package, formely OpenEdition MVS, (<A HREF="http://www-1.ibm.com/servers/eserver/zseries/zos/unix/python.html">http://www-1.ibm.com/servers/eserver/zseries/zos/unix/python.html</A>). On a side note, there's also a java vm ported - so, in theory, jython could run too.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.005.htp">Log info</A>

/ Last changed on Mon Nov 18 03:18:39 2002 by
<A HREF="mailto:bjessen@uol.com.br">Bruno Jessen</A>
<P>

<HR>
<H2><A NAME="7.6">7.6. Where are the source or Makefiles for the non-UNIX versions?</A></H2>
The standard sources can (almost) be used.  Additional sources can
be found in the platform-specific subdirectories of the distribution.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.006.htp">Log info</A>
<P>

<HR>
<H2><A NAME="7.7">7.7. What is the status and support for the non-UNIX versions?</A></H2>
I don't have access to most of these platforms, so in general I am
dependent on material submitted by volunteers.  However I strive to
integrate all changes needed to get it to compile on a particular
platform back into the standard sources, so porting of the next
version to the various non-UNIX platforms should be easy.
(Note that Linux is classified as a UNIX platform here. :-)
<P>
Some specific platforms:
<P>
Windows: all versions (95, 98, ME, NT, 2000, XP) are supported,
all python.org releases come with a Windows installer.
<P>
MacOS: Jack Jansen does an admirable job of keeping the Mac version
up to date (both MacOS X and older versions);
see <A HREF="http://www.cwi.nl/~jack/macpython.html">http://www.cwi.nl/~jack/macpython.html</A>
<P>
For all supported platforms, see <A HREF="http://www.python.org/download">http://www.python.org/download</A>/
(follow the link to "Other platforms" for less common platforms)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.007.htp">Log info</A>

/ Last changed on Fri May 24 21:34:24 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="7.8">7.8. I have a PC version but it appears to be only a binary. Where's the library?</A></H2>
If you are running any version of Windows, then you have the wrong distribution.  The FAQ lists current Windows versions.  Notably, Pythonwin and wpy provide fully functional installations.
<P>
But if you are sure you have the only distribution with a hope of working on
your system, then...
<P>
You still need to copy the files from the distribution directory
"python/Lib" to your system.  If you don't have the full distribution,
you can get the file lib&lt;version&gt;.tar.gz from most ftp sites carrying
Python; this is a subset of the distribution containing just those
files, e.g.  <A HREF="ftp://ftp.python.org/pub/python/src/lib1.4.tar.gz">ftp://ftp.python.org/pub/python/src/lib1.4.tar.gz</A>.
<P>
Once you have installed the library, you need to point sys.path to it.
Assuming the library is in C:\misc\python\lib, the following commands
will point your Python interpreter to it (note the doubled backslashes
-- you can also use single forward slashes instead):
<P>
<PRE>
        >>> import sys
        >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
        >>>
</PRE>
For a more permanent effect, set the environment variable PYTHONPATH,
as follows (talking to a DOS prompt):
<P>
<PRE>
        C> SET PYTHONPATH=C:\misc\python\lib
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.008.htp">Log info</A>

/ Last changed on Fri May 23 16:28:27 1997 by
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
<P>

<HR>
<H2><A NAME="7.9">7.9. Where's the documentation for the Mac or PC version?</A></H2>
The documentation for the Unix version also applies to the Mac and
PC versions.  Where applicable, differences are indicated in the text.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.009.htp">Log info</A>
<P>

<HR>
<H2><A NAME="7.10">7.10. How do I create a Python program file on the Mac or PC?</A></H2>
Use an external editor.  On the Mac, BBEdit seems to be a popular
no-frills text editor.  I work like this: start the interpreter; edit
a module file using BBedit; import and test it in the interpreter;
edit again in BBedit; then use the built-in function reload() to
re-read the imported module; etc. In the 1.4 distribution 
you will find a BBEdit extension that makes life a little easier: 
it can tell the interpreter to execute the current window. 
See :Mac:Tools:BBPy:README. 
<P>
Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
anyone with a pulse could certainly figure out how to do the same on
MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
Not only can you easily resave and "reload()" from Python after making
changes, but since WinNot auto-copies to the clipboard any text you
select, you can simply select the entire procedure (function) which
you changed in WinNot, switch to QWPython, and shift-ins to reenter
the changed program unit."
<P>
If you're using Windows95 or Windows NT, you should also know about
PythonWin, which provides a GUI framework, with an mouse-driven
editor, an object browser, and a GUI-based debugger.  See
<PRE>
       <A HREF="http://www.python.org/ftp/python/pythonwin">http://www.python.org/ftp/python/pythonwin</A>/
</PRE>
for details.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.010.htp">Log info</A>

/ Last changed on Sun May 25 10:04:25 1997 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="7.11">7.11. How can I use Tkinter on Windows 95/NT?</A></H2>
Starting from Python 1.5, it's very easy -- just download and install
Python and Tcl/Tk and you're in business.  See 
<P>
<PRE>
  <A HREF="http://www.python.org/download/download_windows.html">http://www.python.org/download/download_windows.html</A>
</PRE>
One warning: don't attempt to use Tkinter from PythonWin
(Mark Hammond's IDE).  Use it from the command line interface
(python.exe) or the windowless interpreter (pythonw.exe).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.011.htp">Log info</A>

/ Last changed on Fri Jun 12 09:32:48 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="7.12">7.12. cgi.py (or other CGI programming) doesn't work sometimes on NT or win95!</A></H2>
Be sure you have the latest python.exe, that you are using
python.exe rather than a GUI version of python and that you
have configured the server to execute
<P>
<PRE>
     "...\python.exe -u ..."
</PRE>
for the cgi execution.  The -u (unbuffered) option on NT and
win95 prevents the interpreter from altering newlines in the
standard input and output.  Without it post/multipart requests
will seem to have the wrong length and binary (eg, GIF)
responses may get garbled (resulting in, eg, a "broken image").
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.012.htp">Log info</A>

/ Last changed on Wed Jul 30 10:48:02 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="7.13">7.13. Why doesn't os.popen() work in PythonWin on NT?</A></H2>
The reason that os.popen() doesn't work from within PythonWin is due to a bug in Microsoft's C Runtime Library (CRT). The CRT assumes you  have a Win32 console attached to the process.
<P>
You should use the win32pipe module's popen() instead which doesn't depend on having an attached Win32 console.
<P>
Example:
<PRE>
 import win32pipe
 f = win32pipe.popen('dir /c c:\\')
 print f.readlines()
 f.close()
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.013.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.013.htp">Log info</A>

/ Last changed on Thu Jul 31 15:34:09 1997 by
<A HREF="mailto:billtut@microsoft.com">Bill Tutt</A>
<P>

<HR>
<H2><A NAME="7.14">7.14. How do I use different functionality on different platforms with the same program?</A></H2>
Remember that Python is extremely dynamic and that you
can use this dynamism to configure a program at run-time to
use available functionality on different platforms.  For example
you can test the sys.platform and import different modules based
on its value.
<P>
<PRE>
   import sys
   if sys.platform == "win32":
      import win32pipe
      popen = win32pipe.popen
   else:
      import os
      popen = os.popen
</PRE>
(See FAQ 7.13 for an explanation of why you might want to
do something like this.)  Also you can try to import a module
and use a fallback if the import fails:
<P>
<PRE>
    try:
         import really_fast_implementation
         choice = really_fast_implementation
    except ImportError:
         import slower_implementation
         choice = slower_implementation
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.014.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.014.htp">Log info</A>

/ Last changed on Wed Aug 13 07:39:06 1997 by
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
<P>

<HR>
<H2><A NAME="7.15">7.15. Is there an Amiga version of Python?</A></H2>
Yes. See the AmigaPython homepage at <A HREF="http://www.bigfoot.com/~irmen/python.html">http://www.bigfoot.com/~irmen/python.html</A>.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.015.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.015.htp">Log info</A>

/ Last changed on Mon Dec 14 06:53:32 1998 by
<A HREF="mailto:irmen@bigfoot.com">Irmen de Jong</A>
<P>

<HR>
<H2><A NAME="7.16">7.16. Why doesn't os.popen()/win32pipe.popen() work on Win9x?</A></H2>
There is a bug in Win9x that prevents os.popen/win32pipe.popen* from working. The good news is there is a way to work around this problem.
The Microsoft Knowledge Base article that you need to lookup is: Q150956. You will find links to the knowledge base at:
<A HREF="http://www.microsoft.com/kb">http://www.microsoft.com/kb</A>.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq07.016.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq07.016.htp">Log info</A>

/ Last changed on Fri Jun 25 10:45:38 1999 by
<A HREF="mailto:billtut@microsoft.com">Bill Tutt</A>
<P>

<HR>
<H1>8. Python on Windows</H1>

<HR>
<H2><A NAME="8.1">8.1. Using Python for CGI on Microsoft Windows</A></H2>
** Setting up the Microsoft IIS Server/Peer Server
<P>
On the Microsoft IIS 
server or on the Win95 MS Personal Web Server
you set up python in the same way that you 
would set up any other scripting engine. 
<P>
Run regedt32 and go to: 
<P>
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\ScriptMap 
<P>
and enter the following line (making any specific changes that your system may need) 
<P>
.py :REG_SZ: c:\&lt;path to python&gt;\python.exe -u %s %s 
<P>
This line will allow you to call your script with a simple reference like:
http://yourserver/scripts/yourscript.py 
provided "scripts" is an "executable" directory for your server (which
it usually is by default).
The "-u" flag specifies unbuffered and binary mode for stdin - needed when working with binary data
<P>
In addition, it is recommended by people who would know that using ".py" may 
not be a good idea for the file extensions when used in this context
(you might want to reserve *.py for support modules and use *.cgi or *.cgp
for "main program" scripts).
However, that issue is beyond this Windows FAQ entry.
<P>
<P>
** Apache configuration
<P>
In the Apache configuration file httpd.conf, add the following line at 
the end of the file:
<P>
ScriptInterpreterSource Registry
<P>
Then, give your Python CGI-scripts the extension .py and put them in the cgi-bin directory.
<P>
<P>
** Netscape Servers:
Information on this topic exists at:
<A HREF="http://home.netscape.com/comprod/server_central/support/fasttrack_man/programs.htm#1010870">http://home.netscape.com/comprod/server_central/support/fasttrack_man/programs.htm#1010870</A>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.001.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.001.htp">Log info</A>

/ Last changed on Wed Mar 27 12:25:54 2002 by
<A HREF="mailto:gerhard@bigfoot.de">Gerhard Hring</A>
<P>

<HR>
<H2><A NAME="8.2">8.2. How to check for a keypress without blocking?</A></H2>
Use the msvcrt module.  This is a standard Windows-specific extensions
in Python 1.5 and beyond.  It defines a function kbhit() which checks
whether a keyboard hit is present; also getch() which gets one
character without echo.  Plus a few other goodies.
<P>
(Search for "keypress" to find an answer for Unix as well.)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.002.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.002.htp">Log info</A>

/ Last changed on Mon Mar 30 16:21:46 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.3">8.3. $PYTHONPATH</A></H2>
In MS-DOS derived environments, a unix variable such as $PYTHONPATH is 
set as PYTHONPATH, without the dollar sign.  PYTHONPATH is useful for
specifying the location of library files.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.003.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.003.htp">Log info</A>

/ Last changed on Thu Jun 11 00:41:26 1998 by
<A HREF="mailto:guido@python.org">Gvr</A>
<P>

<HR>
<H2><A NAME="8.4">8.4. dedent syntax errors</A></H2>
The FAQ does not recommend using tabs, and Guido's Python Style Guide recommends 4 spaces for distributed Python code; this is also the Emacs python-mode default; see
<P>
<PRE>
    <A HREF="http://www.python.org/doc/essays/styleguide.html">http://www.python.org/doc/essays/styleguide.html</A>
</PRE>
Under any editor mixing tabs and spaces is a bad idea.  MSVC is no different in this respect, and is easily configured to use spaces:  Take Tools -> Options -> Tabs, and for file type "Default" set "Tab size" and "Indent size" to 4, and select the "Insert spaces" radio button.
<P>
If you suspect mixed tabs and spaces are causing problems in leading whitespace, run Python with the -t switch or, run Tools/Scripts/tabnanny.py to check a directory tree in batch mode.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.004.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.004.htp">Log info</A>

/ Last changed on Mon Feb 12 15:04:14 2001 by
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
<P>

<HR>
<H2><A NAME="8.5">8.5. How do I emulate os.kill() in Windows?</A></H2>
Use win32api:
<P>
<PRE>
    def kill(pid):
        """kill function for Win32"""
        import win32api
        handle = win32api.OpenProcess(1, 0, pid)
        return (0 != win32api.TerminateProcess(handle, 0))
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.005.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.005.htp">Log info</A>

/ Last changed on Sat Aug  8 18:55:06 1998 by
<A HREF="mailto:jeffbauer@bigfoot.com">Jeff Bauer</A>
<P>

<HR>
<H2><A NAME="8.6">8.6. Why does os.path.isdir() fail on NT shared directories?</A></H2>
The solution appears to be always append the "\\" on 
the end of shared drives.
<P>
<PRE>
  >>> import os
  >>> os.path.isdir( '\\\\rorschach\\public')
  0
  >>> os.path.isdir( '\\\\rorschach\\public\\')
  1
</PRE>
[Blake Winton responds:]
I've had the same problem doing "Start >> Run" and then a
directory on a shared drive.  If I use "\\rorschach\public", 
it will fail, but if I use "\\rorschach\public\", it will 
work.  For that matter, os.stat() does the same thing (well, 
it gives an error for "\\\\rorschach\\public", but you get 
the idea)...
<P>
I've got a theory about why this happens, but it's only 
a theory.  NT knows the difference between shared directories, 
and regular directories.  "\\rorschach\public" isn't a 
directory, it's _really_ an IPC abstraction.  This is sort 
of lended credence to by the fact that when you're mapping 
a network drive, you can't map "\\rorschach\public\utils", 
but only "\\rorschach\public".
<P>
[Clarification by <A HREF="mailto:funkster@midwinter.com">funkster@midwinter.com</A>]
It's not actually a Python 
question, as Python is working just fine; it's clearing up something 
a bit muddled about Windows networked drives.
<P>
It helps to think of share points as being like drive letters.
Example:
<PRE>
        k: is not a directory
        k:\ is a directory
        k:\media is a directory
        k:\media\ is not a directory
</PRE>
The same rules apply if you substitute "k:" with "\\conky\foo":
<PRE>
        \\conky\foo  is not a directory
        \\conky\foo\ is a directory
        \\conky\foo\media is a directory
        \\conky\foo\media\ is not a directory
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.006.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.006.htp">Log info</A>

/ Last changed on Sun Jan 31 08:44:48 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.7">8.7. PyRun_SimpleFile() crashes on Windows but not on Unix</A></H2>
I've seen a number of reports of PyRun_SimpleFile() failing
in a Windows port of an application embedding Python that worked
fine on Unix.  PyRun_SimpleString() works fine on both platforms.
<P>
I think this happens because the application was compiled with a 
different set of compiler flags than Python15.DLL.  It seems that some 
compiler flags affect the standard I/O library in such a way that 
using different flags makes calls fail.  You need to set it for 
the non-debug multi-threaded DLL (/MD on the command line, or can be set via MSVC under Project Settings-&gt;C++/Code Generation then the "Use rum-time library" dropdown.)
<P>
Also note that you can not mix-and-match Debug and Release versions.  If you wish to use the Debug Multithreaded DLL, then your module _must_ have an "_d" appended to the base name.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.007.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.007.htp">Log info</A>

/ Last changed on Wed Nov 17 17:37:07 1999 by
<A HREF="mailto:mhammond@skippinet.com.au">Mark Hammond</A>
<P>

<HR>
<H2><A NAME="8.8">8.8. Import of _tkinter fails on Windows 95/98</A></H2>
Sometimes, the import of _tkinter fails on Windows 95 or 98,
complaining with a message like the following:
<P>
<PRE>
  ImportError: DLL load failed: One of the library files needed
  to run this application cannot be found.
</PRE>
It could be that you haven't installed Tcl/Tk, but if you did
install Tcl/Tk, and the Wish application works correctly,
the problem may be that its installer didn't
manage to edit the autoexec.bat file correctly.  It tries to add a
statement that changes the PATH environment variable to include
the Tcl/Tk 'bin' subdirectory, but sometimes this edit doesn't
quite work.  Opening it with notepad usually reveals what the
problem is.
<P>
(One additional hint, noted by David Szafranski: you can't use
long filenames here; e.g. use C:\PROGRA~1\Tcl\bin instead of
C:\Program Files\Tcl\bin.)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.008.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.008.htp">Log info</A>

/ Last changed on Wed Dec  2 22:32:41 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.9">8.9. Can't extract the downloaded documentation on Windows</A></H2>
Sometimes, when you download the documentation package to a Windows
machine using a web browser, the file extension of the saved file
ends up being .EXE.  This is a mistake; the extension should be .TGZ.
<P>
Simply rename the downloaded file to have the .TGZ extension, and
WinZip will be able to handle it.  (If your copy of WinZip doesn't,
get a newer one from <A HREF="http://www.winzip.com">http://www.winzip.com</A>.)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.009.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.009.htp">Log info</A>

/ Last changed on Sat Nov 21 13:41:35 1998 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.10">8.10. Can't get Py_RunSimpleFile() to work.</A></H2>
This is very sensitive to the compiler vendor, version and (perhaps)
even options.  If the FILE* structure in your embedding program isn't
the same as is assumed by the Python interpreter it won't work.
<P>
The Python 1.5.* DLLs (python15.dll) are all compiled
with MS VC++ 5.0 and with multithreading-DLL options (/MD, I think).
<P>
If you can't change compilers or flags, try using Py_RunSimpleString().
A trick to get it to run an arbitrary file is to construct a call to
execfile() with the name of your file as argument.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.010.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.010.htp">Log info</A>

/ Last changed on Wed Jan 13 10:58:14 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.11">8.11. Where is Freeze for Windows?</A></H2>
("Freeze" is a program that allows you to ship a Python program
as a single stand-alone executable file.  It is <I>not</I> a compiler,
your programs don't run any faster, but they are more easily
distributable (to platforms with the same OS and CPU).  Read the
README file of the freeze program for more disclaimers.)
<P>
You can use freeze on Windows, but you must download the source
tree (see <A HREF="http://www.python.org/download/download_source.html">http://www.python.org/download/download_source.html</A>).
This is recommended for Python 1.5.2 (and betas thereof) only;
older versions don't quite work.
<P>
You need the Microsoft VC++ 5.0 compiler (maybe it works with
6.0 too).  You probably need to build Python -- the project files
are all in the PCbuild directory.
<P>
The freeze program is in the Tools\freeze subdirectory of the source
tree.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.011.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.011.htp">Log info</A>

/ Last changed on Wed Feb 17 18:47:24 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.12">8.12. Is a *.pyd file the same as a DLL?</A></H2>
Yes, .pyd files are dll's.  But there are a few differences.  If you
have a DLL named foo.pyd, then it must have a function initfoo().  You
can then write Python "import foo", and Python will search for foo.pyd
(as well as foo.py, foo.pyc) and if it finds it, will attempt to call
initfoo() to initialize it.  You do not link your .exe with foo.lib,
as that would cause Windows to require the DLL to be present.
<P>
Note that the search path for foo.pyd is PYTHONPATH, not the same as
the path that Windows uses to search for foo.dll.  Also, foo.pyd need
not be present to run your program, whereas if you linked your program
with a dll, the dll is required.  Of course, foo.pyd is required if
you want to say "import foo".  In a dll, linkage is declared in the
source code with __declspec(dllexport).  In a .pyd, linkage is defined
in a list of available functions.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.012.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.012.htp">Log info</A>

/ Last changed on Tue Nov 23 02:40:08 1999 by
<A HREF="mailto:jquinn+psa@cs.oberlin.edu">Jameson Quinn</A>
<P>

<HR>
<H2><A NAME="8.13">8.13. Missing cw3215mt.dll (or missing cw3215.dll)</A></H2>
Sometimes, when using Tkinter on Windows, you get an error that
cw3215mt.dll or cw3215.dll is missing.
<P>
Cause: you have an old Tcl/Tk DLL built with cygwin in your path
(probably C:\Windows).  You must use the Tcl/Tk DLLs from the
standard Tcl/Tk installation (Python 1.5.2 comes with one).
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.013.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.013.htp">Log info</A>

/ Last changed on Fri Jun 11 00:54:13 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.14">8.14. How to make python scripts executable:</A></H2>
[Blake Coverett]
<P>
Win2K:
<P>
The standard installer already associates the .py extension with a file type
(Python.File) and gives that file type an open command that runs the
interpreter (D:\Program Files\Python\python.exe "%1" %*).  This is enough to
make scripts executable from the command prompt as 'foo.py'.  If you'd
rather be able to execute the script by simple typing 'foo' with no
extension you need to add .py to the PATHEXT environment variable.
<P>
WinNT:
<P>
The steps taken by the installed as described above allow you do run a
script with 'foo.py', but a long time bug in the NT command processor
prevents you from redirecting the input or output of any script executed in
this way.  This is often important.
<P>
An appropriate incantation for making a Python script executable under WinNT
is to give the file an extension of .cmd and add the following as the first
line:
<P>
<PRE>
    @setlocal enableextensions &amp; python -x %~f0 %* &amp; goto :EOF
</PRE>
Win9x:
<P>
[Due to Bruce Eckel]
<P>
<PRE>
  @echo off
  rem = """
  rem run python on this bat file. Needs the full path where
  rem you keep your python files. The -x causes python to skip
  rem the first line of the file:
  python -x c:\aaa\Python\\"%0".bat %1 %2 %3 %4 %5 %6 %7 %8 %9
  goto endofpython
  rem """
</PRE>
<PRE>
  # The python program goes here:
</PRE>
<PRE>
  print "hello, Python"
</PRE>
<PRE>
  # For the end of the batch file:
  rem = """
  :endofpython
  rem """
</PRE>
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.014.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.014.htp">Log info</A>

/ Last changed on Tue Nov 30 10:25:17 1999 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.15">8.15. Warning about CTL3D32 version from installer</A></H2>
The Python installer issues a warning like this:
<P>
<PRE>
  This version uses CTL3D32.DLL whitch is not the correct version.
  This version is used for windows NT applications only.
</PRE>
[Tim Peters]
This is a Microsoft DLL, and a notorious
source of problems.  The msg means what it says:  you have the wrong version
of this DLL for your operating system.  The Python installation did not
cause this -- something else you installed previous to this overwrote the
DLL that came with your OS (probably older shareware of some sort, but
there's no way to tell now).  If you search for "CTL3D32" using any search
engine (AltaVista, for example), you'll find hundreds and hundreds of web
pages complaining about the same problem with all sorts of installation
programs.  They'll point you to ways to get the correct version reinstalled
on your system (since Python doesn't cause this, we can't fix it).
<P>
David A Burton has written a little program to fix this.  Go to
<A HREF="http://www.burtonsys.com/download.html">http://www.burtonsys.com/download.html</A> and click on "ctl3dfix.zip"
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.015.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.015.htp">Log info</A>

/ Last changed on Thu Oct 26 15:42:00 2000 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<H2><A NAME="8.16">8.16. How can I embed Python into a Windows application?</A></H2>
Edward K. Ream &lt;<A HREF="mailto:edream@tds.net">edream@tds.net</A>&gt; writes
<P>
When '##' appears in a file name below, it is an abbreviated version number.  For example, for Python 2.1.1, ## will be replaced by 21.
<P>
Embedding the Python interpreter in a Windows app can be summarized as
follows:
<P>
1. Do _not_ build Python into your .exe file directly.  On Windows,
Python must be a DLL to handle importing modules that are themselves
DLL's.  (This is the first key undocumented fact.) Instead, link to
python##.dll; it is typically installed in c:\Windows\System.  
<P>
You can link to Python statically or dynamically.  Linking statically
means linking against python##.lib The drawback is that your app won't
run if python##.dll does not exist on your system.
<P>
General note: python##.lib is the so-called "import lib" corresponding
to python.dll.  It merely defines symbols for the linker.
<P>
Borland note: convert python##.lib to OMF format using Coff2Omf.exe
first.
<P>
Linking dynamically greatly simplifies link options; everything happens
at run time.  Your code must load python##.dll using the Windows
LoadLibraryEx() routine.  The code must also use access routines and
data in python##.dll (that is, Python's C API's) using pointers
obtained by the Windows GetProcAddress() routine.  Macros can make
using these pointers transparent to any C code that calls routines in
Python's C API.
<P>
2. If you use SWIG, it is easy to create a Python "extension module"
that will make the app's data and methods available to Python.  SWIG
will handle just about all the grungy details for you.  The result is C
code that you link _into your .exe file_ (!)  You do _not_ have to
create a DLL file, and this also simplifies linking.
<P>
3.  SWIG will create an init function (a C function) whose name depends
on the name of the extension module.  For example, if the name of the
module is leo, the init function will be called initleo().  If you use
SWIG shadow classes, as you should, the init function will be called
initleoc().  This initializes a mostly hidden helper class used by the
shadow class.
<P>
The reason you can link the C code in step 2 into your .exe file is that
calling the initialization function is equivalent to importing the
module into Python! (This is the second key undocumented fact.)
<P>
4. In short, you can use the following code to initialize the Python
interpreter with your extension module.
<P>
<PRE>
    #include "python.h"
    ...
    Py_Initialize();  // Initialize Python.
    initmyAppc();  // Initialize (import) the helper class. 
    PyRun_SimpleString("import myApp") ;  // Import the shadow class.
</PRE>
5. There are two problems with Python's C API which will become apparent
if you use a compiler other than MSVC, the compiler used to build
python##.dll.
<P>
Problem 1: The so-called "Very High Level" functions that take FILE *
arguments will not work in a multi-compiler environment; each compiler's
notion of a struct FILE will be different.  From an implementation
standpoint these are very _low_ level functions.
<P>
Problem 2: SWIG generates the following code when generating wrappers to
void functions:
<P>
<PRE>
    Py_INCREF(Py_None);
    _resultobj = Py_None;
    return _resultobj;
</PRE>
Alas, Py_None is a macro that expands to a reference to a complex data
structure called _Py_NoneStruct inside python##.dll.  Again, this code
will fail in a mult-compiler environment.  Replace such code by:
<P>
<PRE>
    return Py_BuildValue("");
</PRE>
It may be possible to use SWIG's %typemap command to make the change
automatically, though I have not been able to get this to work (I'm a
complete SWIG newbie).
<P>
6. Using a Python shell script to put up a Python interpreter window
from inside your Windows app is not a good idea; the resulting window
will be independent of your app's windowing system.  Rather, you (or the
wxPythonWindow class) should create a "native" interpreter window.  It
is easy to connect that window to the Python interpreter.  You can
redirect Python's i/o to _any_ object that supports read and write, so
all you need is a Python object (defined in your extension module) that
contains read() and write() methods.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.016.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.016.htp">Log info</A>

/ Last changed on Thu Jan 31 16:29:34 2002 by
<A HREF="mailto:vkryukov@ufg.com">Victor Kryukov</A>
<P>

<HR>
<H2><A NAME="8.17">8.17. Setting up IIS 5 to use Python for CGI</A></H2>
In order to set up Internet Information Services 5 to use Python for CGI processing, please see the following links:
<P>
<A HREF="http://www.e-coli.net/pyiis_server.html">http://www.e-coli.net/pyiis_server.html</A> (for Win2k Server)
<A HREF="http://www.e-coli.net/pyiis.html">http://www.e-coli.net/pyiis.html</A> (for Win2k pro)
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.017.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.017.htp">Log info</A>

/ Last changed on Fri Mar 22 22:05:51 2002 by
<A HREF="mailto:dsavitsk@e-coli.net">douglas savitsky</A>
<P>

<HR>
<H2><A NAME="8.18">8.18. How do I run a Python program under Windows?</A></H2>
This is not necessarily quite the straightforward question it appears
to be. If you are already familiar with running programs from the
Windows command line then everything will seem really easy and
obvious. If your computer experience is limited then you might need a
little more guidance. Also there are differences between Windows 95,
98, NT, ME, 2000 and XP which can add to the confusion. You might
think of this as "why I pay software support charges" if you have a
helpful and friendly administrator to help you set things up without
having to understand all this yourself. If so, then great! Show them
this page and it should be a done deal.
<P>
Unless you use some sort of integrated development environment (such
as PythonWin or IDLE, to name only two in a growing family) then you
will end up <I>typing</I> Windows commands into what is variously referred
to as a "DOS window" or "Command prompt window". Usually you can
create such a window from your Start menu (under Windows 2000 I use
"Start | Programs | Accessories | Command Prompt").  You should be
able to recognize when you have started such a window because you will
see a Windows "command prompt", which usually looks like this:
<P>
<PRE>
    C:\>
</PRE>
The letter may be different, and there might be other things after it,
so you might just as easily see something like:
<P>
<PRE>
    D:\Steve\Projects\Python>
</PRE>
depending on how your computer has been set up and what else you have
recently done with it. Once you have started such a window, you are
well on the way to running Python programs.
<P>
You need to realize that your Python scripts have to be processed by
another program, usually called the "Python interpreter". The
interpreter reads your script, "compiles" it into "Python bytecodes"
(which are instructions for an imaginary computer known as the "Python
Virtual Machine") and then executes the bytecodes to run your
program. So, how do you arrange for the interpreter to handle your
Python?
<P>
First, you need to make sure that your command window recognises the
word "python" as an instruction to start the interpreter. If you have
opened a command window, you should try entering the command:
<P>
<PRE>
    python
</PRE>
and hitting return. If you then see something like:
<P>
<PRE>
    Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
</PRE>
then this part of the job has been correctly managed during Python's
installation process, and you have started the interpreter in
"interactive mode". That means you can enter Python statements or
expressions interactively and have them executed or evaluated while
you wait. This is one of Python's strongest features, but it takes a
little getting used to. Check it by entering a few expressions of your
choice and seeing the results...
<P>
<PRE>
    >>> print "Hello"
    Hello
    >>> "Hello" * 3
    HelloHelloHello
</PRE>
When you want to end your interactive Python session, enter a
terminator (hold the Ctrl key down while you enter a Z, then hit the
"Enter" key) to get back to your Windows command prompt.  You may also
find that you have a Start-menu entry such as "Start | Programs |
Python 2.2 | Python (command line)" that results in you seeing the
">>>" prompt in a new window. If so, the window will disappear after
you enter the terminator -- Windows runs a single "python" command in
the window, which terminates when you terminate the interpreter.
<P>
If the "python" command, instead of displaying the interpreter prompt ">>>", gives you a message like
<P>
<PRE>
    'python' is not recognized as an internal or external command,
    operable program or batch file.
</PRE>
or
<P>
<PRE>
    Bad command or filename
</PRE>
then you need to make sure that your computer knows where to find the
Python interpreter. To do this you will have to modify a setting
called the PATH, which is a just list of directories where Windows
will look for programs. Rather than just enter the right command every
time you create a command window, you should arrange for Python's
installation directory to be added to the PATH of every command window
as it starts. If you installed Python fairly recently then the command
<P>
<PRE>
    dir C:\py*
</PRE>
will probably tell you where it is installed. Alternatively, perhaps
you made a note. Otherwise you will be reduced to a search of your
whole disk ... break out the Windows explorer and use "Tools | Find"
or hit the "Search" button and look for "python.exe". Suppose you
discover that Python is installed in the C:\Python22 directory (the
default at the time of writing) then you should make sure that
entering the command
<P>
<PRE>
    c:\Python22\python
</PRE>
starts up the interpreter as above (and don't forget you'll need a
"CTRL-Z" and an "Enter" to get out of it). Once you have verified the
directory, you need to add it to the start-up routines your computer
goes through.  For older versions of Windows the easiest way to do
this is to edit the C:\AUTOEXEC.BAT file. You would want to add a line
like the following to AUTOEXEC.BAT:
<P>
<PRE>
    PATH C:\Python22;%PATH%
</PRE>
For Windows NT, 2000 and (I assume) XP, you will need to add a string
such as
<P>
<PRE>
    ;C:\Python22
</PRE>
to the current setting for the PATH environment variable, which you
will find in the properties window of "My Computer" under the
"Advanced" tab. Note that if you have sufficient privilege you might
get a choice of installing the settings either for the Current User or
for System. The latter is preferred if you want everybody to be able
to run Python on the machine.
<P>
If you aren't confident doing any of these manipulations yourself, ask
for help!  At this stage you may or may not want to reboot your system
to make absolutely sure the new setting has "taken" (don't you love
the way Windows gives you these freqeuent coffee breaks). You probably
won't need to for Windows NT, XP or 2000. You can also avoid it in
earlier versions by editing the file C:\WINDOWS\COMMAND\CMDINIT.BAT
instead of AUTOEXEC.BAT.
<P>
You should now be able to start a new command window, enter
<P>
<PRE>
    python
</PRE>
at the "C:>" (or whatever) prompt, and see the ">>>" prompt that
indicates the Python interpreter is reading interactive commands.
<P>
Let's suppose you have a program called "pytest.py" in directory
"C:\Steve\Projects\Python". A session to run that program might look
like this:
<P>
<PRE>
    C:\> cd \Steve\Projects\Python
    C:\Steve\Projects\Python> python pytest.py
</PRE>
Because you added a file name to the command to start the interpreter,
when it starts up it reads the Python script in the named file,
compiles it, executes it, and terminates (so you see another "C:\>"
prompt).  You might also have entered
<P>
<PRE>
    C:\> python \Steve\Projects\Python\pytest.py
</PRE>
if you hadn't wanted to change your current directory.
<P>
Under NT, 2000 and XP you may well find that the installation process
has also arranged that the command
<P>
<PRE>
    pytest.py
</PRE>
(or, if the file isn't in the current directory)
<P>
<PRE>
    C:\Steve\Projects\Python\pytest.py
</PRE>
will automatically recognize the ".py" extension and run the Python
interpreter on the named file. Using this feature is fine, but <I>some</I>
versions of Windows have bugs which mean that this form isn't exactly
equivalent to using the interpreter explicitly, so be careful. Easier
to remember, for now, that
<P>
<PRE>
    python C:\Steve\Projects\Python\pytest.py
</PRE>
works pretty close to the same, and redirection will work (more)
reliably.
<P>
The important things to remember are:
<P>
1. Start Python from the Start Menu, or make sure the PATH is set
correctly so Windows can find the Python interpreter.
<P>
<PRE>
    python
</PRE>
should give you a '>>>" prompt from the Python interpreter. Don't
forget the CTRL-Z and ENTER to terminate the interpreter (and, if you
started the window from the Start Menu, make the window disappear).
<P>
2. Once this works, you run programs with commands:
<P>
<PRE>
    python {program-file}
</PRE>
3. When you know the commands to use you can build Windows shortcuts
to run the Python interpreter on any of your scripts, naming
particular working directories, and adding them to your menus, but
that's another lessFAQ. Take a look at
<P>
<PRE>
    python --help
</PRE>
if your needs are complex.
<P>
4. Interactive mode (where you see the ">>>" prompt) is best used
<I>not</I> for running programs, which are better executed as in steps 2
and 3, but for checking that individual statements and expressions do
what you think they will, and for developing code by experiment.
<P>

<A HREF="faqw.py?req=edit&amp;file=faq08.018.htp">Edit this entry</A> /
<A HREF="faqw.py?req=log&amp;file=faq08.018.htp">Log info</A>

/ Last changed on Tue Aug 20 16:19:53 2002 by
<A HREF="mailto:guido@python.org">GvR</A>
<P>

<HR>
<A HREF="http://www.python.org">Python home</A> /
<A HREF="faqw.py?req=home">Python FAQ Wizard 1.0.3</A> /
Feedback to <A HREF="mailto:guido@python.org">GvR</A>
<P><A HREF="http://www.python.org/"><IMG
SRC="http://www.python.org/pics/PythonPowered.gif" 
ALIGN=top WIDTH=110 HEIGHT=44 ALT="Python Powered" BORDER=0></A><BR>
</BODY>
</HTML>