1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
|
# Autogenerated by Sphinx on Tue May 6 18:33:44 2025
# as part of the release process.
topics = {
'assert': r'''The "assert" statement
**********************
Assert statements are a convenient way to insert debugging assertions
into a program:
assert_stmt: "assert" expression ["," expression]
The simple form, "assert expression", is equivalent to
if __debug__:
if not expression: raise AssertionError
The extended form, "assert expression1, expression2", is equivalent to
if __debug__:
if not expression1: raise AssertionError(expression2)
These equivalences assume that "__debug__" and "AssertionError" refer
to the built-in variables with those names. In the current
implementation, the built-in variable "__debug__" is "True" under
normal circumstances, "False" when optimization is requested (command
line option "-O"). The current code generator emits no code for an
"assert" statement when optimization is requested at compile time.
Note that it is unnecessary to include the source code for the
expression that failed in the error message; it will be displayed as
part of the stack trace.
Assignments to "__debug__" are illegal. The value for the built-in
variable is determined when the interpreter starts.
''',
'assignment': r'''Assignment statements
*********************
Assignment statements are used to (re)bind names to values and to
modify attributes or items of mutable objects:
assignment_stmt: (target_list "=")+ (starred_expression | yield_expression)
target_list: target ("," target)* [","]
target: identifier
| "(" [target_list] ")"
| "[" [target_list] "]"
| attributeref
| subscription
| slicing
| "*" target
(See section Primaries for the syntax definitions for *attributeref*,
*subscription*, and *slicing*.)
An assignment statement evaluates the expression list (remember that
this can be a single expression or a comma-separated list, the latter
yielding a tuple) and assigns the single resulting object to each of
the target lists, from left to right.
Assignment is defined recursively depending on the form of the target
(list). When a target is part of a mutable object (an attribute
reference, subscription or slicing), the mutable object must
ultimately perform the assignment and decide about its validity, and
may raise an exception if the assignment is unacceptable. The rules
observed by various types and the exceptions raised are given with the
definition of the object types (see section The standard type
hierarchy).
Assignment of an object to a target list, optionally enclosed in
parentheses or square brackets, is recursively defined as follows.
* If the target list is a single target with no trailing comma,
optionally in parentheses, the object is assigned to that target.
* Else:
* If the target list contains one target prefixed with an asterisk,
called a “starred” target: The object must be an iterable with at
least as many items as there are targets in the target list, minus
one. The first items of the iterable are assigned, from left to
right, to the targets before the starred target. The final items
of the iterable are assigned to the targets after the starred
target. A list of the remaining items in the iterable is then
assigned to the starred target (the list can be empty).
* Else: The object must be an iterable with the same number of items
as there are targets in the target list, and the items are
assigned, from left to right, to the corresponding targets.
Assignment of an object to a single target is recursively defined as
follows.
* If the target is an identifier (name):
* If the name does not occur in a "global" or "nonlocal" statement
in the current code block: the name is bound to the object in the
current local namespace.
* Otherwise: the name is bound to the object in the global namespace
or the outer namespace determined by "nonlocal", respectively.
The name is rebound if it was already bound. This may cause the
reference count for the object previously bound to the name to reach
zero, causing the object to be deallocated and its destructor (if it
has one) to be called.
* If the target is an attribute reference: The primary expression in
the reference is evaluated. It should yield an object with
assignable attributes; if this is not the case, "TypeError" is
raised. That object is then asked to assign the assigned object to
the given attribute; if it cannot perform the assignment, it raises
an exception (usually but not necessarily "AttributeError").
Note: If the object is a class instance and the attribute reference
occurs on both sides of the assignment operator, the right-hand side
expression, "a.x" can access either an instance attribute or (if no
instance attribute exists) a class attribute. The left-hand side
target "a.x" is always set as an instance attribute, creating it if
necessary. Thus, the two occurrences of "a.x" do not necessarily
refer to the same attribute: if the right-hand side expression
refers to a class attribute, the left-hand side creates a new
instance attribute as the target of the assignment:
class Cls:
x = 3 # class variable
inst = Cls()
inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3
This description does not necessarily apply to descriptor
attributes, such as properties created with "property()".
* If the target is a subscription: The primary expression in the
reference is evaluated. It should yield either a mutable sequence
object (such as a list) or a mapping object (such as a dictionary).
Next, the subscript expression is evaluated.
If the primary is a mutable sequence object (such as a list), the
subscript must yield an integer. If it is negative, the sequence’s
length is added to it. The resulting value must be a nonnegative
integer less than the sequence’s length, and the sequence is asked
to assign the assigned object to its item with that index. If the
index is out of range, "IndexError" is raised (assignment to a
subscripted sequence cannot add new items to a list).
If the primary is a mapping object (such as a dictionary), the
subscript must have a type compatible with the mapping’s key type,
and the mapping is then asked to create a key/value pair which maps
the subscript to the assigned object. This can either replace an
existing key/value pair with the same key value, or insert a new
key/value pair (if no key with the same value existed).
For user-defined objects, the "__setitem__()" method is called with
appropriate arguments.
* If the target is a slicing: The primary expression in the reference
is evaluated. It should yield a mutable sequence object (such as a
list). The assigned object should be a sequence object of the same
type. Next, the lower and upper bound expressions are evaluated,
insofar they are present; defaults are zero and the sequence’s
length. The bounds should evaluate to integers. If either bound is
negative, the sequence’s length is added to it. The resulting
bounds are clipped to lie between zero and the sequence’s length,
inclusive. Finally, the sequence object is asked to replace the
slice with the items of the assigned sequence. The length of the
slice may be different from the length of the assigned sequence,
thus changing the length of the target sequence, if the target
sequence allows it.
**CPython implementation detail:** In the current implementation, the
syntax for targets is taken to be the same as for expressions, and
invalid syntax is rejected during the code generation phase, causing
less detailed error messages.
Although the definition of assignment implies that overlaps between
the left-hand side and the right-hand side are ‘simultaneous’ (for
example "a, b = b, a" swaps two variables), overlaps *within* the
collection of assigned-to variables occur left-to-right, sometimes
resulting in confusion. For instance, the following program prints
"[0, 2]":
x = [0, 1]
i = 0
i, x[i] = 1, 2 # i is updated, then x[i] is updated
print(x)
See also:
**PEP 3132** - Extended Iterable Unpacking
The specification for the "*target" feature.
Augmented assignment statements
===============================
Augmented assignment is the combination, in a single statement, of a
binary operation and an assignment statement:
augmented_assignment_stmt: augtarget augop (expression_list | yield_expression)
augtarget: identifier | attributeref | subscription | slicing
augop: "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**="
| ">>=" | "<<=" | "&=" | "^=" | "|="
(See section Primaries for the syntax definitions of the last three
symbols.)
An augmented assignment evaluates the target (which, unlike normal
assignment statements, cannot be an unpacking) and the expression
list, performs the binary operation specific to the type of assignment
on the two operands, and assigns the result to the original target.
The target is only evaluated once.
An augmented assignment statement like "x += 1" can be rewritten as "x
= x + 1" to achieve a similar, but not exactly equal effect. In the
augmented version, "x" is only evaluated once. Also, when possible,
the actual operation is performed *in-place*, meaning that rather than
creating a new object and assigning that to the target, the old object
is modified instead.
Unlike normal assignments, augmented assignments evaluate the left-
hand side *before* evaluating the right-hand side. For example, "a[i]
+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs
the addition, and lastly, it writes the result back to "a[i]".
With the exception of assigning to tuples and multiple targets in a
single statement, the assignment done by augmented assignment
statements is handled the same way as normal assignments. Similarly,
with the exception of the possible *in-place* behavior, the binary
operation performed by augmented assignment is the same as the normal
binary operations.
For targets which are attribute references, the same caveat about
class and instance attributes applies as for regular assignments.
Annotated assignment statements
===============================
*Annotation* assignment is the combination, in a single statement, of
a variable or attribute annotation and an optional assignment
statement:
annotated_assignment_stmt: augtarget ":" expression
["=" (starred_expression | yield_expression)]
The difference from normal Assignment statements is that only a single
target is allowed.
The assignment target is considered “simple” if it consists of a
single name that is not enclosed in parentheses. For simple assignment
targets, if in class or module scope, the annotations are gathered in
a lazily evaluated annotation scope. The annotations can be evaluated
using the "__annotations__" attribute of a class or module, or using
the facilities in the "annotationlib" module.
If the assignment target is not simple (an attribute, subscript node,
or parenthesized name), the annotation is never evaluated.
If a name is annotated in a function scope, then this name is local
for that scope. Annotations are never evaluated and stored in function
scopes.
If the right hand side is present, an annotated assignment performs
the actual assignment as if there was no annotation present. If the
right hand side is not present for an expression target, then the
interpreter evaluates the target except for the last "__setitem__()"
or "__setattr__()" call.
See also:
**PEP 526** - Syntax for Variable Annotations
The proposal that added syntax for annotating the types of
variables (including class variables and instance variables),
instead of expressing them through comments.
**PEP 484** - Type hints
The proposal that added the "typing" module to provide a standard
syntax for type annotations that can be used in static analysis
tools and IDEs.
Changed in version 3.8: Now annotated assignments allow the same
expressions in the right hand side as regular assignments. Previously,
some expressions (like un-parenthesized tuple expressions) caused a
syntax error.
Changed in version 3.14: Annotations are now lazily evaluated in a
separate annotation scope. If the assignment target is not simple,
annotations are never evaluated.
''',
'assignment-expressions': r'''Assignment expressions
**********************
assignment_expression: [identifier ":="] expression
An assignment expression (sometimes also called a “named expression”
or “walrus”) assigns an "expression" to an "identifier", while also
returning the value of the "expression".
One common use case is when handling matched regular expressions:
if matching := pattern.search(data):
do_something(matching)
Or, when processing a file stream in chunks:
while chunk := file.read(9000):
process(chunk)
Assignment expressions must be surrounded by parentheses when used as
expression statements and when used as sub-expressions in slicing,
conditional, lambda, keyword-argument, and comprehension-if
expressions and in "assert", "with", and "assignment" statements. In
all other places where they can be used, parentheses are not required,
including in "if" and "while" statements.
Added in version 3.8: See **PEP 572** for more details about
assignment expressions.
''',
'async': r'''Coroutines
**********
Added in version 3.5.
Coroutine function definition
=============================
async_funcdef: [decorators] "async" "def" funcname "(" [parameter_list] ")"
["->" expression] ":" suite
Execution of Python coroutines can be suspended and resumed at many
points (see *coroutine*). "await" expressions, "async for" and "async
with" can only be used in the body of a coroutine function.
Functions defined with "async def" syntax are always coroutine
functions, even if they do not contain "await" or "async" keywords.
It is a "SyntaxError" to use a "yield from" expression inside the body
of a coroutine function.
An example of a coroutine function:
async def func(param1, param2):
do_stuff()
await some_coroutine()
Changed in version 3.7: "await" and "async" are now keywords;
previously they were only treated as such inside the body of a
coroutine function.
The "async for" statement
=========================
async_for_stmt: "async" for_stmt
An *asynchronous iterable* provides an "__aiter__" method that
directly returns an *asynchronous iterator*, which can call
asynchronous code in its "__anext__" method.
The "async for" statement allows convenient iteration over
asynchronous iterables.
The following code:
async for TARGET in ITER:
SUITE
else:
SUITE2
Is semantically equivalent to:
iter = (ITER)
iter = type(iter).__aiter__(iter)
running = True
while running:
try:
TARGET = await type(iter).__anext__(iter)
except StopAsyncIteration:
running = False
else:
SUITE
else:
SUITE2
See also "__aiter__()" and "__anext__()" for details.
It is a "SyntaxError" to use an "async for" statement outside the body
of a coroutine function.
The "async with" statement
==========================
async_with_stmt: "async" with_stmt
An *asynchronous context manager* is a *context manager* that is able
to suspend execution in its *enter* and *exit* methods.
The following code:
async with EXPRESSION as TARGET:
SUITE
is semantically equivalent to:
manager = (EXPRESSION)
aenter = type(manager).__aenter__
aexit = type(manager).__aexit__
value = await aenter(manager)
hit_except = False
try:
TARGET = value
SUITE
except:
hit_except = True
if not await aexit(manager, *sys.exc_info()):
raise
finally:
if not hit_except:
await aexit(manager, None, None, None)
See also "__aenter__()" and "__aexit__()" for details.
It is a "SyntaxError" to use an "async with" statement outside the
body of a coroutine function.
See also:
**PEP 492** - Coroutines with async and await syntax
The proposal that made coroutines a proper standalone concept in
Python, and added supporting syntax.
''',
'atom-identifiers': r'''Identifiers (Names)
*******************
An identifier occurring as an atom is a name. See section Identifiers
and keywords for lexical definition and section Naming and binding for
documentation of naming and binding.
When the name is bound to an object, evaluation of the atom yields
that object. When a name is not bound, an attempt to evaluate it
raises a "NameError" exception.
Private name mangling
=====================
When an identifier that textually occurs in a class definition begins
with two or more underscore characters and does not end in two or more
underscores, it is considered a *private name* of that class.
See also: The class specifications.
More precisely, private names are transformed to a longer form before
code is generated for them. If the transformed name is longer than
255 characters, implementation-defined truncation may happen.
The transformation is independent of the syntactical context in which
the identifier is used but only the following private identifiers are
mangled:
* Any name used as the name of a variable that is assigned or read or
any name of an attribute being accessed.
The "__name__" attribute of nested functions, classes, and type
aliases is however not mangled.
* The name of imported modules, e.g., "__spam" in "import __spam". If
the module is part of a package (i.e., its name contains a dot), the
name is *not* mangled, e.g., the "__foo" in "import __foo.bar" is
not mangled.
* The name of an imported member, e.g., "__f" in "from spam import
__f".
The transformation rule is defined as follows:
* The class name, with leading underscores removed and a single
leading underscore inserted, is inserted in front of the identifier,
e.g., the identifier "__spam" occurring in a class named "Foo",
"_Foo" or "__Foo" is transformed to "_Foo__spam".
* If the class name consists only of underscores, the transformation
is the identity, e.g., the identifier "__spam" occurring in a class
named "_" or "__" is left as is.
''',
'atom-literals': r'''Literals
********
Python supports string and bytes literals and various numeric
literals:
literal: stringliteral | bytesliteral
| integer | floatnumber | imagnumber
Evaluation of a literal yields an object of the given type (string,
bytes, integer, floating-point number, complex number) with the given
value. The value may be approximated in the case of floating-point
and imaginary (complex) literals. See section Literals for details.
All literals correspond to immutable data types, and hence the
object’s identity is less important than its value. Multiple
evaluations of literals with the same value (either the same
occurrence in the program text or a different occurrence) may obtain
the same object or a different object with the same value.
''',
'attribute-access': r'''Customizing attribute access
****************************
The following methods can be defined to customize the meaning of
attribute access (use of, assignment to, or deletion of "x.name") for
class instances.
object.__getattr__(self, name)
Called when the default attribute access fails with an
"AttributeError" (either "__getattribute__()" raises an
"AttributeError" because *name* is not an instance attribute or an
attribute in the class tree for "self"; or "__get__()" of a *name*
property raises "AttributeError"). This method should either
return the (computed) attribute value or raise an "AttributeError"
exception. The "object" class itself does not provide this method.
Note that if the attribute is found through the normal mechanism,
"__getattr__()" is not called. (This is an intentional asymmetry
between "__getattr__()" and "__setattr__()".) This is done both for
efficiency reasons and because otherwise "__getattr__()" would have
no way to access other attributes of the instance. Note that at
least for instance variables, you can take total control by not
inserting any values in the instance attribute dictionary (but
instead inserting them in another object). See the
"__getattribute__()" method below for a way to actually get total
control over attribute access.
object.__getattribute__(self, name)
Called unconditionally to implement attribute accesses for
instances of the class. If the class also defines "__getattr__()",
the latter will not be called unless "__getattribute__()" either
calls it explicitly or raises an "AttributeError". This method
should return the (computed) attribute value or raise an
"AttributeError" exception. In order to avoid infinite recursion in
this method, its implementation should always call the base class
method with the same name to access any attributes it needs, for
example, "object.__getattribute__(self, name)".
Note:
This method may still be bypassed when looking up special methods
as the result of implicit invocation via language syntax or
built-in functions. See Special method lookup.
For certain sensitive attribute accesses, raises an auditing event
"object.__getattr__" with arguments "obj" and "name".
object.__setattr__(self, name, value)
Called when an attribute assignment is attempted. This is called
instead of the normal mechanism (i.e. store the value in the
instance dictionary). *name* is the attribute name, *value* is the
value to be assigned to it.
If "__setattr__()" wants to assign to an instance attribute, it
should call the base class method with the same name, for example,
"object.__setattr__(self, name, value)".
For certain sensitive attribute assignments, raises an auditing
event "object.__setattr__" with arguments "obj", "name", "value".
object.__delattr__(self, name)
Like "__setattr__()" but for attribute deletion instead of
assignment. This should only be implemented if "del obj.name" is
meaningful for the object.
For certain sensitive attribute deletions, raises an auditing event
"object.__delattr__" with arguments "obj" and "name".
object.__dir__(self)
Called when "dir()" is called on the object. An iterable must be
returned. "dir()" converts the returned iterable to a list and
sorts it.
Customizing module attribute access
===================================
Special names "__getattr__" and "__dir__" can be also used to
customize access to module attributes. The "__getattr__" function at
the module level should accept one argument which is the name of an
attribute and return the computed value or raise an "AttributeError".
If an attribute is not found on a module object through the normal
lookup, i.e. "object.__getattribute__()", then "__getattr__" is
searched in the module "__dict__" before raising an "AttributeError".
If found, it is called with the attribute name and the result is
returned.
The "__dir__" function should accept no arguments, and return an
iterable of strings that represents the names accessible on module. If
present, this function overrides the standard "dir()" search on a
module.
For a more fine grained customization of the module behavior (setting
attributes, properties, etc.), one can set the "__class__" attribute
of a module object to a subclass of "types.ModuleType". For example:
import sys
from types import ModuleType
class VerboseModule(ModuleType):
def __repr__(self):
return f'Verbose {self.__name__}'
def __setattr__(self, attr, value):
print(f'Setting {attr}...')
super().__setattr__(attr, value)
sys.modules[__name__].__class__ = VerboseModule
Note:
Defining module "__getattr__" and setting module "__class__" only
affect lookups made using the attribute access syntax – directly
accessing the module globals (whether by code within the module, or
via a reference to the module’s globals dictionary) is unaffected.
Changed in version 3.5: "__class__" module attribute is now writable.
Added in version 3.7: "__getattr__" and "__dir__" module attributes.
See also:
**PEP 562** - Module __getattr__ and __dir__
Describes the "__getattr__" and "__dir__" functions on modules.
Implementing Descriptors
========================
The following methods only apply when an instance of the class
containing the method (a so-called *descriptor* class) appears in an
*owner* class (the descriptor must be in either the owner’s class
dictionary or in the class dictionary for one of its parents). In the
examples below, “the attribute” refers to the attribute whose name is
the key of the property in the owner class’ "__dict__". The "object"
class itself does not implement any of these protocols.
object.__get__(self, instance, owner=None)
Called to get the attribute of the owner class (class attribute
access) or of an instance of that class (instance attribute
access). The optional *owner* argument is the owner class, while
*instance* is the instance that the attribute was accessed through,
or "None" when the attribute is accessed through the *owner*.
This method should return the computed attribute value or raise an
"AttributeError" exception.
**PEP 252** specifies that "__get__()" is callable with one or two
arguments. Python’s own built-in descriptors support this
specification; however, it is likely that some third-party tools
have descriptors that require both arguments. Python’s own
"__getattribute__()" implementation always passes in both arguments
whether they are required or not.
object.__set__(self, instance, value)
Called to set the attribute on an instance *instance* of the owner
class to a new value, *value*.
Note, adding "__set__()" or "__delete__()" changes the kind of
descriptor to a “data descriptor”. See Invoking Descriptors for
more details.
object.__delete__(self, instance)
Called to delete the attribute on an instance *instance* of the
owner class.
Instances of descriptors may also have the "__objclass__" attribute
present:
object.__objclass__
The attribute "__objclass__" is interpreted by the "inspect" module
as specifying the class where this object was defined (setting this
appropriately can assist in runtime introspection of dynamic class
attributes). For callables, it may indicate that an instance of the
given type (or a subclass) is expected or required as the first
positional argument (for example, CPython sets this attribute for
unbound methods that are implemented in C).
Invoking Descriptors
====================
In general, a descriptor is an object attribute with “binding
behavior”, one whose attribute access has been overridden by methods
in the descriptor protocol: "__get__()", "__set__()", and
"__delete__()". If any of those methods are defined for an object, it
is said to be a descriptor.
The default behavior for attribute access is to get, set, or delete
the attribute from an object’s dictionary. For instance, "a.x" has a
lookup chain starting with "a.__dict__['x']", then
"type(a).__dict__['x']", and continuing through the base classes of
"type(a)" excluding metaclasses.
However, if the looked-up value is an object defining one of the
descriptor methods, then Python may override the default behavior and
invoke the descriptor method instead. Where this occurs in the
precedence chain depends on which descriptor methods were defined and
how they were called.
The starting point for descriptor invocation is a binding, "a.x". How
the arguments are assembled depends on "a":
Direct Call
The simplest and least common call is when user code directly
invokes a descriptor method: "x.__get__(a)".
Instance Binding
If binding to an object instance, "a.x" is transformed into the
call: "type(a).__dict__['x'].__get__(a, type(a))".
Class Binding
If binding to a class, "A.x" is transformed into the call:
"A.__dict__['x'].__get__(None, A)".
Super Binding
A dotted lookup such as "super(A, a).x" searches
"a.__class__.__mro__" for a base class "B" following "A" and then
returns "B.__dict__['x'].__get__(a, A)". If not a descriptor, "x"
is returned unchanged.
For instance bindings, the precedence of descriptor invocation depends
on which descriptor methods are defined. A descriptor can define any
combination of "__get__()", "__set__()" and "__delete__()". If it
does not define "__get__()", then accessing the attribute will return
the descriptor object itself unless there is a value in the object’s
instance dictionary. If the descriptor defines "__set__()" and/or
"__delete__()", it is a data descriptor; if it defines neither, it is
a non-data descriptor. Normally, data descriptors define both
"__get__()" and "__set__()", while non-data descriptors have just the
"__get__()" method. Data descriptors with "__get__()" and "__set__()"
(and/or "__delete__()") defined always override a redefinition in an
instance dictionary. In contrast, non-data descriptors can be
overridden by instances.
Python methods (including those decorated with "@staticmethod" and
"@classmethod") are implemented as non-data descriptors. Accordingly,
instances can redefine and override methods. This allows individual
instances to acquire behaviors that differ from other instances of the
same class.
The "property()" function is implemented as a data descriptor.
Accordingly, instances cannot override the behavior of a property.
__slots__
=========
*__slots__* allow us to explicitly declare data members (like
properties) and deny the creation of "__dict__" and *__weakref__*
(unless explicitly declared in *__slots__* or available in a parent.)
The space saved over using "__dict__" can be significant. Attribute
lookup speed can be significantly improved as well.
object.__slots__
This class variable can be assigned a string, iterable, or sequence
of strings with variable names used by instances. *__slots__*
reserves space for the declared variables and prevents the
automatic creation of "__dict__" and *__weakref__* for each
instance.
Notes on using *__slots__*:
* When inheriting from a class without *__slots__*, the "__dict__" and
*__weakref__* attribute of the instances will always be accessible.
* Without a "__dict__" variable, instances cannot be assigned new
variables not listed in the *__slots__* definition. Attempts to
assign to an unlisted variable name raises "AttributeError". If
dynamic assignment of new variables is desired, then add
"'__dict__'" to the sequence of strings in the *__slots__*
declaration.
* Without a *__weakref__* variable for each instance, classes defining
*__slots__* do not support "weak references" to its instances. If
weak reference support is needed, then add "'__weakref__'" to the
sequence of strings in the *__slots__* declaration.
* *__slots__* are implemented at the class level by creating
descriptors for each variable name. As a result, class attributes
cannot be used to set default values for instance variables defined
by *__slots__*; otherwise, the class attribute would overwrite the
descriptor assignment.
* The action of a *__slots__* declaration is not limited to the class
where it is defined. *__slots__* declared in parents are available
in child classes. However, instances of a child subclass will get a
"__dict__" and *__weakref__* unless the subclass also defines
*__slots__* (which should only contain names of any *additional*
slots).
* If a class defines a slot also defined in a base class, the instance
variable defined by the base class slot is inaccessible (except by
retrieving its descriptor directly from the base class). This
renders the meaning of the program undefined. In the future, a
check may be added to prevent this.
* "TypeError" will be raised if nonempty *__slots__* are defined for a
class derived from a ""variable-length" built-in type" such as
"int", "bytes", and "tuple".
* Any non-string *iterable* may be assigned to *__slots__*.
* If a "dictionary" is used to assign *__slots__*, the dictionary keys
will be used as the slot names. The values of the dictionary can be
used to provide per-attribute docstrings that will be recognised by
"inspect.getdoc()" and displayed in the output of "help()".
* "__class__" assignment works only if both classes have the same
*__slots__*.
* Multiple inheritance with multiple slotted parent classes can be
used, but only one parent is allowed to have attributes created by
slots (the other bases must have empty slot layouts) - violations
raise "TypeError".
* If an *iterator* is used for *__slots__* then a *descriptor* is
created for each of the iterator’s values. However, the *__slots__*
attribute will be an empty iterator.
''',
'attribute-references': r'''Attribute references
********************
An attribute reference is a primary followed by a period and a name:
attributeref: primary "." identifier
The primary must evaluate to an object of a type that supports
attribute references, which most objects do. This object is then
asked to produce the attribute whose name is the identifier. The type
and value produced is determined by the object. Multiple evaluations
of the same attribute reference may yield different objects.
This production can be customized by overriding the
"__getattribute__()" method or the "__getattr__()" method. The
"__getattribute__()" method is called first and either returns a value
or raises "AttributeError" if the attribute is not available.
If an "AttributeError" is raised and the object has a "__getattr__()"
method, that method is called as a fallback.
''',
'augassign': r'''Augmented assignment statements
*******************************
Augmented assignment is the combination, in a single statement, of a
binary operation and an assignment statement:
augmented_assignment_stmt: augtarget augop (expression_list | yield_expression)
augtarget: identifier | attributeref | subscription | slicing
augop: "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**="
| ">>=" | "<<=" | "&=" | "^=" | "|="
(See section Primaries for the syntax definitions of the last three
symbols.)
An augmented assignment evaluates the target (which, unlike normal
assignment statements, cannot be an unpacking) and the expression
list, performs the binary operation specific to the type of assignment
on the two operands, and assigns the result to the original target.
The target is only evaluated once.
An augmented assignment statement like "x += 1" can be rewritten as "x
= x + 1" to achieve a similar, but not exactly equal effect. In the
augmented version, "x" is only evaluated once. Also, when possible,
the actual operation is performed *in-place*, meaning that rather than
creating a new object and assigning that to the target, the old object
is modified instead.
Unlike normal assignments, augmented assignments evaluate the left-
hand side *before* evaluating the right-hand side. For example, "a[i]
+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs
the addition, and lastly, it writes the result back to "a[i]".
With the exception of assigning to tuples and multiple targets in a
single statement, the assignment done by augmented assignment
statements is handled the same way as normal assignments. Similarly,
with the exception of the possible *in-place* behavior, the binary
operation performed by augmented assignment is the same as the normal
binary operations.
For targets which are attribute references, the same caveat about
class and instance attributes applies as for regular assignments.
''',
'await': r'''Await expression
****************
Suspend the execution of *coroutine* on an *awaitable* object. Can
only be used inside a *coroutine function*.
await_expr: "await" primary
Added in version 3.5.
''',
'binary': r'''Binary arithmetic operations
****************************
The binary arithmetic operations have the conventional priority
levels. Note that some of these operations also apply to certain non-
numeric types. Apart from the power operator, there are only two
levels, one for multiplicative operators and one for additive
operators:
m_expr: u_expr | m_expr "*" u_expr | m_expr "@" m_expr |
m_expr "//" u_expr | m_expr "/" u_expr |
m_expr "%" u_expr
a_expr: m_expr | a_expr "+" m_expr | a_expr "-" m_expr
The "*" (multiplication) operator yields the product of its arguments.
The arguments must either both be numbers, or one argument must be an
integer and the other must be a sequence. In the former case, the
numbers are converted to a common real type and then multiplied
together. In the latter case, sequence repetition is performed; a
negative repetition factor yields an empty sequence.
This operation can be customized using the special "__mul__()" and
"__rmul__()" methods.
Changed in version 3.14: If only one operand is a complex number, the
other operand is converted to a floating-point number.
The "@" (at) operator is intended to be used for matrix
multiplication. No builtin Python types implement this operator.
This operation can be customized using the special "__matmul__()" and
"__rmatmul__()" methods.
Added in version 3.5.
The "/" (division) and "//" (floor division) operators yield the
quotient of their arguments. The numeric arguments are first
converted to a common type. Division of integers yields a float, while
floor division of integers results in an integer; the result is that
of mathematical division with the ‘floor’ function applied to the
result. Division by zero raises the "ZeroDivisionError" exception.
The division operation can be customized using the special
"__truediv__()" and "__rtruediv__()" methods. The floor division
operation can be customized using the special "__floordiv__()" and
"__rfloordiv__()" methods.
The "%" (modulo) operator yields the remainder from the division of
the first argument by the second. The numeric arguments are first
converted to a common type. A zero right argument raises the
"ZeroDivisionError" exception. The arguments may be floating-point
numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals "4*0.7 +
0.34".) The modulo operator always yields a result with the same sign
as its second operand (or zero); the absolute value of the result is
strictly smaller than the absolute value of the second operand [1].
The floor division and modulo operators are connected by the following
identity: "x == (x//y)*y + (x%y)". Floor division and modulo are also
connected with the built-in function "divmod()": "divmod(x, y) ==
(x//y, x%y)". [2].
In addition to performing the modulo operation on numbers, the "%"
operator is also overloaded by string objects to perform old-style
string formatting (also known as interpolation). The syntax for
string formatting is described in the Python Library Reference,
section printf-style String Formatting.
The *modulo* operation can be customized using the special "__mod__()"
and "__rmod__()" methods.
The floor division operator, the modulo operator, and the "divmod()"
function are not defined for complex numbers. Instead, convert to a
floating-point number using the "abs()" function if appropriate.
The "+" (addition) operator yields the sum of its arguments. The
arguments must either both be numbers or both be sequences of the same
type. In the former case, the numbers are converted to a common real
type and then added together. In the latter case, the sequences are
concatenated.
This operation can be customized using the special "__add__()" and
"__radd__()" methods.
Changed in version 3.14: If only one operand is a complex number, the
other operand is converted to a floating-point number.
The "-" (subtraction) operator yields the difference of its arguments.
The numeric arguments are first converted to a common real type.
This operation can be customized using the special "__sub__()" and
"__rsub__()" methods.
Changed in version 3.14: If only one operand is a complex number, the
other operand is converted to a floating-point number.
''',
'bitwise': r'''Binary bitwise operations
*************************
Each of the three bitwise operations has a different priority level:
and_expr: shift_expr | and_expr "&" shift_expr
xor_expr: and_expr | xor_expr "^" and_expr
or_expr: xor_expr | or_expr "|" xor_expr
The "&" operator yields the bitwise AND of its arguments, which must
be integers or one of them must be a custom object overriding
"__and__()" or "__rand__()" special methods.
The "^" operator yields the bitwise XOR (exclusive OR) of its
arguments, which must be integers or one of them must be a custom
object overriding "__xor__()" or "__rxor__()" special methods.
The "|" operator yields the bitwise (inclusive) OR of its arguments,
which must be integers or one of them must be a custom object
overriding "__or__()" or "__ror__()" special methods.
''',
'bltin-code-objects': r'''Code Objects
************
Code objects are used by the implementation to represent “pseudo-
compiled” executable Python code such as a function body. They differ
from function objects because they don’t contain a reference to their
global execution environment. Code objects are returned by the built-
in "compile()" function and can be extracted from function objects
through their "__code__" attribute. See also the "code" module.
Accessing "__code__" raises an auditing event "object.__getattr__"
with arguments "obj" and ""__code__"".
A code object can be executed or evaluated by passing it (instead of a
source string) to the "exec()" or "eval()" built-in functions.
See The standard type hierarchy for more information.
''',
'bltin-ellipsis-object': r'''The Ellipsis Object
*******************
This object is commonly used by slicing (see Slicings). It supports
no special operations. There is exactly one ellipsis object, named
"Ellipsis" (a built-in name). "type(Ellipsis)()" produces the
"Ellipsis" singleton.
It is written as "Ellipsis" or "...".
''',
'bltin-null-object': r'''The Null Object
***************
This object is returned by functions that don’t explicitly return a
value. It supports no special operations. There is exactly one null
object, named "None" (a built-in name). "type(None)()" produces the
same singleton.
It is written as "None".
''',
'bltin-type-objects': r'''Type Objects
************
Type objects represent the various object types. An object’s type is
accessed by the built-in function "type()". There are no special
operations on types. The standard module "types" defines names for
all standard built-in types.
Types are written like this: "<class 'int'>".
''',
'booleans': r'''Boolean operations
******************
or_test: and_test | or_test "or" and_test
and_test: not_test | and_test "and" not_test
not_test: comparison | "not" not_test
In the context of Boolean operations, and also when expressions are
used by control flow statements, the following values are interpreted
as false: "False", "None", numeric zero of all types, and empty
strings and containers (including strings, tuples, lists,
dictionaries, sets and frozensets). All other values are interpreted
as true. User-defined objects can customize their truth value by
providing a "__bool__()" method.
The operator "not" yields "True" if its argument is false, "False"
otherwise.
The expression "x and y" first evaluates *x*; if *x* is false, its
value is returned; otherwise, *y* is evaluated and the resulting value
is returned.
The expression "x or y" first evaluates *x*; if *x* is true, its value
is returned; otherwise, *y* is evaluated and the resulting value is
returned.
Note that neither "and" nor "or" restrict the value and type they
return to "False" and "True", but rather return the last evaluated
argument. This is sometimes useful, e.g., if "s" is a string that
should be replaced by a default value if it is empty, the expression
"s or 'foo'" yields the desired value. Because "not" has to create a
new value, it returns a boolean value regardless of the type of its
argument (for example, "not 'foo'" produces "False" rather than "''".)
''',
'break': r'''The "break" statement
*********************
break_stmt: "break"
"break" may only occur syntactically nested in a "for" or "while"
loop, but not nested in a function or class definition within that
loop.
It terminates the nearest enclosing loop, skipping the optional "else"
clause if the loop has one.
If a "for" loop is terminated by "break", the loop control target
keeps its current value.
When "break" passes control out of a "try" statement with a "finally"
clause, that "finally" clause is executed before really leaving the
loop.
''',
'callable-types': r'''Emulating callable objects
**************************
object.__call__(self[, args...])
Called when the instance is “called” as a function; if this method
is defined, "x(arg1, arg2, ...)" roughly translates to
"type(x).__call__(x, arg1, ...)". The "object" class itself does
not provide this method.
''',
'calls': r'''Calls
*****
A call calls a callable object (e.g., a *function*) with a possibly
empty series of *arguments*:
call: primary "(" [argument_list [","] | comprehension] ")"
argument_list: positional_arguments ["," starred_and_keywords]
["," keywords_arguments]
| starred_and_keywords ["," keywords_arguments]
| keywords_arguments
positional_arguments: positional_item ("," positional_item)*
positional_item: assignment_expression | "*" expression
starred_and_keywords: ("*" expression | keyword_item)
("," "*" expression | "," keyword_item)*
keywords_arguments: (keyword_item | "**" expression)
("," keyword_item | "," "**" expression)*
keyword_item: identifier "=" expression
An optional trailing comma may be present after the positional and
keyword arguments but does not affect the semantics.
The primary must evaluate to a callable object (user-defined
functions, built-in functions, methods of built-in objects, class
objects, methods of class instances, and all objects having a
"__call__()" method are callable). All argument expressions are
evaluated before the call is attempted. Please refer to section
Function definitions for the syntax of formal *parameter* lists.
If keyword arguments are present, they are first converted to
positional arguments, as follows. First, a list of unfilled slots is
created for the formal parameters. If there are N positional
arguments, they are placed in the first N slots. Next, for each
keyword argument, the identifier is used to determine the
corresponding slot (if the identifier is the same as the first formal
parameter name, the first slot is used, and so on). If the slot is
already filled, a "TypeError" exception is raised. Otherwise, the
argument is placed in the slot, filling it (even if the expression is
"None", it fills the slot). When all arguments have been processed,
the slots that are still unfilled are filled with the corresponding
default value from the function definition. (Default values are
calculated, once, when the function is defined; thus, a mutable object
such as a list or dictionary used as default value will be shared by
all calls that don’t specify an argument value for the corresponding
slot; this should usually be avoided.) If there are any unfilled
slots for which no default value is specified, a "TypeError" exception
is raised. Otherwise, the list of filled slots is used as the
argument list for the call.
**CPython implementation detail:** An implementation may provide
built-in functions whose positional parameters do not have names, even
if they are ‘named’ for the purpose of documentation, and which
therefore cannot be supplied by keyword. In CPython, this is the case
for functions implemented in C that use "PyArg_ParseTuple()" to parse
their arguments.
If there are more positional arguments than there are formal parameter
slots, a "TypeError" exception is raised, unless a formal parameter
using the syntax "*identifier" is present; in this case, that formal
parameter receives a tuple containing the excess positional arguments
(or an empty tuple if there were no excess positional arguments).
If any keyword argument does not correspond to a formal parameter
name, a "TypeError" exception is raised, unless a formal parameter
using the syntax "**identifier" is present; in this case, that formal
parameter receives a dictionary containing the excess keyword
arguments (using the keywords as keys and the argument values as
corresponding values), or a (new) empty dictionary if there were no
excess keyword arguments.
If the syntax "*expression" appears in the function call, "expression"
must evaluate to an *iterable*. Elements from these iterables are
treated as if they were additional positional arguments. For the call
"f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, …, *yM*,
this is equivalent to a call with M+4 positional arguments *x1*, *x2*,
*y1*, …, *yM*, *x3*, *x4*.
A consequence of this is that although the "*expression" syntax may
appear *after* explicit keyword arguments, it is processed *before*
the keyword arguments (and any "**expression" arguments – see below).
So:
>>> def f(a, b):
... print(a, b)
...
>>> f(b=1, *(2,))
2 1
>>> f(a=1, *(2,))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'a'
>>> f(1, *(2,))
1 2
It is unusual for both keyword arguments and the "*expression" syntax
to be used in the same call, so in practice this confusion does not
often arise.
If the syntax "**expression" appears in the function call,
"expression" must evaluate to a *mapping*, the contents of which are
treated as additional keyword arguments. If a parameter matching a key
has already been given a value (by an explicit keyword argument, or
from another unpacking), a "TypeError" exception is raised.
When "**expression" is used, each key in this mapping must be a
string. Each value from the mapping is assigned to the first formal
parameter eligible for keyword assignment whose name is equal to the
key. A key need not be a Python identifier (e.g. ""max-temp °F"" is
acceptable, although it will not match any formal parameter that could
be declared). If there is no match to a formal parameter the key-value
pair is collected by the "**" parameter, if there is one, or if there
is not, a "TypeError" exception is raised.
Formal parameters using the syntax "*identifier" or "**identifier"
cannot be used as positional argument slots or as keyword argument
names.
Changed in version 3.5: Function calls accept any number of "*" and
"**" unpackings, positional arguments may follow iterable unpackings
("*"), and keyword arguments may follow dictionary unpackings ("**").
Originally proposed by **PEP 448**.
A call always returns some value, possibly "None", unless it raises an
exception. How this value is computed depends on the type of the
callable object.
If it is—
a user-defined function:
The code block for the function is executed, passing it the
argument list. The first thing the code block will do is bind the
formal parameters to the arguments; this is described in section
Function definitions. When the code block executes a "return"
statement, this specifies the return value of the function call.
If execution reaches the end of the code block without executing a
"return" statement, the return value is "None".
a built-in function or method:
The result is up to the interpreter; see Built-in Functions for the
descriptions of built-in functions and methods.
a class object:
A new instance of that class is returned.
a class instance method:
The corresponding user-defined function is called, with an argument
list that is one longer than the argument list of the call: the
instance becomes the first argument.
a class instance:
The class must define a "__call__()" method; the effect is then the
same as if that method was called.
''',
'class': r'''Class definitions
*****************
A class definition defines a class object (see section The standard
type hierarchy):
classdef: [decorators] "class" classname [type_params] [inheritance] ":" suite
inheritance: "(" [argument_list] ")"
classname: identifier
A class definition is an executable statement. The inheritance list
usually gives a list of base classes (see Metaclasses for more
advanced uses), so each item in the list should evaluate to a class
object which allows subclassing. Classes without an inheritance list
inherit, by default, from the base class "object"; hence,
class Foo:
pass
is equivalent to
class Foo(object):
pass
The class’s suite is then executed in a new execution frame (see
Naming and binding), using a newly created local namespace and the
original global namespace. (Usually, the suite contains mostly
function definitions.) When the class’s suite finishes execution, its
execution frame is discarded but its local namespace is saved. [5] A
class object is then created using the inheritance list for the base
classes and the saved local namespace for the attribute dictionary.
The class name is bound to this class object in the original local
namespace.
The order in which attributes are defined in the class body is
preserved in the new class’s "__dict__". Note that this is reliable
only right after the class is created and only for classes that were
defined using the definition syntax.
Class creation can be customized heavily using metaclasses.
Classes can also be decorated: just like when decorating functions,
@f1(arg)
@f2
class Foo: pass
is roughly equivalent to
class Foo: pass
Foo = f1(arg)(f2(Foo))
The evaluation rules for the decorator expressions are the same as for
function decorators. The result is then bound to the class name.
Changed in version 3.9: Classes may be decorated with any valid
"assignment_expression". Previously, the grammar was much more
restrictive; see **PEP 614** for details.
A list of type parameters may be given in square brackets immediately
after the class’s name. This indicates to static type checkers that
the class is generic. At runtime, the type parameters can be retrieved
from the class’s "__type_params__" attribute. See Generic classes for
more.
Changed in version 3.12: Type parameter lists are new in Python 3.12.
**Programmer’s note:** Variables defined in the class definition are
class attributes; they are shared by instances. Instance attributes
can be set in a method with "self.name = value". Both class and
instance attributes are accessible through the notation “"self.name"”,
and an instance attribute hides a class attribute with the same name
when accessed in this way. Class attributes can be used as defaults
for instance attributes, but using mutable values there can lead to
unexpected results. Descriptors can be used to create instance
variables with different implementation details.
See also:
**PEP 3115** - Metaclasses in Python 3000
The proposal that changed the declaration of metaclasses to the
current syntax, and the semantics for how classes with
metaclasses are constructed.
**PEP 3129** - Class Decorators
The proposal that added class decorators. Function and method
decorators were introduced in **PEP 318**.
''',
'comparisons': r'''Comparisons
***********
Unlike C, all comparison operations in Python have the same priority,
which is lower than that of any arithmetic, shifting or bitwise
operation. Also unlike C, expressions like "a < b < c" have the
interpretation that is conventional in mathematics:
comparison: or_expr (comp_operator or_expr)*
comp_operator: "<" | ">" | "==" | ">=" | "<=" | "!="
| "is" ["not"] | ["not"] "in"
Comparisons yield boolean values: "True" or "False". Custom *rich
comparison methods* may return non-boolean values. In this case Python
will call "bool()" on such value in boolean contexts.
Comparisons can be chained arbitrarily, e.g., "x < y <= z" is
equivalent to "x < y and y <= z", except that "y" is evaluated only
once (but in both cases "z" is not evaluated at all when "x < y" is
found to be false).
Formally, if *a*, *b*, *c*, …, *y*, *z* are expressions and *op1*,
*op2*, …, *opN* are comparison operators, then "a op1 b op2 c ... y
opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", except
that each expression is evaluated at most once.
Note that "a op1 b op2 c" doesn’t imply any kind of comparison between
*a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though
perhaps not pretty).
Value comparisons
=================
The operators "<", ">", "==", ">=", "<=", and "!=" compare the values
of two objects. The objects do not need to have the same type.
Chapter Objects, values and types states that objects have a value (in
addition to type and identity). The value of an object is a rather
abstract notion in Python: For example, there is no canonical access
method for an object’s value. Also, there is no requirement that the
value of an object should be constructed in a particular way, e.g.
comprised of all its data attributes. Comparison operators implement a
particular notion of what the value of an object is. One can think of
them as defining the value of an object indirectly, by means of their
comparison implementation.
Because all types are (direct or indirect) subtypes of "object", they
inherit the default comparison behavior from "object". Types can
customize their comparison behavior by implementing *rich comparison
methods* like "__lt__()", described in Basic customization.
The default behavior for equality comparison ("==" and "!=") is based
on the identity of the objects. Hence, equality comparison of
instances with the same identity results in equality, and equality
comparison of instances with different identities results in
inequality. A motivation for this default behavior is the desire that
all objects should be reflexive (i.e. "x is y" implies "x == y").
A default order comparison ("<", ">", "<=", and ">=") is not provided;
an attempt raises "TypeError". A motivation for this default behavior
is the lack of a similar invariant as for equality.
The behavior of the default equality comparison, that instances with
different identities are always unequal, may be in contrast to what
types will need that have a sensible definition of object value and
value-based equality. Such types will need to customize their
comparison behavior, and in fact, a number of built-in types have done
that.
The following list describes the comparison behavior of the most
important built-in types.
* Numbers of built-in numeric types (Numeric Types — int, float,
complex) and of the standard library types "fractions.Fraction" and
"decimal.Decimal" can be compared within and across their types,
with the restriction that complex numbers do not support order
comparison. Within the limits of the types involved, they compare
mathematically (algorithmically) correct without loss of precision.
The not-a-number values "float('NaN')" and "decimal.Decimal('NaN')"
are special. Any ordered comparison of a number to a not-a-number
value is false. A counter-intuitive implication is that not-a-number
values are not equal to themselves. For example, if "x =
float('NaN')", "3 < x", "x < 3" and "x == x" are all false, while "x
!= x" is true. This behavior is compliant with IEEE 754.
* "None" and "NotImplemented" are singletons. **PEP 8** advises that
comparisons for singletons should always be done with "is" or "is
not", never the equality operators.
* Binary sequences (instances of "bytes" or "bytearray") can be
compared within and across their types. They compare
lexicographically using the numeric values of their elements.
* Strings (instances of "str") compare lexicographically using the
numerical Unicode code points (the result of the built-in function
"ord()") of their characters. [3]
Strings and binary sequences cannot be directly compared.
* Sequences (instances of "tuple", "list", or "range") can be compared
only within each of their types, with the restriction that ranges do
not support order comparison. Equality comparison across these
types results in inequality, and ordering comparison across these
types raises "TypeError".
Sequences compare lexicographically using comparison of
corresponding elements. The built-in containers typically assume
identical objects are equal to themselves. That lets them bypass
equality tests for identical objects to improve performance and to
maintain their internal invariants.
Lexicographical comparison between built-in collections works as
follows:
* For two collections to compare equal, they must be of the same
type, have the same length, and each pair of corresponding
elements must compare equal (for example, "[1,2] == (1,2)" is
false because the type is not the same).
* Collections that support order comparison are ordered the same as
their first unequal elements (for example, "[1,2,x] <= [1,2,y]"
has the same value as "x <= y"). If a corresponding element does
not exist, the shorter collection is ordered first (for example,
"[1,2] < [1,2,3]" is true).
* Mappings (instances of "dict") compare equal if and only if they
have equal "(key, value)" pairs. Equality comparison of the keys and
values enforces reflexivity.
Order comparisons ("<", ">", "<=", and ">=") raise "TypeError".
* Sets (instances of "set" or "frozenset") can be compared within and
across their types.
They define order comparison operators to mean subset and superset
tests. Those relations do not define total orderings (for example,
the two sets "{1,2}" and "{2,3}" are not equal, nor subsets of one
another, nor supersets of one another). Accordingly, sets are not
appropriate arguments for functions which depend on total ordering
(for example, "min()", "max()", and "sorted()" produce undefined
results given a list of sets as inputs).
Comparison of sets enforces reflexivity of its elements.
* Most other built-in types have no comparison methods implemented, so
they inherit the default comparison behavior.
User-defined classes that customize their comparison behavior should
follow some consistency rules, if possible:
* Equality comparison should be reflexive. In other words, identical
objects should compare equal:
"x is y" implies "x == y"
* Comparison should be symmetric. In other words, the following
expressions should have the same result:
"x == y" and "y == x"
"x != y" and "y != x"
"x < y" and "y > x"
"x <= y" and "y >= x"
* Comparison should be transitive. The following (non-exhaustive)
examples illustrate that:
"x > y and y > z" implies "x > z"
"x < y and y <= z" implies "x < z"
* Inverse comparison should result in the boolean negation. In other
words, the following expressions should have the same result:
"x == y" and "not x != y"
"x < y" and "not x >= y" (for total ordering)
"x > y" and "not x <= y" (for total ordering)
The last two expressions apply to totally ordered collections (e.g.
to sequences, but not to sets or mappings). See also the
"total_ordering()" decorator.
* The "hash()" result should be consistent with equality. Objects that
are equal should either have the same hash value, or be marked as
unhashable.
Python does not enforce these consistency rules. In fact, the
not-a-number values are an example for not following these rules.
Membership test operations
==========================
The operators "in" and "not in" test for membership. "x in s"
evaluates to "True" if *x* is a member of *s*, and "False" otherwise.
"x not in s" returns the negation of "x in s". All built-in sequences
and set types support this as well as dictionary, for which "in" tests
whether the dictionary has a given key. For container types such as
list, tuple, set, frozenset, dict, or collections.deque, the
expression "x in y" is equivalent to "any(x is e or x == e for e in
y)".
For the string and bytes types, "x in y" is "True" if and only if *x*
is a substring of *y*. An equivalent test is "y.find(x) != -1".
Empty strings are always considered to be a substring of any other
string, so """ in "abc"" will return "True".
For user-defined classes which define the "__contains__()" method, "x
in y" returns "True" if "y.__contains__(x)" returns a true value, and
"False" otherwise.
For user-defined classes which do not define "__contains__()" but do
define "__iter__()", "x in y" is "True" if some value "z", for which
the expression "x is z or x == z" is true, is produced while iterating
over "y". If an exception is raised during the iteration, it is as if
"in" raised that exception.
Lastly, the old-style iteration protocol is tried: if a class defines
"__getitem__()", "x in y" is "True" if and only if there is a non-
negative integer index *i* such that "x is y[i] or x == y[i]", and no
lower integer index raises the "IndexError" exception. (If any other
exception is raised, it is as if "in" raised that exception).
The operator "not in" is defined to have the inverse truth value of
"in".
Identity comparisons
====================
The operators "is" and "is not" test for an object’s identity: "x is
y" is true if and only if *x* and *y* are the same object. An
Object’s identity is determined using the "id()" function. "x is not
y" yields the inverse truth value. [4]
''',
'compound': r'''Compound statements
*******************
Compound statements contain (groups of) other statements; they affect
or control the execution of those other statements in some way. In
general, compound statements span multiple lines, although in simple
incarnations a whole compound statement may be contained in one line.
The "if", "while" and "for" statements implement traditional control
flow constructs. "try" specifies exception handlers and/or cleanup
code for a group of statements, while the "with" statement allows the
execution of initialization and finalization code around a block of
code. Function and class definitions are also syntactically compound
statements.
A compound statement consists of one or more ‘clauses.’ A clause
consists of a header and a ‘suite.’ The clause headers of a
particular compound statement are all at the same indentation level.
Each clause header begins with a uniquely identifying keyword and ends
with a colon. A suite is a group of statements controlled by a
clause. A suite can be one or more semicolon-separated simple
statements on the same line as the header, following the header’s
colon, or it can be one or more indented statements on subsequent
lines. Only the latter form of a suite can contain nested compound
statements; the following is illegal, mostly because it wouldn’t be
clear to which "if" clause a following "else" clause would belong:
if test1: if test2: print(x)
Also note that the semicolon binds tighter than the colon in this
context, so that in the following example, either all or none of the
"print()" calls are executed:
if x < y < z: print(x); print(y); print(z)
Summarizing:
compound_stmt: if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| match_stmt
| funcdef
| classdef
| async_with_stmt
| async_for_stmt
| async_funcdef
suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement: stmt_list NEWLINE | compound_stmt
stmt_list: simple_stmt (";" simple_stmt)* [";"]
Note that statements always end in a "NEWLINE" possibly followed by a
"DEDENT". Also note that optional continuation clauses always begin
with a keyword that cannot start a statement, thus there are no
ambiguities (the ‘dangling "else"’ problem is solved in Python by
requiring nested "if" statements to be indented).
The formatting of the grammar rules in the following sections places
each clause on a separate line for clarity.
The "if" statement
==================
The "if" statement is used for conditional execution:
if_stmt: "if" assignment_expression ":" suite
("elif" assignment_expression ":" suite)*
["else" ":" suite]
It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section Boolean operations
for the definition of true and false); then that suite is executed
(and no other part of the "if" statement is executed or evaluated).
If all expressions are false, the suite of the "else" clause, if
present, is executed.
The "while" statement
=====================
The "while" statement is used for repeated execution as long as an
expression is true:
while_stmt: "while" assignment_expression ":" suite
["else" ":" suite]
This repeatedly tests the expression and, if it is true, executes the
first suite; if the expression is false (which may be the first time
it is tested) the suite of the "else" clause, if present, is executed
and the loop terminates.
A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite. A "continue" statement
executed in the first suite skips the rest of the suite and goes back
to testing the expression.
The "for" statement
===================
The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:
for_stmt: "for" target_list "in" starred_list ":" suite
["else" ":" suite]
The "starred_list" expression is evaluated once; it should yield an
*iterable* object. An *iterator* is created for that iterable. The
first item provided by the iterator is then assigned to the target
list using the standard rules for assignments (see Assignment
statements), and the suite is executed. This repeats for each item
provided by the iterator. When the iterator is exhausted, the suite
in the "else" clause, if present, is executed, and the loop
terminates.
A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite. A "continue" statement
executed in the first suite skips the rest of the suite and continues
with the next item, or with the "else" clause if there is no next
item.
The for-loop makes assignments to the variables in the target list.
This overwrites all previous assignments to those variables including
those made in the suite of the for-loop:
for i in range(10):
print(i)
i = 5 # this will not affect the for-loop
# because i will be overwritten with the next
# index in the range
Names in the target list are not deleted when the loop is finished,
but if the sequence is empty, they will not have been assigned to at
all by the loop. Hint: the built-in type "range()" represents
immutable arithmetic sequences of integers. For instance, iterating
"range(3)" successively yields 0, 1, and then 2.
Changed in version 3.11: Starred elements are now allowed in the
expression list.
The "try" statement
===================
The "try" statement specifies exception handlers and/or cleanup code
for a group of statements:
try_stmt: try1_stmt | try2_stmt | try3_stmt
try1_stmt: "try" ":" suite
("except" [expression ["as" identifier]] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try2_stmt: "try" ":" suite
("except" "*" expression ["as" identifier] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try3_stmt: "try" ":" suite
"finally" ":" suite
Additional information on exceptions can be found in section
Exceptions, and information on using the "raise" statement to generate
exceptions may be found in section The raise statement.
Changed in version 3.14: Support for optionally dropping grouping
parentheses when using multiple exception types. See **PEP 758**.
"except" clause
---------------
The "except" clause(s) specify one or more exception handlers. When no
exception occurs in the "try" clause, no exception handler is
executed. When an exception occurs in the "try" suite, a search for an
exception handler is started. This search inspects the "except"
clauses in turn until one is found that matches the exception. An
expression-less "except" clause, if present, must be last; it matches
any exception.
For an "except" clause with an expression, the expression must
evaluate to an exception type or a tuple of exception types.
Parentheses can be dropped if multiple exception types are provided
and the "as" clause is not used. The raised exception matches an
"except" clause whose expression evaluates to the class or a *non-
virtual base class* of the exception object, or to a tuple that
contains such a class.
If no "except" clause matches the exception, the search for an
exception handler continues in the surrounding code and on the
invocation stack. [1]
If the evaluation of an expression in the header of an "except" clause
raises an exception, the original search for a handler is canceled and
a search starts for the new exception in the surrounding code and on
the call stack (it is treated as if the entire "try" statement raised
the exception).
When a matching "except" clause is found, the exception is assigned to
the target specified after the "as" keyword in that "except" clause,
if present, and the "except" clause’s suite is executed. All "except"
clauses must have an executable block. When the end of this block is
reached, execution continues normally after the entire "try"
statement. (This means that if two nested handlers exist for the same
exception, and the exception occurs in the "try" clause of the inner
handler, the outer handler will not handle the exception.)
When an exception has been assigned using "as target", it is cleared
at the end of the "except" clause. This is as if
except E as N:
foo
was translated to
except E as N:
try:
foo
finally:
del N
This means the exception must be assigned to a different name to be
able to refer to it after the "except" clause. Exceptions are cleared
because with the traceback attached to them, they form a reference
cycle with the stack frame, keeping all locals in that frame alive
until the next garbage collection occurs.
Before an "except" clause’s suite is executed, the exception is stored
in the "sys" module, where it can be accessed from within the body of
the "except" clause by calling "sys.exception()". When leaving an
exception handler, the exception stored in the "sys" module is reset
to its previous value:
>>> print(sys.exception())
None
>>> try:
... raise TypeError
... except:
... print(repr(sys.exception()))
... try:
... raise ValueError
... except:
... print(repr(sys.exception()))
... print(repr(sys.exception()))
...
TypeError()
ValueError()
TypeError()
>>> print(sys.exception())
None
"except*" clause
----------------
The "except*" clause(s) are used for handling "ExceptionGroup"s. The
exception type for matching is interpreted as in the case of "except",
but in the case of exception groups we can have partial matches when
the type matches some of the exceptions in the group. This means that
multiple "except*" clauses can execute, each handling part of the
exception group. Each clause executes at most once and handles an
exception group of all matching exceptions. Each exception in the
group is handled by at most one "except*" clause, the first that
matches it.
>>> try:
... raise ExceptionGroup("eg",
... [ValueError(1), TypeError(2), OSError(3), OSError(4)])
... except* TypeError as e:
... print(f'caught {type(e)} with nested {e.exceptions}')
... except* OSError as e:
... print(f'caught {type(e)} with nested {e.exceptions}')
...
caught <class 'ExceptionGroup'> with nested (TypeError(2),)
caught <class 'ExceptionGroup'> with nested (OSError(3), OSError(4))
+ Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module>
| ExceptionGroup: eg
+-+---------------- 1 ----------------
| ValueError: 1
+------------------------------------
Any remaining exceptions that were not handled by any "except*" clause
are re-raised at the end, along with all exceptions that were raised
from within the "except*" clauses. If this list contains more than one
exception to reraise, they are combined into an exception group.
If the raised exception is not an exception group and its type matches
one of the "except*" clauses, it is caught and wrapped by an exception
group with an empty message string.
>>> try:
... raise BlockingIOError
... except* BlockingIOError as e:
... print(repr(e))
...
ExceptionGroup('', (BlockingIOError()))
An "except*" clause must have a matching expression; it cannot be
"except*:". Furthermore, this expression cannot contain exception
group types, because that would have ambiguous semantics.
It is not possible to mix "except" and "except*" in the same "try".
"break", "continue" and "return" cannot appear in an "except*" clause.
"else" clause
-------------
The optional "else" clause is executed if the control flow leaves the
"try" suite, no exception was raised, and no "return", "continue", or
"break" statement was executed. Exceptions in the "else" clause are
not handled by the preceding "except" clauses.
"finally" clause
----------------
If "finally" is present, it specifies a ‘cleanup’ handler. The "try"
clause is executed, including any "except" and "else" clauses. If an
exception occurs in any of the clauses and is not handled, the
exception is temporarily saved. The "finally" clause is executed. If
there is a saved exception it is re-raised at the end of the "finally"
clause. If the "finally" clause raises another exception, the saved
exception is set as the context of the new exception. If the "finally"
clause executes a "return", "break" or "continue" statement, the saved
exception is discarded. For example, this function returns 42.
def f():
try:
1/0
finally:
return 42
The exception information is not available to the program during
execution of the "finally" clause.
When a "return", "break" or "continue" statement is executed in the
"try" suite of a "try"…"finally" statement, the "finally" clause is
also executed ‘on the way out.’
The return value of a function is determined by the last "return"
statement executed. Since the "finally" clause always executes, a
"return" statement executed in the "finally" clause will always be the
last one executed. The following function returns ‘finally’.
def foo():
try:
return 'try'
finally:
return 'finally'
Changed in version 3.8: Prior to Python 3.8, a "continue" statement
was illegal in the "finally" clause due to a problem with the
implementation.
Changed in version 3.14: The compiler emits a "SyntaxWarning" when a
"return", "break" or "continue" appears in a "finally" block (see
**PEP 765**).
The "with" statement
====================
The "with" statement is used to wrap the execution of a block with
methods defined by a context manager (see section With Statement
Context Managers). This allows common "try"…"except"…"finally" usage
patterns to be encapsulated for convenient reuse.
with_stmt: "with" ( "(" with_stmt_contents ","? ")" | with_stmt_contents ) ":" suite
with_stmt_contents: with_item ("," with_item)*
with_item: expression ["as" target]
The execution of the "with" statement with one “item” proceeds as
follows:
1. The context expression (the expression given in the "with_item") is
evaluated to obtain a context manager.
2. The context manager’s "__enter__()" is loaded for later use.
3. The context manager’s "__exit__()" is loaded for later use.
4. The context manager’s "__enter__()" method is invoked.
5. If a target was included in the "with" statement, the return value
from "__enter__()" is assigned to it.
Note:
The "with" statement guarantees that if the "__enter__()" method
returns without an error, then "__exit__()" will always be
called. Thus, if an error occurs during the assignment to the
target list, it will be treated the same as an error occurring
within the suite would be. See step 7 below.
6. The suite is executed.
7. The context manager’s "__exit__()" method is invoked. If an
exception caused the suite to be exited, its type, value, and
traceback are passed as arguments to "__exit__()". Otherwise, three
"None" arguments are supplied.
If the suite was exited due to an exception, and the return value
from the "__exit__()" method was false, the exception is reraised.
If the return value was true, the exception is suppressed, and
execution continues with the statement following the "with"
statement.
If the suite was exited for any reason other than an exception, the
return value from "__exit__()" is ignored, and execution proceeds
at the normal location for the kind of exit that was taken.
The following code:
with EXPRESSION as TARGET:
SUITE
is semantically equivalent to:
manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager)
hit_except = False
try:
TARGET = value
SUITE
except:
hit_except = True
if not exit(manager, *sys.exc_info()):
raise
finally:
if not hit_except:
exit(manager, None, None, None)
With more than one item, the context managers are processed as if
multiple "with" statements were nested:
with A() as a, B() as b:
SUITE
is semantically equivalent to:
with A() as a:
with B() as b:
SUITE
You can also write multi-item context managers in multiple lines if
the items are surrounded by parentheses. For example:
with (
A() as a,
B() as b,
):
SUITE
Changed in version 3.1: Support for multiple context expressions.
Changed in version 3.10: Support for using grouping parentheses to
break the statement in multiple lines.
See also:
**PEP 343** - The “with” statement
The specification, background, and examples for the Python "with"
statement.
The "match" statement
=====================
Added in version 3.10.
The match statement is used for pattern matching. Syntax:
match_stmt: 'match' subject_expr ":" NEWLINE INDENT case_block+ DEDENT
subject_expr: star_named_expression "," star_named_expressions?
| named_expression
case_block: 'case' patterns [guard] ":" block
Note:
This section uses single quotes to denote soft keywords.
Pattern matching takes a pattern as input (following "case") and a
subject value (following "match"). The pattern (which may contain
subpatterns) is matched against the subject value. The outcomes are:
* A match success or failure (also termed a pattern success or
failure).
* Possible binding of matched values to a name. The prerequisites for
this are further discussed below.
The "match" and "case" keywords are soft keywords.
See also:
* **PEP 634** – Structural Pattern Matching: Specification
* **PEP 636** – Structural Pattern Matching: Tutorial
Overview
--------
Here’s an overview of the logical flow of a match statement:
1. The subject expression "subject_expr" is evaluated and a resulting
subject value obtained. If the subject expression contains a comma,
a tuple is constructed using the standard rules.
2. Each pattern in a "case_block" is attempted to match with the
subject value. The specific rules for success or failure are
described below. The match attempt can also bind some or all of the
standalone names within the pattern. The precise pattern binding
rules vary per pattern type and are specified below. **Name
bindings made during a successful pattern match outlive the
executed block and can be used after the match statement**.
Note:
During failed pattern matches, some subpatterns may succeed. Do
not rely on bindings being made for a failed match. Conversely,
do not rely on variables remaining unchanged after a failed
match. The exact behavior is dependent on implementation and may
vary. This is an intentional decision made to allow different
implementations to add optimizations.
3. If the pattern succeeds, the corresponding guard (if present) is
evaluated. In this case all name bindings are guaranteed to have
happened.
* If the guard evaluates as true or is missing, the "block" inside
"case_block" is executed.
* Otherwise, the next "case_block" is attempted as described above.
* If there are no further case blocks, the match statement is
completed.
Note:
Users should generally never rely on a pattern being evaluated.
Depending on implementation, the interpreter may cache values or use
other optimizations which skip repeated evaluations.
A sample match statement:
>>> flag = False
>>> match (100, 200):
... case (100, 300): # Mismatch: 200 != 300
... print('Case 1')
... case (100, 200) if flag: # Successful match, but guard fails
... print('Case 2')
... case (100, y): # Matches and binds y to 200
... print(f'Case 3, y: {y}')
... case _: # Pattern not attempted
... print('Case 4, I match anything!')
...
Case 3, y: 200
In this case, "if flag" is a guard. Read more about that in the next
section.
Guards
------
guard: "if" named_expression
A "guard" (which is part of the "case") must succeed for code inside
the "case" block to execute. It takes the form: "if" followed by an
expression.
The logical flow of a "case" block with a "guard" follows:
1. Check that the pattern in the "case" block succeeded. If the
pattern failed, the "guard" is not evaluated and the next "case"
block is checked.
2. If the pattern succeeded, evaluate the "guard".
* If the "guard" condition evaluates as true, the case block is
selected.
* If the "guard" condition evaluates as false, the case block is
not selected.
* If the "guard" raises an exception during evaluation, the
exception bubbles up.
Guards are allowed to have side effects as they are expressions.
Guard evaluation must proceed from the first to the last case block,
one at a time, skipping case blocks whose pattern(s) don’t all
succeed. (I.e., guard evaluation must happen in order.) Guard
evaluation must stop once a case block is selected.
Irrefutable Case Blocks
-----------------------
An irrefutable case block is a match-all case block. A match
statement may have at most one irrefutable case block, and it must be
last.
A case block is considered irrefutable if it has no guard and its
pattern is irrefutable. A pattern is considered irrefutable if we can
prove from its syntax alone that it will always succeed. Only the
following patterns are irrefutable:
* AS Patterns whose left-hand side is irrefutable
* OR Patterns containing at least one irrefutable pattern
* Capture Patterns
* Wildcard Patterns
* parenthesized irrefutable patterns
Patterns
--------
Note:
This section uses grammar notations beyond standard EBNF:
* the notation "SEP.RULE+" is shorthand for "RULE (SEP RULE)*"
* the notation "!RULE" is shorthand for a negative lookahead
assertion
The top-level syntax for "patterns" is:
patterns: open_sequence_pattern | pattern
pattern: as_pattern | or_pattern
closed_pattern: | literal_pattern
| capture_pattern
| wildcard_pattern
| value_pattern
| group_pattern
| sequence_pattern
| mapping_pattern
| class_pattern
The descriptions below will include a description “in simple terms” of
what a pattern does for illustration purposes (credits to Raymond
Hettinger for a document that inspired most of the descriptions). Note
that these descriptions are purely for illustration purposes and **may
not** reflect the underlying implementation. Furthermore, they do not
cover all valid forms.
OR Patterns
~~~~~~~~~~~
An OR pattern is two or more patterns separated by vertical bars "|".
Syntax:
or_pattern: "|".closed_pattern+
Only the final subpattern may be irrefutable, and each subpattern must
bind the same set of names to avoid ambiguity.
An OR pattern matches each of its subpatterns in turn to the subject
value, until one succeeds. The OR pattern is then considered
successful. Otherwise, if none of the subpatterns succeed, the OR
pattern fails.
In simple terms, "P1 | P2 | ..." will try to match "P1", if it fails
it will try to match "P2", succeeding immediately if any succeeds,
failing otherwise.
AS Patterns
~~~~~~~~~~~
An AS pattern matches an OR pattern on the left of the "as" keyword
against a subject. Syntax:
as_pattern: or_pattern "as" capture_pattern
If the OR pattern fails, the AS pattern fails. Otherwise, the AS
pattern binds the subject to the name on the right of the as keyword
and succeeds. "capture_pattern" cannot be a "_".
In simple terms "P as NAME" will match with "P", and on success it
will set "NAME = <subject>".
Literal Patterns
~~~~~~~~~~~~~~~~
A literal pattern corresponds to most literals in Python. Syntax:
literal_pattern: signed_number
| signed_number "+" NUMBER
| signed_number "-" NUMBER
| strings
| "None"
| "True"
| "False"
signed_number: ["-"] NUMBER
The rule "strings" and the token "NUMBER" are defined in the standard
Python grammar. Triple-quoted strings are supported. Raw strings and
byte strings are supported. f-strings are not supported.
The forms "signed_number '+' NUMBER" and "signed_number '-' NUMBER"
are for expressing complex numbers; they require a real number on the
left and an imaginary number on the right. E.g. "3 + 4j".
In simple terms, "LITERAL" will succeed only if "<subject> ==
LITERAL". For the singletons "None", "True" and "False", the "is"
operator is used.
Capture Patterns
~~~~~~~~~~~~~~~~
A capture pattern binds the subject value to a name. Syntax:
capture_pattern: !'_' NAME
A single underscore "_" is not a capture pattern (this is what "!'_'"
expresses). It is instead treated as a "wildcard_pattern".
In a given pattern, a given name can only be bound once. E.g. "case
x, x: ..." is invalid while "case [x] | x: ..." is allowed.
Capture patterns always succeed. The binding follows scoping rules
established by the assignment expression operator in **PEP 572**; the
name becomes a local variable in the closest containing function scope
unless there’s an applicable "global" or "nonlocal" statement.
In simple terms "NAME" will always succeed and it will set "NAME =
<subject>".
Wildcard Patterns
~~~~~~~~~~~~~~~~~
A wildcard pattern always succeeds (matches anything) and binds no
name. Syntax:
wildcard_pattern: '_'
"_" is a soft keyword within any pattern, but only within patterns.
It is an identifier, as usual, even within "match" subject
expressions, "guard"s, and "case" blocks.
In simple terms, "_" will always succeed.
Value Patterns
~~~~~~~~~~~~~~
A value pattern represents a named value in Python. Syntax:
value_pattern: attr
attr: name_or_attr "." NAME
name_or_attr: attr | NAME
The dotted name in the pattern is looked up using standard Python name
resolution rules. The pattern succeeds if the value found compares
equal to the subject value (using the "==" equality operator).
In simple terms "NAME1.NAME2" will succeed only if "<subject> ==
NAME1.NAME2"
Note:
If the same value occurs multiple times in the same match statement,
the interpreter may cache the first value found and reuse it rather
than repeat the same lookup. This cache is strictly tied to a given
execution of a given match statement.
Group Patterns
~~~~~~~~~~~~~~
A group pattern allows users to add parentheses around patterns to
emphasize the intended grouping. Otherwise, it has no additional
syntax. Syntax:
group_pattern: "(" pattern ")"
In simple terms "(P)" has the same effect as "P".
Sequence Patterns
~~~~~~~~~~~~~~~~~
A sequence pattern contains several subpatterns to be matched against
sequence elements. The syntax is similar to the unpacking of a list or
tuple.
sequence_pattern: "[" [maybe_sequence_pattern] "]"
| "(" [open_sequence_pattern] ")"
open_sequence_pattern: maybe_star_pattern "," [maybe_sequence_pattern]
maybe_sequence_pattern: ",".maybe_star_pattern+ ","?
maybe_star_pattern: star_pattern | pattern
star_pattern: "*" (capture_pattern | wildcard_pattern)
There is no difference if parentheses or square brackets are used for
sequence patterns (i.e. "(...)" vs "[...]" ).
Note:
A single pattern enclosed in parentheses without a trailing comma
(e.g. "(3 | 4)") is a group pattern. While a single pattern enclosed
in square brackets (e.g. "[3 | 4]") is still a sequence pattern.
At most one star subpattern may be in a sequence pattern. The star
subpattern may occur in any position. If no star subpattern is
present, the sequence pattern is a fixed-length sequence pattern;
otherwise it is a variable-length sequence pattern.
The following is the logical flow for matching a sequence pattern
against a subject value:
1. If the subject value is not a sequence [2], the sequence pattern
fails.
2. If the subject value is an instance of "str", "bytes" or
"bytearray" the sequence pattern fails.
3. The subsequent steps depend on whether the sequence pattern is
fixed or variable-length.
If the sequence pattern is fixed-length:
1. If the length of the subject sequence is not equal to the number
of subpatterns, the sequence pattern fails
2. Subpatterns in the sequence pattern are matched to their
corresponding items in the subject sequence from left to right.
Matching stops as soon as a subpattern fails. If all
subpatterns succeed in matching their corresponding item, the
sequence pattern succeeds.
Otherwise, if the sequence pattern is variable-length:
1. If the length of the subject sequence is less than the number of
non-star subpatterns, the sequence pattern fails.
2. The leading non-star subpatterns are matched to their
corresponding items as for fixed-length sequences.
3. If the previous step succeeds, the star subpattern matches a
list formed of the remaining subject items, excluding the
remaining items corresponding to non-star subpatterns following
the star subpattern.
4. Remaining non-star subpatterns are matched to their
corresponding subject items, as for a fixed-length sequence.
Note:
The length of the subject sequence is obtained via "len()" (i.e.
via the "__len__()" protocol). This length may be cached by the
interpreter in a similar manner as value patterns.
In simple terms "[P1, P2, P3," … ", P<N>]" matches only if all the
following happens:
* check "<subject>" is a sequence
* "len(subject) == <N>"
* "P1" matches "<subject>[0]" (note that this match can also bind
names)
* "P2" matches "<subject>[1]" (note that this match can also bind
names)
* … and so on for the corresponding pattern/element.
Mapping Patterns
~~~~~~~~~~~~~~~~
A mapping pattern contains one or more key-value patterns. The syntax
is similar to the construction of a dictionary. Syntax:
mapping_pattern: "{" [items_pattern] "}"
items_pattern: ",".key_value_pattern+ ","?
key_value_pattern: (literal_pattern | value_pattern) ":" pattern
| double_star_pattern
double_star_pattern: "**" capture_pattern
At most one double star pattern may be in a mapping pattern. The
double star pattern must be the last subpattern in the mapping
pattern.
Duplicate keys in mapping patterns are disallowed. Duplicate literal
keys will raise a "SyntaxError". Two keys that otherwise have the same
value will raise a "ValueError" at runtime.
The following is the logical flow for matching a mapping pattern
against a subject value:
1. If the subject value is not a mapping [3],the mapping pattern
fails.
2. If every key given in the mapping pattern is present in the subject
mapping, and the pattern for each key matches the corresponding
item of the subject mapping, the mapping pattern succeeds.
3. If duplicate keys are detected in the mapping pattern, the pattern
is considered invalid. A "SyntaxError" is raised for duplicate
literal values; or a "ValueError" for named keys of the same value.
Note:
Key-value pairs are matched using the two-argument form of the
mapping subject’s "get()" method. Matched key-value pairs must
already be present in the mapping, and not created on-the-fly via
"__missing__()" or "__getitem__()".
In simple terms "{KEY1: P1, KEY2: P2, ... }" matches only if all the
following happens:
* check "<subject>" is a mapping
* "KEY1 in <subject>"
* "P1" matches "<subject>[KEY1]"
* … and so on for the corresponding KEY/pattern pair.
Class Patterns
~~~~~~~~~~~~~~
A class pattern represents a class and its positional and keyword
arguments (if any). Syntax:
class_pattern: name_or_attr "(" [pattern_arguments ","?] ")"
pattern_arguments: positional_patterns ["," keyword_patterns]
| keyword_patterns
positional_patterns: ",".pattern+
keyword_patterns: ",".keyword_pattern+
keyword_pattern: NAME "=" pattern
The same keyword should not be repeated in class patterns.
The following is the logical flow for matching a class pattern against
a subject value:
1. If "name_or_attr" is not an instance of the builtin "type" , raise
"TypeError".
2. If the subject value is not an instance of "name_or_attr" (tested
via "isinstance()"), the class pattern fails.
3. If no pattern arguments are present, the pattern succeeds.
Otherwise, the subsequent steps depend on whether keyword or
positional argument patterns are present.
For a number of built-in types (specified below), a single
positional subpattern is accepted which will match the entire
subject; for these types keyword patterns also work as for other
types.
If only keyword patterns are present, they are processed as
follows, one by one:
I. The keyword is looked up as an attribute on the subject.
* If this raises an exception other than "AttributeError", the
exception bubbles up.
* If this raises "AttributeError", the class pattern has failed.
* Else, the subpattern associated with the keyword pattern is
matched against the subject’s attribute value. If this fails,
the class pattern fails; if this succeeds, the match proceeds
to the next keyword.
II. If all keyword patterns succeed, the class pattern succeeds.
If any positional patterns are present, they are converted to
keyword patterns using the "__match_args__" attribute on the class
"name_or_attr" before matching:
I. The equivalent of "getattr(cls, "__match_args__", ())" is
called.
* If this raises an exception, the exception bubbles up.
* If the returned value is not a tuple, the conversion fails and
"TypeError" is raised.
* If there are more positional patterns than
"len(cls.__match_args__)", "TypeError" is raised.
* Otherwise, positional pattern "i" is converted to a keyword
pattern using "__match_args__[i]" as the keyword.
"__match_args__[i]" must be a string; if not "TypeError" is
raised.
* If there are duplicate keywords, "TypeError" is raised.
See also:
Customizing positional arguments in class pattern matching
II. Once all positional patterns have been converted to keyword
patterns,
the match proceeds as if there were only keyword patterns.
For the following built-in types the handling of positional
subpatterns is different:
* "bool"
* "bytearray"
* "bytes"
* "dict"
* "float"
* "frozenset"
* "int"
* "list"
* "set"
* "str"
* "tuple"
These classes accept a single positional argument, and the pattern
there is matched against the whole object rather than an attribute.
For example "int(0|1)" matches the value "0", but not the value
"0.0".
In simple terms "CLS(P1, attr=P2)" matches only if the following
happens:
* "isinstance(<subject>, CLS)"
* convert "P1" to a keyword pattern using "CLS.__match_args__"
* For each keyword argument "attr=P2":
* "hasattr(<subject>, "attr")"
* "P2" matches "<subject>.attr"
* … and so on for the corresponding keyword argument/pattern pair.
See also:
* **PEP 634** – Structural Pattern Matching: Specification
* **PEP 636** – Structural Pattern Matching: Tutorial
Function definitions
====================
A function definition defines a user-defined function object (see
section The standard type hierarchy):
funcdef: [decorators] "def" funcname [type_params] "(" [parameter_list] ")"
["->" expression] ":" suite
decorators: decorator+
decorator: "@" assignment_expression NEWLINE
parameter_list: defparameter ("," defparameter)* "," "/" ["," [parameter_list_no_posonly]]
| parameter_list_no_posonly
parameter_list_no_posonly: defparameter ("," defparameter)* ["," [parameter_list_starargs]]
| parameter_list_starargs
parameter_list_starargs: "*" [star_parameter] ("," defparameter)* ["," [parameter_star_kwargs]]
| "*" ("," defparameter)+ ["," [parameter_star_kwargs]]
| parameter_star_kwargs
parameter_star_kwargs: "**" parameter [","]
parameter: identifier [":" expression]
star_parameter: identifier [":" ["*"] expression]
defparameter: parameter ["=" expression]
funcname: identifier
A function definition is an executable statement. Its execution binds
the function name in the current local namespace to a function object
(a wrapper around the executable code for the function). This
function object contains a reference to the current global namespace
as the global namespace to be used when the function is called.
The function definition does not execute the function body; this gets
executed only when the function is called. [4]
A function definition may be wrapped by one or more *decorator*
expressions. Decorator expressions are evaluated when the function is
defined, in the scope that contains the function definition. The
result must be a callable, which is invoked with the function object
as the only argument. The returned value is bound to the function name
instead of the function object. Multiple decorators are applied in
nested fashion. For example, the following code
@f1(arg)
@f2
def func(): pass
is roughly equivalent to
def func(): pass
func = f1(arg)(f2(func))
except that the original function is not temporarily bound to the name
"func".
Changed in version 3.9: Functions may be decorated with any valid
"assignment_expression". Previously, the grammar was much more
restrictive; see **PEP 614** for details.
A list of type parameters may be given in square brackets between the
function’s name and the opening parenthesis for its parameter list.
This indicates to static type checkers that the function is generic.
At runtime, the type parameters can be retrieved from the function’s
"__type_params__" attribute. See Generic functions for more.
Changed in version 3.12: Type parameter lists are new in Python 3.12.
When one or more *parameters* have the form *parameter* "="
*expression*, the function is said to have “default parameter values.”
For a parameter with a default value, the corresponding *argument* may
be omitted from a call, in which case the parameter’s default value is
substituted. If a parameter has a default value, all following
parameters up until the “"*"” must also have a default value — this is
a syntactic restriction that is not expressed by the grammar.
**Default parameter values are evaluated from left to right when the
function definition is executed.** This means that the expression is
evaluated once, when the function is defined, and that the same “pre-
computed” value is used for each call. This is especially important
to understand when a default parameter value is a mutable object, such
as a list or a dictionary: if the function modifies the object (e.g.
by appending an item to a list), the default parameter value is in
effect modified. This is generally not what was intended. A way
around this is to use "None" as the default, and explicitly test for
it in the body of the function, e.g.:
def whats_on_the_telly(penguin=None):
if penguin is None:
penguin = []
penguin.append("property of the zoo")
return penguin
Function call semantics are described in more detail in section Calls.
A function call always assigns values to all parameters mentioned in
the parameter list, either from positional arguments, from keyword
arguments, or from default values. If the form “"*identifier"” is
present, it is initialized to a tuple receiving any excess positional
parameters, defaulting to the empty tuple. If the form
“"**identifier"” is present, it is initialized to a new ordered
mapping receiving any excess keyword arguments, defaulting to a new
empty mapping of the same type. Parameters after “"*"” or
“"*identifier"” are keyword-only parameters and may only be passed by
keyword arguments. Parameters before “"/"” are positional-only
parameters and may only be passed by positional arguments.
Changed in version 3.8: The "/" function parameter syntax may be used
to indicate positional-only parameters. See **PEP 570** for details.
Parameters may have an *annotation* of the form “": expression"”
following the parameter name. Any parameter may have an annotation,
even those of the form "*identifier" or "**identifier". (As a special
case, parameters of the form "*identifier" may have an annotation “":
*expression"”.) Functions may have “return” annotation of the form
“"-> expression"” after the parameter list. These annotations can be
any valid Python expression. The presence of annotations does not
change the semantics of a function. See Annotations for more
information on annotations.
Changed in version 3.11: Parameters of the form “"*identifier"” may
have an annotation “": *expression"”. See **PEP 646**.
It is also possible to create anonymous functions (functions not bound
to a name), for immediate use in expressions. This uses lambda
expressions, described in section Lambdas. Note that the lambda
expression is merely a shorthand for a simplified function definition;
a function defined in a “"def"” statement can be passed around or
assigned to another name just like a function defined by a lambda
expression. The “"def"” form is actually more powerful since it
allows the execution of multiple statements and annotations.
**Programmer’s note:** Functions are first-class objects. A “"def"”
statement executed inside a function definition defines a local
function that can be returned or passed around. Free variables used
in the nested function can access the local variables of the function
containing the def. See section Naming and binding for details.
See also:
**PEP 3107** - Function Annotations
The original specification for function annotations.
**PEP 484** - Type Hints
Definition of a standard meaning for annotations: type hints.
**PEP 526** - Syntax for Variable Annotations
Ability to type hint variable declarations, including class
variables and instance variables.
**PEP 563** - Postponed Evaluation of Annotations
Support for forward references within annotations by preserving
annotations in a string form at runtime instead of eager
evaluation.
**PEP 318** - Decorators for Functions and Methods
Function and method decorators were introduced. Class decorators
were introduced in **PEP 3129**.
Class definitions
=================
A class definition defines a class object (see section The standard
type hierarchy):
classdef: [decorators] "class" classname [type_params] [inheritance] ":" suite
inheritance: "(" [argument_list] ")"
classname: identifier
A class definition is an executable statement. The inheritance list
usually gives a list of base classes (see Metaclasses for more
advanced uses), so each item in the list should evaluate to a class
object which allows subclassing. Classes without an inheritance list
inherit, by default, from the base class "object"; hence,
class Foo:
pass
is equivalent to
class Foo(object):
pass
The class’s suite is then executed in a new execution frame (see
Naming and binding), using a newly created local namespace and the
original global namespace. (Usually, the suite contains mostly
function definitions.) When the class’s suite finishes execution, its
execution frame is discarded but its local namespace is saved. [5] A
class object is then created using the inheritance list for the base
classes and the saved local namespace for the attribute dictionary.
The class name is bound to this class object in the original local
namespace.
The order in which attributes are defined in the class body is
preserved in the new class’s "__dict__". Note that this is reliable
only right after the class is created and only for classes that were
defined using the definition syntax.
Class creation can be customized heavily using metaclasses.
Classes can also be decorated: just like when decorating functions,
@f1(arg)
@f2
class Foo: pass
is roughly equivalent to
class Foo: pass
Foo = f1(arg)(f2(Foo))
The evaluation rules for the decorator expressions are the same as for
function decorators. The result is then bound to the class name.
Changed in version 3.9: Classes may be decorated with any valid
"assignment_expression". Previously, the grammar was much more
restrictive; see **PEP 614** for details.
A list of type parameters may be given in square brackets immediately
after the class’s name. This indicates to static type checkers that
the class is generic. At runtime, the type parameters can be retrieved
from the class’s "__type_params__" attribute. See Generic classes for
more.
Changed in version 3.12: Type parameter lists are new in Python 3.12.
**Programmer’s note:** Variables defined in the class definition are
class attributes; they are shared by instances. Instance attributes
can be set in a method with "self.name = value". Both class and
instance attributes are accessible through the notation “"self.name"”,
and an instance attribute hides a class attribute with the same name
when accessed in this way. Class attributes can be used as defaults
for instance attributes, but using mutable values there can lead to
unexpected results. Descriptors can be used to create instance
variables with different implementation details.
See also:
**PEP 3115** - Metaclasses in Python 3000
The proposal that changed the declaration of metaclasses to the
current syntax, and the semantics for how classes with
metaclasses are constructed.
**PEP 3129** - Class Decorators
The proposal that added class decorators. Function and method
decorators were introduced in **PEP 318**.
Coroutines
==========
Added in version 3.5.
Coroutine function definition
-----------------------------
async_funcdef: [decorators] "async" "def" funcname "(" [parameter_list] ")"
["->" expression] ":" suite
Execution of Python coroutines can be suspended and resumed at many
points (see *coroutine*). "await" expressions, "async for" and "async
with" can only be used in the body of a coroutine function.
Functions defined with "async def" syntax are always coroutine
functions, even if they do not contain "await" or "async" keywords.
It is a "SyntaxError" to use a "yield from" expression inside the body
of a coroutine function.
An example of a coroutine function:
async def func(param1, param2):
do_stuff()
await some_coroutine()
Changed in version 3.7: "await" and "async" are now keywords;
previously they were only treated as such inside the body of a
coroutine function.
The "async for" statement
-------------------------
async_for_stmt: "async" for_stmt
An *asynchronous iterable* provides an "__aiter__" method that
directly returns an *asynchronous iterator*, which can call
asynchronous code in its "__anext__" method.
The "async for" statement allows convenient iteration over
asynchronous iterables.
The following code:
async for TARGET in ITER:
SUITE
else:
SUITE2
Is semantically equivalent to:
iter = (ITER)
iter = type(iter).__aiter__(iter)
running = True
while running:
try:
TARGET = await type(iter).__anext__(iter)
except StopAsyncIteration:
running = False
else:
SUITE
else:
SUITE2
See also "__aiter__()" and "__anext__()" for details.
It is a "SyntaxError" to use an "async for" statement outside the body
of a coroutine function.
The "async with" statement
--------------------------
async_with_stmt: "async" with_stmt
An *asynchronous context manager* is a *context manager* that is able
to suspend execution in its *enter* and *exit* methods.
The following code:
async with EXPRESSION as TARGET:
SUITE
is semantically equivalent to:
manager = (EXPRESSION)
aenter = type(manager).__aenter__
aexit = type(manager).__aexit__
value = await aenter(manager)
hit_except = False
try:
TARGET = value
SUITE
except:
hit_except = True
if not await aexit(manager, *sys.exc_info()):
raise
finally:
if not hit_except:
await aexit(manager, None, None, None)
See also "__aenter__()" and "__aexit__()" for details.
It is a "SyntaxError" to use an "async with" statement outside the
body of a coroutine function.
See also:
**PEP 492** - Coroutines with async and await syntax
The proposal that made coroutines a proper standalone concept in
Python, and added supporting syntax.
Type parameter lists
====================
Added in version 3.12.
Changed in version 3.13: Support for default values was added (see
**PEP 696**).
type_params: "[" type_param ("," type_param)* "]"
type_param: typevar | typevartuple | paramspec
typevar: identifier (":" expression)? ("=" expression)?
typevartuple: "*" identifier ("=" expression)?
paramspec: "**" identifier ("=" expression)?
Functions (including coroutines), classes and type aliases may contain
a type parameter list:
def max[T](args: list[T]) -> T:
...
async def amax[T](args: list[T]) -> T:
...
class Bag[T]:
def __iter__(self) -> Iterator[T]:
...
def add(self, arg: T) -> None:
...
type ListOrSet[T] = list[T] | set[T]
Semantically, this indicates that the function, class, or type alias
is generic over a type variable. This information is primarily used by
static type checkers, and at runtime, generic objects behave much like
their non-generic counterparts.
Type parameters are declared in square brackets ("[]") immediately
after the name of the function, class, or type alias. The type
parameters are accessible within the scope of the generic object, but
not elsewhere. Thus, after a declaration "def func[T](): pass", the
name "T" is not available in the module scope. Below, the semantics of
generic objects are described with more precision. The scope of type
parameters is modeled with a special function (technically, an
annotation scope) that wraps the creation of the generic object.
Generic functions, classes, and type aliases have a "__type_params__"
attribute listing their type parameters.
Type parameters come in three kinds:
* "typing.TypeVar", introduced by a plain name (e.g., "T").
Semantically, this represents a single type to a type checker.
* "typing.TypeVarTuple", introduced by a name prefixed with a single
asterisk (e.g., "*Ts"). Semantically, this stands for a tuple of any
number of types.
* "typing.ParamSpec", introduced by a name prefixed with two asterisks
(e.g., "**P"). Semantically, this stands for the parameters of a
callable.
"typing.TypeVar" declarations can define *bounds* and *constraints*
with a colon (":") followed by an expression. A single expression
after the colon indicates a bound (e.g. "T: int"). Semantically, this
means that the "typing.TypeVar" can only represent types that are a
subtype of this bound. A parenthesized tuple of expressions after the
colon indicates a set of constraints (e.g. "T: (str, bytes)"). Each
member of the tuple should be a type (again, this is not enforced at
runtime). Constrained type variables can only take on one of the types
in the list of constraints.
For "typing.TypeVar"s declared using the type parameter list syntax,
the bound and constraints are not evaluated when the generic object is
created, but only when the value is explicitly accessed through the
attributes "__bound__" and "__constraints__". To accomplish this, the
bounds or constraints are evaluated in a separate annotation scope.
"typing.TypeVarTuple"s and "typing.ParamSpec"s cannot have bounds or
constraints.
All three flavors of type parameters can also have a *default value*,
which is used when the type parameter is not explicitly provided. This
is added by appending a single equals sign ("=") followed by an
expression. Like the bounds and constraints of type variables, the
default value is not evaluated when the object is created, but only
when the type parameter’s "__default__" attribute is accessed. To this
end, the default value is evaluated in a separate annotation scope. If
no default value is specified for a type parameter, the "__default__"
attribute is set to the special sentinel object "typing.NoDefault".
The following example indicates the full set of allowed type parameter
declarations:
def overly_generic[
SimpleTypeVar,
TypeVarWithDefault = int,
TypeVarWithBound: int,
TypeVarWithConstraints: (str, bytes),
*SimpleTypeVarTuple = (int, float),
**SimpleParamSpec = (str, bytearray),
](
a: SimpleTypeVar,
b: TypeVarWithDefault,
c: TypeVarWithBound,
d: Callable[SimpleParamSpec, TypeVarWithConstraints],
*e: SimpleTypeVarTuple,
): ...
Generic functions
-----------------
Generic functions are declared as follows:
def func[T](arg: T): ...
This syntax is equivalent to:
annotation-def TYPE_PARAMS_OF_func():
T = typing.TypeVar("T")
def func(arg: T): ...
func.__type_params__ = (T,)
return func
func = TYPE_PARAMS_OF_func()
Here "annotation-def" indicates an annotation scope, which is not
actually bound to any name at runtime. (One other liberty is taken in
the translation: the syntax does not go through attribute access on
the "typing" module, but creates an instance of "typing.TypeVar"
directly.)
The annotations of generic functions are evaluated within the
annotation scope used for declaring the type parameters, but the
function’s defaults and decorators are not.
The following example illustrates the scoping rules for these cases,
as well as for additional flavors of type parameters:
@decorator
def func[T: int, *Ts, **P](*args: *Ts, arg: Callable[P, T] = some_default):
...
Except for the lazy evaluation of the "TypeVar" bound, this is
equivalent to:
DEFAULT_OF_arg = some_default
annotation-def TYPE_PARAMS_OF_func():
annotation-def BOUND_OF_T():
return int
# In reality, BOUND_OF_T() is evaluated only on demand.
T = typing.TypeVar("T", bound=BOUND_OF_T())
Ts = typing.TypeVarTuple("Ts")
P = typing.ParamSpec("P")
def func(*args: *Ts, arg: Callable[P, T] = DEFAULT_OF_arg):
...
func.__type_params__ = (T, Ts, P)
return func
func = decorator(TYPE_PARAMS_OF_func())
The capitalized names like "DEFAULT_OF_arg" are not actually bound at
runtime.
Generic classes
---------------
Generic classes are declared as follows:
class Bag[T]: ...
This syntax is equivalent to:
annotation-def TYPE_PARAMS_OF_Bag():
T = typing.TypeVar("T")
class Bag(typing.Generic[T]):
__type_params__ = (T,)
...
return Bag
Bag = TYPE_PARAMS_OF_Bag()
Here again "annotation-def" (not a real keyword) indicates an
annotation scope, and the name "TYPE_PARAMS_OF_Bag" is not actually
bound at runtime.
Generic classes implicitly inherit from "typing.Generic". The base
classes and keyword arguments of generic classes are evaluated within
the type scope for the type parameters, and decorators are evaluated
outside that scope. This is illustrated by this example:
@decorator
class Bag(Base[T], arg=T): ...
This is equivalent to:
annotation-def TYPE_PARAMS_OF_Bag():
T = typing.TypeVar("T")
class Bag(Base[T], typing.Generic[T], arg=T):
__type_params__ = (T,)
...
return Bag
Bag = decorator(TYPE_PARAMS_OF_Bag())
Generic type aliases
--------------------
The "type" statement can also be used to create a generic type alias:
type ListOrSet[T] = list[T] | set[T]
Except for the lazy evaluation of the value, this is equivalent to:
annotation-def TYPE_PARAMS_OF_ListOrSet():
T = typing.TypeVar("T")
annotation-def VALUE_OF_ListOrSet():
return list[T] | set[T]
# In reality, the value is lazily evaluated
return typing.TypeAliasType("ListOrSet", VALUE_OF_ListOrSet(), type_params=(T,))
ListOrSet = TYPE_PARAMS_OF_ListOrSet()
Here, "annotation-def" (not a real keyword) indicates an annotation
scope. The capitalized names like "TYPE_PARAMS_OF_ListOrSet" are not
actually bound at runtime.
Annotations
===========
Changed in version 3.14: Annotations are now lazily evaluated by
default.
Variables and function parameters may carry *annotations*, created by
adding a colon after the name, followed by an expression:
x: annotation = 1
def f(param: annotation): ...
Functions may also carry a return annotation following an arrow:
def f() -> annotation: ...
Annotations are conventionally used for *type hints*, but this is not
enforced by the language, and in general annotations may contain
arbitrary expressions. The presence of annotations does not change the
runtime semantics of the code, except if some mechanism is used that
introspects and uses the annotations (such as "dataclasses" or
"functools.singledispatch()").
By default, annotations are lazily evaluated in a annotation scope.
This means that they are not evaluated when the code containing the
annotation is evaluated. Instead, the interpreter saves information
that can be used to evaluate the annotation later if requested. The
"annotationlib" module provides tools for evaluating annotations.
If the future statement "from __future__ import annotations" is
present, all annotations are instead stored as strings:
>>> from __future__ import annotations
>>> def f(param: annotation): ...
>>> f.__annotations__
{'param': 'annotation'}
-[ Footnotes ]-
[1] The exception is propagated to the invocation stack unless there
is a "finally" clause which happens to raise another exception.
That new exception causes the old one to be lost.
[2] In pattern matching, a sequence is defined as one of the
following:
* a class that inherits from "collections.abc.Sequence"
* a Python class that has been registered as
"collections.abc.Sequence"
* a builtin class that has its (CPython) "Py_TPFLAGS_SEQUENCE" bit
set
* a class that inherits from any of the above
The following standard library classes are sequences:
* "array.array"
* "collections.deque"
* "list"
* "memoryview"
* "range"
* "tuple"
Note:
Subject values of type "str", "bytes", and "bytearray" do not
match sequence patterns.
[3] In pattern matching, a mapping is defined as one of the following:
* a class that inherits from "collections.abc.Mapping"
* a Python class that has been registered as
"collections.abc.Mapping"
* a builtin class that has its (CPython) "Py_TPFLAGS_MAPPING" bit
set
* a class that inherits from any of the above
The standard library classes "dict" and "types.MappingProxyType"
are mappings.
[4] A string literal appearing as the first statement in the function
body is transformed into the function’s "__doc__" attribute and
therefore the function’s *docstring*.
[5] A string literal appearing as the first statement in the class
body is transformed into the namespace’s "__doc__" item and
therefore the class’s *docstring*.
''',
'context-managers': r'''With Statement Context Managers
*******************************
A *context manager* is an object that defines the runtime context to
be established when executing a "with" statement. The context manager
handles the entry into, and the exit from, the desired runtime context
for the execution of the block of code. Context managers are normally
invoked using the "with" statement (described in section The with
statement), but can also be used by directly invoking their methods.
Typical uses of context managers include saving and restoring various
kinds of global state, locking and unlocking resources, closing opened
files, etc.
For more information on context managers, see Context Manager Types.
The "object" class itself does not provide the context manager
methods.
object.__enter__(self)
Enter the runtime context related to this object. The "with"
statement will bind this method’s return value to the target(s)
specified in the "as" clause of the statement, if any.
object.__exit__(self, exc_type, exc_value, traceback)
Exit the runtime context related to this object. The parameters
describe the exception that caused the context to be exited. If the
context was exited without an exception, all three arguments will
be "None".
If an exception is supplied, and the method wishes to suppress the
exception (i.e., prevent it from being propagated), it should
return a true value. Otherwise, the exception will be processed
normally upon exit from this method.
Note that "__exit__()" methods should not reraise the passed-in
exception; this is the caller’s responsibility.
See also:
**PEP 343** - The “with” statement
The specification, background, and examples for the Python "with"
statement.
''',
'continue': r'''The "continue" statement
************************
continue_stmt: "continue"
"continue" may only occur syntactically nested in a "for" or "while"
loop, but not nested in a function or class definition within that
loop. It continues with the next cycle of the nearest enclosing loop.
When "continue" passes control out of a "try" statement with a
"finally" clause, that "finally" clause is executed before really
starting the next loop cycle.
''',
'conversions': r'''Arithmetic conversions
**********************
When a description of an arithmetic operator below uses the phrase
“the numeric arguments are converted to a common real type”, this
means that the operator implementation for built-in types works as
follows:
* If both arguments are complex numbers, no conversion is performed;
* if either argument is a complex or a floating-point number, the
other is converted to a floating-point number;
* otherwise, both must be integers and no conversion is necessary.
Some additional rules apply for certain operators (e.g., a string as a
left argument to the ‘%’ operator). Extensions must define their own
conversion behavior.
''',
'customization': r'''Basic customization
*******************
object.__new__(cls[, ...])
Called to create a new instance of class *cls*. "__new__()" is a
static method (special-cased so you need not declare it as such)
that takes the class of which an instance was requested as its
first argument. The remaining arguments are those passed to the
object constructor expression (the call to the class). The return
value of "__new__()" should be the new object instance (usually an
instance of *cls*).
Typical implementations create a new instance of the class by
invoking the superclass’s "__new__()" method using
"super().__new__(cls[, ...])" with appropriate arguments and then
modifying the newly created instance as necessary before returning
it.
If "__new__()" is invoked during object construction and it returns
an instance of *cls*, then the new instance’s "__init__()" method
will be invoked like "__init__(self[, ...])", where *self* is the
new instance and the remaining arguments are the same as were
passed to the object constructor.
If "__new__()" does not return an instance of *cls*, then the new
instance’s "__init__()" method will not be invoked.
"__new__()" is intended mainly to allow subclasses of immutable
types (like int, str, or tuple) to customize instance creation. It
is also commonly overridden in custom metaclasses in order to
customize class creation.
object.__init__(self[, ...])
Called after the instance has been created (by "__new__()"), but
before it is returned to the caller. The arguments are those
passed to the class constructor expression. If a base class has an
"__init__()" method, the derived class’s "__init__()" method, if
any, must explicitly call it to ensure proper initialization of the
base class part of the instance; for example:
"super().__init__([args...])".
Because "__new__()" and "__init__()" work together in constructing
objects ("__new__()" to create it, and "__init__()" to customize
it), no non-"None" value may be returned by "__init__()"; doing so
will cause a "TypeError" to be raised at runtime.
object.__del__(self)
Called when the instance is about to be destroyed. This is also
called a finalizer or (improperly) a destructor. If a base class
has a "__del__()" method, the derived class’s "__del__()" method,
if any, must explicitly call it to ensure proper deletion of the
base class part of the instance.
It is possible (though not recommended!) for the "__del__()" method
to postpone destruction of the instance by creating a new reference
to it. This is called object *resurrection*. It is
implementation-dependent whether "__del__()" is called a second
time when a resurrected object is about to be destroyed; the
current *CPython* implementation only calls it once.
It is not guaranteed that "__del__()" methods are called for
objects that still exist when the interpreter exits.
"weakref.finalize" provides a straightforward way to register a
cleanup function to be called when an object is garbage collected.
Note:
"del x" doesn’t directly call "x.__del__()" — the former
decrements the reference count for "x" by one, and the latter is
only called when "x"’s reference count reaches zero.
**CPython implementation detail:** It is possible for a reference
cycle to prevent the reference count of an object from going to
zero. In this case, the cycle will be later detected and deleted
by the *cyclic garbage collector*. A common cause of reference
cycles is when an exception has been caught in a local variable.
The frame’s locals then reference the exception, which references
its own traceback, which references the locals of all frames caught
in the traceback.
See also: Documentation for the "gc" module.
Warning:
Due to the precarious circumstances under which "__del__()"
methods are invoked, exceptions that occur during their execution
are ignored, and a warning is printed to "sys.stderr" instead.
In particular:
* "__del__()" can be invoked when arbitrary code is being
executed, including from any arbitrary thread. If "__del__()"
needs to take a lock or invoke any other blocking resource, it
may deadlock as the resource may already be taken by the code
that gets interrupted to execute "__del__()".
* "__del__()" can be executed during interpreter shutdown. As a
consequence, the global variables it needs to access (including
other modules) may already have been deleted or set to "None".
Python guarantees that globals whose name begins with a single
underscore are deleted from their module before other globals
are deleted; if no other references to such globals exist, this
may help in assuring that imported modules are still available
at the time when the "__del__()" method is called.
object.__repr__(self)
Called by the "repr()" built-in function to compute the “official”
string representation of an object. If at all possible, this
should look like a valid Python expression that could be used to
recreate an object with the same value (given an appropriate
environment). If this is not possible, a string of the form
"<...some useful description...>" should be returned. The return
value must be a string object. If a class defines "__repr__()" but
not "__str__()", then "__repr__()" is also used when an “informal”
string representation of instances of that class is required.
This is typically used for debugging, so it is important that the
representation is information-rich and unambiguous. A default
implementation is provided by the "object" class itself.
object.__str__(self)
Called by "str(object)", the default "__format__()" implementation,
and the built-in function "print()", to compute the “informal” or
nicely printable string representation of an object. The return
value must be a str object.
This method differs from "object.__repr__()" in that there is no
expectation that "__str__()" return a valid Python expression: a
more convenient or concise representation can be used.
The default implementation defined by the built-in type "object"
calls "object.__repr__()".
object.__bytes__(self)
Called by bytes to compute a byte-string representation of an
object. This should return a "bytes" object. The "object" class
itself does not provide this method.
object.__format__(self, format_spec)
Called by the "format()" built-in function, and by extension,
evaluation of formatted string literals and the "str.format()"
method, to produce a “formatted” string representation of an
object. The *format_spec* argument is a string that contains a
description of the formatting options desired. The interpretation
of the *format_spec* argument is up to the type implementing
"__format__()", however most classes will either delegate
formatting to one of the built-in types, or use a similar
formatting option syntax.
See Format Specification Mini-Language for a description of the
standard formatting syntax.
The return value must be a string object.
The default implementation by the "object" class should be given an
empty *format_spec* string. It delegates to "__str__()".
Changed in version 3.4: The __format__ method of "object" itself
raises a "TypeError" if passed any non-empty string.
Changed in version 3.7: "object.__format__(x, '')" is now
equivalent to "str(x)" rather than "format(str(x), '')".
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
These are the so-called “rich comparison” methods. The
correspondence between operator symbols and method names is as
follows: "x<y" calls "x.__lt__(y)", "x<=y" calls "x.__le__(y)",
"x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", "x>y" calls
"x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".
A rich comparison method may return the singleton "NotImplemented"
if it does not implement the operation for a given pair of
arguments. By convention, "False" and "True" are returned for a
successful comparison. However, these methods can return any value,
so if the comparison operator is used in a Boolean context (e.g.,
in the condition of an "if" statement), Python will call "bool()"
on the value to determine if the result is true or false.
By default, "object" implements "__eq__()" by using "is", returning
"NotImplemented" in the case of a false comparison: "True if x is y
else NotImplemented". For "__ne__()", by default it delegates to
"__eq__()" and inverts the result unless it is "NotImplemented".
There are no other implied relationships among the comparison
operators or default implementations; for example, the truth of
"(x<y or x==y)" does not imply "x<=y". To automatically generate
ordering operations from a single root operation, see
"functools.total_ordering()".
By default, the "object" class provides implementations consistent
with Value comparisons: equality compares according to object
identity, and order comparisons raise "TypeError". Each default
method may generate these results directly, but may also return
"NotImplemented".
See the paragraph on "__hash__()" for some important notes on
creating *hashable* objects which support custom comparison
operations and are usable as dictionary keys.
There are no swapped-argument versions of these methods (to be used
when the left argument does not support the operation but the right
argument does); rather, "__lt__()" and "__gt__()" are each other’s
reflection, "__le__()" and "__ge__()" are each other’s reflection,
and "__eq__()" and "__ne__()" are their own reflection. If the
operands are of different types, and the right operand’s type is a
direct or indirect subclass of the left operand’s type, the
reflected method of the right operand has priority, otherwise the
left operand’s method has priority. Virtual subclassing is not
considered.
When no appropriate method returns any value other than
"NotImplemented", the "==" and "!=" operators will fall back to
"is" and "is not", respectively.
object.__hash__(self)
Called by built-in function "hash()" and for operations on members
of hashed collections including "set", "frozenset", and "dict".
The "__hash__()" method should return an integer. The only required
property is that objects which compare equal have the same hash
value; it is advised to mix together the hash values of the
components of the object that also play a part in comparison of
objects by packing them into a tuple and hashing the tuple.
Example:
def __hash__(self):
return hash((self.name, self.nick, self.color))
Note:
"hash()" truncates the value returned from an object’s custom
"__hash__()" method to the size of a "Py_ssize_t". This is
typically 8 bytes on 64-bit builds and 4 bytes on 32-bit builds.
If an object’s "__hash__()" must interoperate on builds of
different bit sizes, be sure to check the width on all supported
builds. An easy way to do this is with "python -c "import sys;
print(sys.hash_info.width)"".
If a class does not define an "__eq__()" method it should not
define a "__hash__()" operation either; if it defines "__eq__()"
but not "__hash__()", its instances will not be usable as items in
hashable collections. If a class defines mutable objects and
implements an "__eq__()" method, it should not implement
"__hash__()", since the implementation of *hashable* collections
requires that a key’s hash value is immutable (if the object’s hash
value changes, it will be in the wrong hash bucket).
User-defined classes have "__eq__()" and "__hash__()" methods by
default (inherited from the "object" class); with them, all objects
compare unequal (except with themselves) and "x.__hash__()" returns
an appropriate value such that "x == y" implies both that "x is y"
and "hash(x) == hash(y)".
A class that overrides "__eq__()" and does not define "__hash__()"
will have its "__hash__()" implicitly set to "None". When the
"__hash__()" method of a class is "None", instances of the class
will raise an appropriate "TypeError" when a program attempts to
retrieve their hash value, and will also be correctly identified as
unhashable when checking "isinstance(obj,
collections.abc.Hashable)".
If a class that overrides "__eq__()" needs to retain the
implementation of "__hash__()" from a parent class, the interpreter
must be told this explicitly by setting "__hash__ =
<ParentClass>.__hash__".
If a class that does not override "__eq__()" wishes to suppress
hash support, it should include "__hash__ = None" in the class
definition. A class which defines its own "__hash__()" that
explicitly raises a "TypeError" would be incorrectly identified as
hashable by an "isinstance(obj, collections.abc.Hashable)" call.
Note:
By default, the "__hash__()" values of str and bytes objects are
“salted” with an unpredictable random value. Although they
remain constant within an individual Python process, they are not
predictable between repeated invocations of Python.This is
intended to provide protection against a denial-of-service caused
by carefully chosen inputs that exploit the worst case
performance of a dict insertion, *O*(*n*^2) complexity. See
http://ocert.org/advisories/ocert-2011-003.html for
details.Changing hash values affects the iteration order of sets.
Python has never made guarantees about this ordering (and it
typically varies between 32-bit and 64-bit builds).See also
"PYTHONHASHSEED".
Changed in version 3.3: Hash randomization is enabled by default.
object.__bool__(self)
Called to implement truth value testing and the built-in operation
"bool()"; should return "False" or "True". When this method is not
defined, "__len__()" is called, if it is defined, and the object is
considered true if its result is nonzero. If a class defines
neither "__len__()" nor "__bool__()" (which is true of the "object"
class itself), all its instances are considered true.
''',
'debugger': r'''"pdb" — The Python Debugger
***************************
**Source code:** Lib/pdb.py
======================================================================
The module "pdb" defines an interactive source code debugger for
Python programs. It supports setting (conditional) breakpoints and
single stepping at the source line level, inspection of stack frames,
source code listing, and evaluation of arbitrary Python code in the
context of any stack frame. It also supports post-mortem debugging
and can be called under program control.
The debugger is extensible – it is actually defined as the class
"Pdb". This is currently undocumented but easily understood by reading
the source. The extension interface uses the modules "bdb" and "cmd".
See also:
Module "faulthandler"
Used to dump Python tracebacks explicitly, on a fault, after a
timeout, or on a user signal.
Module "traceback"
Standard interface to extract, format and print stack traces of
Python programs.
The typical usage to break into the debugger is to insert:
import pdb; pdb.set_trace()
Or:
breakpoint()
at the location you want to break into the debugger, and then run the
program. You can then step through the code following this statement,
and continue running without the debugger using the "continue"
command.
Changed in version 3.7: The built-in "breakpoint()", when called with
defaults, can be used instead of "import pdb; pdb.set_trace()".
def double(x):
breakpoint()
return x * 2
val = 3
print(f"{val} * 2 is {double(val)}")
The debugger’s prompt is "(Pdb)", which is the indicator that you are
in debug mode:
> ...(2)double()
-> breakpoint()
(Pdb) p x
3
(Pdb) continue
3 * 2 is 6
Changed in version 3.3: Tab-completion via the "readline" module is
available for commands and command arguments, e.g. the current global
and local names are offered as arguments of the "p" command.
You can also invoke "pdb" from the command line to debug other
scripts. For example:
python -m pdb [-c command] (-m module | pyfile) [args ...]
When invoked as a module, pdb will automatically enter post-mortem
debugging if the program being debugged exits abnormally. After post-
mortem debugging (or after normal exit of the program), pdb will
restart the program. Automatic restarting preserves pdb’s state (such
as breakpoints) and in most cases is more useful than quitting the
debugger upon program’s exit.
-c, --command <command>
To execute commands as if given in a ".pdbrc" file; see Debugger
Commands.
Changed in version 3.2: Added the "-c" option.
-m <module>
To execute modules similar to the way "python -m" does. As with a
script, the debugger will pause execution just before the first
line of the module.
Changed in version 3.7: Added the "-m" option.
Typical usage to execute a statement under control of the debugger is:
>>> import pdb
>>> def f(x):
... print(1 / x)
>>> pdb.run("f(2)")
> <string>(1)<module>()
(Pdb) continue
0.5
>>>
The typical usage to inspect a crashed program is:
>>> import pdb
>>> def f(x):
... print(1 / x)
...
>>> f(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
ZeroDivisionError: division by zero
>>> pdb.pm()
> <stdin>(2)f()
(Pdb) p x
0
(Pdb)
Changed in version 3.13: The implementation of **PEP 667** means that
name assignments made via "pdb" will immediately affect the active
scope, even when running inside an *optimized scope*.
The module defines the following functions; each enters the debugger
in a slightly different way:
pdb.run(statement, globals=None, locals=None)
Execute the *statement* (given as a string or a code object) under
debugger control. The debugger prompt appears before any code is
executed; you can set breakpoints and type "continue", or you can
step through the statement using "step" or "next" (all these
commands are explained below). The optional *globals* and *locals*
arguments specify the environment in which the code is executed; by
default the dictionary of the module "__main__" is used. (See the
explanation of the built-in "exec()" or "eval()" functions.)
pdb.runeval(expression, globals=None, locals=None)
Evaluate the *expression* (given as a string or a code object)
under debugger control. When "runeval()" returns, it returns the
value of the *expression*. Otherwise this function is similar to
"run()".
pdb.runcall(function, *args, **kwds)
Call the *function* (a function or method object, not a string)
with the given arguments. When "runcall()" returns, it returns
whatever the function call returned. The debugger prompt appears
as soon as the function is entered.
pdb.set_trace(*, header=None, commands=None)
Enter the debugger at the calling stack frame. This is useful to
hard-code a breakpoint at a given point in a program, even if the
code is not otherwise being debugged (e.g. when an assertion
fails). If given, *header* is printed to the console just before
debugging begins. The *commands* argument, if given, is a list of
commands to execute when the debugger starts.
Changed in version 3.7: The keyword-only argument *header*.
Changed in version 3.13: "set_trace()" will enter the debugger
immediately, rather than on the next line of code to be executed.
Added in version 3.14: The *commands* argument.
awaitable pdb.set_trace_async(*, header=None, commands=None)
async version of "set_trace()". This function should be used inside
an async function with "await".
async def f():
await pdb.set_trace_async()
"await" statements are supported if the debugger is invoked by this
function.
Added in version 3.14.
pdb.post_mortem(t=None)
Enter post-mortem debugging of the given exception or traceback
object. If no value is given, it uses the exception that is
currently being handled, or raises "ValueError" if there isn’t one.
Changed in version 3.13: Support for exception objects was added.
pdb.pm()
Enter post-mortem debugging of the exception found in
"sys.last_exc".
pdb.set_default_backend(backend)
There are two supported backends for pdb: "'settrace'" and
"'monitoring'". See "bdb.Bdb" for details. The user can set the
default backend to use if none is specified when instantiating
"Pdb". If no backend is specified, the default is "'settrace'".
Note:
"breakpoint()" and "set_trace()" will not be affected by this
function. They always use "'monitoring'" backend.
Added in version 3.14.
pdb.get_default_backend()
Returns the default backend for pdb.
Added in version 3.14.
The "run*" functions and "set_trace()" are aliases for instantiating
the "Pdb" class and calling the method of the same name. If you want
to access further features, you have to do this yourself:
class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=False, readrc=True, mode=None, backend=None, colorize=False)
"Pdb" is the debugger class.
The *completekey*, *stdin* and *stdout* arguments are passed to the
underlying "cmd.Cmd" class; see the description there.
The *skip* argument, if given, must be an iterable of glob-style
module name patterns. The debugger will not step into frames that
originate in a module that matches one of these patterns. [1]
By default, Pdb sets a handler for the SIGINT signal (which is sent
when the user presses "Ctrl"-"C" on the console) when you give a
"continue" command. This allows you to break into the debugger
again by pressing "Ctrl"-"C". If you want Pdb not to touch the
SIGINT handler, set *nosigint* to true.
The *readrc* argument defaults to true and controls whether Pdb
will load .pdbrc files from the filesystem.
The *mode* argument specifies how the debugger was invoked. It
impacts the workings of some debugger commands. Valid values are
"'inline'" (used by the breakpoint() builtin), "'cli'" (used by the
command line invocation) or "None" (for backwards compatible
behaviour, as before the *mode* argument was added).
The *backend* argument specifies the backend to use for the
debugger. If "None" is passed, the default backend will be used.
See "set_default_backend()". Otherwise the supported backends are
"'settrace'" and "'monitoring'".
The *colorize* argument, if set to "True", will enable colorized
output in the debugger, if color is supported. This will highlight
source code displayed in pdb.
Example call to enable tracing with *skip*:
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
Raises an auditing event "pdb.Pdb" with no arguments.
Changed in version 3.1: Added the *skip* parameter.
Changed in version 3.2: Added the *nosigint* parameter. Previously,
a SIGINT handler was never set by Pdb.
Changed in version 3.6: The *readrc* argument.
Added in version 3.14: Added the *mode* argument.
Added in version 3.14: Added the *backend* argument.
Added in version 3.14: Added the *colorize* argument.
Changed in version 3.14: Inline breakpoints like "breakpoint()" or
"pdb.set_trace()" will always stop the program at calling frame,
ignoring the *skip* pattern (if any).
run(statement, globals=None, locals=None)
runeval(expression, globals=None, locals=None)
runcall(function, *args, **kwds)
set_trace()
See the documentation for the functions explained above.
Debugger Commands
=================
The commands recognized by the debugger are listed below. Most
commands can be abbreviated to one or two letters as indicated; e.g.
"h(elp)" means that either "h" or "help" can be used to enter the help
command (but not "he" or "hel", nor "H" or "Help" or "HELP").
Arguments to commands must be separated by whitespace (spaces or
tabs). Optional arguments are enclosed in square brackets ("[]") in
the command syntax; the square brackets must not be typed.
Alternatives in the command syntax are separated by a vertical bar
("|").
Entering a blank line repeats the last command entered. Exception: if
the last command was a "list" command, the next 11 lines are listed.
Commands that the debugger doesn’t recognize are assumed to be Python
statements and are executed in the context of the program being
debugged. Python statements can also be prefixed with an exclamation
point ("!"). This is a powerful way to inspect the program being
debugged; it is even possible to change a variable or call a function.
When an exception occurs in such a statement, the exception name is
printed but the debugger’s state is not changed.
Changed in version 3.13: Expressions/Statements whose prefix is a pdb
command are now correctly identified and executed.
The debugger supports aliases. Aliases can have parameters which
allows one a certain level of adaptability to the context under
examination.
Multiple commands may be entered on a single line, separated by ";;".
(A single ";" is not used as it is the separator for multiple commands
in a line that is passed to the Python parser.) No intelligence is
applied to separating the commands; the input is split at the first
";;" pair, even if it is in the middle of a quoted string. A
workaround for strings with double semicolons is to use implicit
string concatenation "';'';'" or "";"";"".
To set a temporary global variable, use a *convenience variable*. A
*convenience variable* is a variable whose name starts with "$". For
example, "$foo = 1" sets a global variable "$foo" which you can use in
the debugger session. The *convenience variables* are cleared when
the program resumes execution so it’s less likely to interfere with
your program compared to using normal variables like "foo = 1".
There are four preset *convenience variables*:
* "$_frame": the current frame you are debugging
* "$_retval": the return value if the frame is returning
* "$_exception": the exception if the frame is raising an exception
* "$_asynctask": the asyncio task if pdb stops in an async function
Added in version 3.12: Added the *convenience variable* feature.
Added in version 3.14: Added the "$_asynctask" convenience variable.
If a file ".pdbrc" exists in the user’s home directory or in the
current directory, it is read with "'utf-8'" encoding and executed as
if it had been typed at the debugger prompt, with the exception that
empty lines and lines starting with "#" are ignored. This is
particularly useful for aliases. If both files exist, the one in the
home directory is read first and aliases defined there can be
overridden by the local file.
Changed in version 3.2: ".pdbrc" can now contain commands that
continue debugging, such as "continue" or "next". Previously, these
commands had no effect.
Changed in version 3.11: ".pdbrc" is now read with "'utf-8'" encoding.
Previously, it was read with the system locale encoding.
h(elp) [command]
Without argument, print the list of available commands. With a
*command* as argument, print help about that command. "help pdb"
displays the full documentation (the docstring of the "pdb"
module). Since the *command* argument must be an identifier, "help
exec" must be entered to get help on the "!" command.
w(here) [count]
Print a stack trace, with the most recent frame at the bottom. if
*count* is 0, print the current frame entry. If *count* is
negative, print the least recent - *count* frames. If *count* is
positive, print the most recent *count* frames. An arrow (">")
indicates the current frame, which determines the context of most
commands.
Changed in version 3.14: *count* argument is added.
d(own) [count]
Move the current frame *count* (default one) levels down in the
stack trace (to a newer frame).
u(p) [count]
Move the current frame *count* (default one) levels up in the stack
trace (to an older frame).
b(reak) [([filename:]lineno | function) [, condition]]
With a *lineno* argument, set a break at line *lineno* in the
current file. The line number may be prefixed with a *filename* and
a colon, to specify a breakpoint in another file (possibly one that
hasn’t been loaded yet). The file is searched on "sys.path".
Acceptable forms of *filename* are "/abspath/to/file.py",
"relpath/file.py", "module" and "package.module".
With a *function* argument, set a break at the first executable
statement within that function. *function* can be any expression
that evaluates to a function in the current namespace.
If a second argument is present, it is an expression which must
evaluate to true before the breakpoint is honored.
Without argument, list all breaks, including for each breakpoint,
the number of times that breakpoint has been hit, the current
ignore count, and the associated condition if any.
Each breakpoint is assigned a number to which all the other
breakpoint commands refer.
tbreak [([filename:]lineno | function) [, condition]]
Temporary breakpoint, which is removed automatically when it is
first hit. The arguments are the same as for "break".
cl(ear) [filename:lineno | bpnumber ...]
With a *filename:lineno* argument, clear all the breakpoints at
this line. With a space separated list of breakpoint numbers, clear
those breakpoints. Without argument, clear all breaks (but first
ask confirmation).
disable bpnumber [bpnumber ...]
Disable the breakpoints given as a space separated list of
breakpoint numbers. Disabling a breakpoint means it cannot cause
the program to stop execution, but unlike clearing a breakpoint, it
remains in the list of breakpoints and can be (re-)enabled.
enable bpnumber [bpnumber ...]
Enable the breakpoints specified.
ignore bpnumber [count]
Set the ignore count for the given breakpoint number. If *count*
is omitted, the ignore count is set to 0. A breakpoint becomes
active when the ignore count is zero. When non-zero, the *count*
is decremented each time the breakpoint is reached and the
breakpoint is not disabled and any associated condition evaluates
to true.
condition bpnumber [condition]
Set a new *condition* for the breakpoint, an expression which must
evaluate to true before the breakpoint is honored. If *condition*
is absent, any existing condition is removed; i.e., the breakpoint
is made unconditional.
commands [bpnumber]
Specify a list of commands for breakpoint number *bpnumber*. The
commands themselves appear on the following lines. Type a line
containing just "end" to terminate the commands. An example:
(Pdb) commands 1
(com) p some_variable
(com) end
(Pdb)
To remove all commands from a breakpoint, type "commands" and
follow it immediately with "end"; that is, give no commands.
With no *bpnumber* argument, "commands" refers to the last
breakpoint set.
You can use breakpoint commands to start your program up again.
Simply use the "continue" command, or "step", or any other command
that resumes execution.
Specifying any command resuming execution (currently "continue",
"step", "next", "return", "until", "jump", "quit" and their
abbreviations) terminates the command list (as if that command was
immediately followed by end). This is because any time you resume
execution (even with a simple next or step), you may encounter
another breakpoint—which could have its own command list, leading
to ambiguities about which list to execute.
If the list of commands contains the "silent" command, or a command
that resumes execution, then the breakpoint message containing
information about the frame is not displayed.
Changed in version 3.14: Frame information will not be displayed if
a command that resumes execution is present in the command list.
s(tep)
Execute the current line, stop at the first possible occasion
(either in a function that is called or on the next line in the
current function).
n(ext)
Continue execution until the next line in the current function is
reached or it returns. (The difference between "next" and "step"
is that "step" stops inside a called function, while "next"
executes called functions at (nearly) full speed, only stopping at
the next line in the current function.)
unt(il) [lineno]
Without argument, continue execution until the line with a number
greater than the current one is reached.
With *lineno*, continue execution until a line with a number
greater or equal to *lineno* is reached. In both cases, also stop
when the current frame returns.
Changed in version 3.2: Allow giving an explicit line number.
r(eturn)
Continue execution until the current function returns.
c(ont(inue))
Continue execution, only stop when a breakpoint is encountered.
j(ump) lineno
Set the next line that will be executed. Only available in the
bottom-most frame. This lets you jump back and execute code again,
or jump forward to skip code that you don’t want to run.
It should be noted that not all jumps are allowed – for instance it
is not possible to jump into the middle of a "for" loop or out of a
"finally" clause.
l(ist) [first[, last]]
List source code for the current file. Without arguments, list 11
lines around the current line or continue the previous listing.
With "." as argument, list 11 lines around the current line. With
one argument, list 11 lines around at that line. With two
arguments, list the given range; if the second argument is less
than the first, it is interpreted as a count.
The current line in the current frame is indicated by "->". If an
exception is being debugged, the line where the exception was
originally raised or propagated is indicated by ">>", if it differs
from the current line.
Changed in version 3.2: Added the ">>" marker.
ll | longlist
List all source code for the current function or frame.
Interesting lines are marked as for "list".
Added in version 3.2.
a(rgs)
Print the arguments of the current function and their current
values.
p expression
Evaluate *expression* in the current context and print its value.
Note:
"print()" can also be used, but is not a debugger command — this
executes the Python "print()" function.
pp expression
Like the "p" command, except the value of *expression* is pretty-
printed using the "pprint" module.
whatis expression
Print the type of *expression*.
source expression
Try to get source code of *expression* and display it.
Added in version 3.2.
display [expression]
Display the value of *expression* if it changed, each time
execution stops in the current frame.
Without *expression*, list all display expressions for the current
frame.
Note:
Display evaluates *expression* and compares to the result of the
previous evaluation of *expression*, so when the result is
mutable, display may not be able to pick up the changes.
Example:
lst = []
breakpoint()
pass
lst.append(1)
print(lst)
Display won’t realize "lst" has been changed because the result of
evaluation is modified in place by "lst.append(1)" before being
compared:
> example.py(3)<module>()
-> pass
(Pdb) display lst
display lst: []
(Pdb) n
> example.py(4)<module>()
-> lst.append(1)
(Pdb) n
> example.py(5)<module>()
-> print(lst)
(Pdb)
You can do some tricks with copy mechanism to make it work:
> example.py(3)<module>()
-> pass
(Pdb) display lst[:]
display lst[:]: []
(Pdb) n
> example.py(4)<module>()
-> lst.append(1)
(Pdb) n
> example.py(5)<module>()
-> print(lst)
display lst[:]: [1] [old: []]
(Pdb)
Added in version 3.2.
undisplay [expression]
Do not display *expression* anymore in the current frame. Without
*expression*, clear all display expressions for the current frame.
Added in version 3.2.
interact
Start an interactive interpreter (using the "code" module) in a new
global namespace initialised from the local and global namespaces
for the current scope. Use "exit()" or "quit()" to exit the
interpreter and return to the debugger.
Note:
As "interact" creates a new dedicated namespace for code
execution, assignments to variables will not affect the original
namespaces. However, modifications to any referenced mutable
objects will be reflected in the original namespaces as usual.
Added in version 3.2.
Changed in version 3.13: "exit()" and "quit()" can be used to exit
the "interact" command.
Changed in version 3.13: "interact" directs its output to the
debugger’s output channel rather than "sys.stderr".
alias [name [command]]
Create an alias called *name* that executes *command*. The
*command* must *not* be enclosed in quotes. Replaceable parameters
can be indicated by "%1", "%2", … and "%9", while "%*" is replaced
by all the parameters. If *command* is omitted, the current alias
for *name* is shown. If no arguments are given, all aliases are
listed.
Aliases may be nested and can contain anything that can be legally
typed at the pdb prompt. Note that internal pdb commands *can* be
overridden by aliases. Such a command is then hidden until the
alias is removed. Aliasing is recursively applied to the first
word of the command line; all other words in the line are left
alone.
As an example, here are two useful aliases (especially when placed
in the ".pdbrc" file):
# Print instance variables (usage "pi classInst")
alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}")
# Print instance variables in self
alias ps pi self
unalias name
Delete the specified alias *name*.
! statement
Execute the (one-line) *statement* in the context of the current
stack frame. The exclamation point can be omitted unless the first
word of the statement resembles a debugger command, e.g.:
(Pdb) ! n=42
(Pdb)
To set a global variable, you can prefix the assignment command
with a "global" statement on the same line, e.g.:
(Pdb) global list_options; list_options = ['-l']
(Pdb)
run [args ...]
restart [args ...]
Restart the debugged Python program. If *args* is supplied, it is
split with "shlex" and the result is used as the new "sys.argv".
History, breakpoints, actions and debugger options are preserved.
"restart" is an alias for "run".
Changed in version 3.14: "run" and "restart" commands are disabled
when the debugger is invoked in "'inline'" mode.
q(uit)
Quit from the debugger. The program being executed is aborted. An
end-of-file input is equivalent to "quit".
A confirmation prompt will be shown if the debugger is invoked in
"'inline'" mode. Either "y", "Y", "<Enter>" or "EOF" will confirm
the quit.
Changed in version 3.14: A confirmation prompt will be shown if the
debugger is invoked in "'inline'" mode. After the confirmation, the
debugger will call "sys.exit()" immediately, instead of raising
"bdb.BdbQuit" in the next trace event.
debug code
Enter a recursive debugger that steps through *code* (which is an
arbitrary expression or statement to be executed in the current
environment).
retval
Print the return value for the last return of the current function.
exceptions [excnumber]
List or jump between chained exceptions.
When using "pdb.pm()" or "Pdb.post_mortem(...)" with a chained
exception instead of a traceback, it allows the user to move
between the chained exceptions using "exceptions" command to list
exceptions, and "exceptions <number>" to switch to that exception.
Example:
def out():
try:
middle()
except Exception as e:
raise ValueError("reraise middle() error") from e
def middle():
try:
return inner(0)
except Exception as e:
raise ValueError("Middle fail")
def inner(x):
1 / x
out()
calling "pdb.pm()" will allow to move between exceptions:
> example.py(5)out()
-> raise ValueError("reraise middle() error") from e
(Pdb) exceptions
0 ZeroDivisionError('division by zero')
1 ValueError('Middle fail')
> 2 ValueError('reraise middle() error')
(Pdb) exceptions 0
> example.py(16)inner()
-> 1 / x
(Pdb) up
> example.py(10)middle()
-> return inner(0)
Added in version 3.13.
-[ Footnotes ]-
[1] Whether a frame is considered to originate in a certain module is
determined by the "__name__" in the frame globals.
''',
'del': r'''The "del" statement
*******************
del_stmt: "del" target_list
Deletion is recursively defined very similar to the way assignment is
defined. Rather than spelling it out in full details, here are some
hints.
Deletion of a target list recursively deletes each target, from left
to right.
Deletion of a name removes the binding of that name from the local or
global namespace, depending on whether the name occurs in a "global"
statement in the same code block. If the name is unbound, a
"NameError" exception will be raised.
Deletion of attribute references, subscriptions and slicings is passed
to the primary object involved; deletion of a slicing is in general
equivalent to assignment of an empty slice of the right type (but even
this is determined by the sliced object).
Changed in version 3.2: Previously it was illegal to delete a name
from the local namespace if it occurs as a free variable in a nested
block.
''',
'dict': r'''Dictionary displays
*******************
A dictionary display is a possibly empty series of dict items
(key/value pairs) enclosed in curly braces:
dict_display: "{" [dict_item_list | dict_comprehension] "}"
dict_item_list: dict_item ("," dict_item)* [","]
dict_item: expression ":" expression | "**" or_expr
dict_comprehension: expression ":" expression comp_for
A dictionary display yields a new dictionary object.
If a comma-separated sequence of dict items is given, they are
evaluated from left to right to define the entries of the dictionary:
each key object is used as a key into the dictionary to store the
corresponding value. This means that you can specify the same key
multiple times in the dict item list, and the final dictionary’s value
for that key will be the last one given.
A double asterisk "**" denotes *dictionary unpacking*. Its operand
must be a *mapping*. Each mapping item is added to the new
dictionary. Later values replace values already set by earlier dict
items and earlier dictionary unpackings.
Added in version 3.5: Unpacking into dictionary displays, originally
proposed by **PEP 448**.
A dict comprehension, in contrast to list and set comprehensions,
needs two expressions separated with a colon followed by the usual
“for” and “if” clauses. When the comprehension is run, the resulting
key and value elements are inserted in the new dictionary in the order
they are produced.
Restrictions on the types of the key values are listed earlier in
section The standard type hierarchy. (To summarize, the key type
should be *hashable*, which excludes all mutable objects.) Clashes
between duplicate keys are not detected; the last value (textually
rightmost in the display) stored for a given key value prevails.
Changed in version 3.8: Prior to Python 3.8, in dict comprehensions,
the evaluation order of key and value was not well-defined. In
CPython, the value was evaluated before the key. Starting with 3.8,
the key is evaluated before the value, as proposed by **PEP 572**.
''',
'dynamic-features': r'''Interaction with dynamic features
*********************************
Name resolution of free variables occurs at runtime, not at compile
time. This means that the following code will print 42:
i = 10
def f():
print(i)
i = 42
f()
The "eval()" and "exec()" functions do not have access to the full
environment for resolving names. Names may be resolved in the local
and global namespaces of the caller. Free variables are not resolved
in the nearest enclosing namespace, but in the global namespace. [1]
The "exec()" and "eval()" functions have optional arguments to
override the global and local namespace. If only one namespace is
specified, it is used for both.
''',
'else': r'''The "if" statement
******************
The "if" statement is used for conditional execution:
if_stmt: "if" assignment_expression ":" suite
("elif" assignment_expression ":" suite)*
["else" ":" suite]
It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section Boolean operations
for the definition of true and false); then that suite is executed
(and no other part of the "if" statement is executed or evaluated).
If all expressions are false, the suite of the "else" clause, if
present, is executed.
''',
'exceptions': r'''Exceptions
**********
Exceptions are a means of breaking out of the normal flow of control
of a code block in order to handle errors or other exceptional
conditions. An exception is *raised* at the point where the error is
detected; it may be *handled* by the surrounding code block or by any
code block that directly or indirectly invoked the code block where
the error occurred.
The Python interpreter raises an exception when it detects a run-time
error (such as division by zero). A Python program can also
explicitly raise an exception with the "raise" statement. Exception
handlers are specified with the "try" … "except" statement. The
"finally" clause of such a statement can be used to specify cleanup
code which does not handle the exception, but is executed whether an
exception occurred or not in the preceding code.
Python uses the “termination” model of error handling: an exception
handler can find out what happened and continue execution at an outer
level, but it cannot repair the cause of the error and retry the
failing operation (except by re-entering the offending piece of code
from the top).
When an exception is not handled at all, the interpreter terminates
execution of the program, or returns to its interactive main loop. In
either case, it prints a stack traceback, except when the exception is
"SystemExit".
Exceptions are identified by class instances. The "except" clause is
selected depending on the class of the instance: it must reference the
class of the instance or a *non-virtual base class* thereof. The
instance can be received by the handler and can carry additional
information about the exceptional condition.
Note:
Exception messages are not part of the Python API. Their contents
may change from one version of Python to the next without warning
and should not be relied on by code which will run under multiple
versions of the interpreter.
See also the description of the "try" statement in section The try
statement and "raise" statement in section The raise statement.
-[ Footnotes ]-
[1] This limitation occurs because the code that is executed by these
operations is not available at the time the module is compiled.
''',
'execmodel': r'''Execution model
***************
Structure of a program
======================
A Python program is constructed from code blocks. A *block* is a piece
of Python program text that is executed as a unit. The following are
blocks: a module, a function body, and a class definition. Each
command typed interactively is a block. A script file (a file given
as standard input to the interpreter or specified as a command line
argument to the interpreter) is a code block. A script command (a
command specified on the interpreter command line with the "-c"
option) is a code block. A module run as a top level script (as module
"__main__") from the command line using a "-m" argument is also a code
block. The string argument passed to the built-in functions "eval()"
and "exec()" is a code block.
A code block is executed in an *execution frame*. A frame contains
some administrative information (used for debugging) and determines
where and how execution continues after the code block’s execution has
completed.
Naming and binding
==================
Binding of names
----------------
*Names* refer to objects. Names are introduced by name binding
operations.
The following constructs bind names:
* formal parameters to functions,
* class definitions,
* function definitions,
* assignment expressions,
* targets that are identifiers if occurring in an assignment:
* "for" loop header,
* after "as" in a "with" statement, "except" clause, "except*"
clause, or in the as-pattern in structural pattern matching,
* in a capture pattern in structural pattern matching
* "import" statements.
* "type" statements.
* type parameter lists.
The "import" statement of the form "from ... import *" binds all names
defined in the imported module, except those beginning with an
underscore. This form may only be used at the module level.
A target occurring in a "del" statement is also considered bound for
this purpose (though the actual semantics are to unbind the name).
Each assignment or import statement occurs within a block defined by a
class or function definition or at the module level (the top-level
code block).
If a name is bound in a block, it is a local variable of that block,
unless declared as "nonlocal" or "global". If a name is bound at the
module level, it is a global variable. (The variables of the module
code block are local and global.) If a variable is used in a code
block but not defined there, it is a *free variable*.
Each occurrence of a name in the program text refers to the *binding*
of that name established by the following name resolution rules.
Resolution of names
-------------------
A *scope* defines the visibility of a name within a block. If a local
variable is defined in a block, its scope includes that block. If the
definition occurs in a function block, the scope extends to any blocks
contained within the defining one, unless a contained block introduces
a different binding for the name.
When a name is used in a code block, it is resolved using the nearest
enclosing scope. The set of all such scopes visible to a code block
is called the block’s *environment*.
When a name is not found at all, a "NameError" exception is raised. If
the current scope is a function scope, and the name refers to a local
variable that has not yet been bound to a value at the point where the
name is used, an "UnboundLocalError" exception is raised.
"UnboundLocalError" is a subclass of "NameError".
If a name binding operation occurs anywhere within a code block, all
uses of the name within the block are treated as references to the
current block. This can lead to errors when a name is used within a
block before it is bound. This rule is subtle. Python lacks
declarations and allows name binding operations to occur anywhere
within a code block. The local variables of a code block can be
determined by scanning the entire text of the block for name binding
operations. See the FAQ entry on UnboundLocalError for examples.
If the "global" statement occurs within a block, all uses of the names
specified in the statement refer to the bindings of those names in the
top-level namespace. Names are resolved in the top-level namespace by
searching the global namespace, i.e. the namespace of the module
containing the code block, and the builtins namespace, the namespace
of the module "builtins". The global namespace is searched first. If
the names are not found there, the builtins namespace is searched
next. If the names are also not found in the builtins namespace, new
variables are created in the global namespace. The global statement
must precede all uses of the listed names.
The "global" statement has the same scope as a name binding operation
in the same block. If the nearest enclosing scope for a free variable
contains a global statement, the free variable is treated as a global.
The "nonlocal" statement causes corresponding names to refer to
previously bound variables in the nearest enclosing function scope.
"SyntaxError" is raised at compile time if the given name does not
exist in any enclosing function scope. Type parameters cannot be
rebound with the "nonlocal" statement.
The namespace for a module is automatically created the first time a
module is imported. The main module for a script is always called
"__main__".
Class definition blocks and arguments to "exec()" and "eval()" are
special in the context of name resolution. A class definition is an
executable statement that may use and define names. These references
follow the normal rules for name resolution with an exception that
unbound local variables are looked up in the global namespace. The
namespace of the class definition becomes the attribute dictionary of
the class. The scope of names defined in a class block is limited to
the class block; it does not extend to the code blocks of methods.
This includes comprehensions and generator expressions, but it does
not include annotation scopes, which have access to their enclosing
class scopes. This means that the following will fail:
class A:
a = 42
b = list(a + i for i in range(10))
However, the following will succeed:
class A:
type Alias = Nested
class Nested: pass
print(A.Alias.__value__) # <type 'A.Nested'>
Annotation scopes
-----------------
*Annotations*, type parameter lists and "type" statements introduce
*annotation scopes*, which behave mostly like function scopes, but
with some exceptions discussed below.
Annotation scopes are used in the following contexts:
* *Function annotations*.
* *Variable annotations*.
* Type parameter lists for generic type aliases.
* Type parameter lists for generic functions. A generic function’s
annotations are executed within the annotation scope, but its
defaults and decorators are not.
* Type parameter lists for generic classes. A generic class’s base
classes and keyword arguments are executed within the annotation
scope, but its decorators are not.
* The bounds, constraints, and default values for type parameters
(lazily evaluated).
* The value of type aliases (lazily evaluated).
Annotation scopes differ from function scopes in the following ways:
* Annotation scopes have access to their enclosing class namespace. If
an annotation scope is immediately within a class scope, or within
another annotation scope that is immediately within a class scope,
the code in the annotation scope can use names defined in the class
scope as if it were executed directly within the class body. This
contrasts with regular functions defined within classes, which
cannot access names defined in the class scope.
* Expressions in annotation scopes cannot contain "yield", "yield
from", "await", or ":=" expressions. (These expressions are allowed
in other scopes contained within the annotation scope.)
* Names defined in annotation scopes cannot be rebound with "nonlocal"
statements in inner scopes. This includes only type parameters, as
no other syntactic elements that can appear within annotation scopes
can introduce new names.
* While annotation scopes have an internal name, that name is not
reflected in the *qualified name* of objects defined within the
scope. Instead, the "__qualname__" of such objects is as if the
object were defined in the enclosing scope.
Added in version 3.12: Annotation scopes were introduced in Python
3.12 as part of **PEP 695**.
Changed in version 3.13: Annotation scopes are also used for type
parameter defaults, as introduced by **PEP 696**.
Changed in version 3.14: Annotation scopes are now also used for
annotations, as specified in **PEP 649** and **PEP 749**.
Lazy evaluation
---------------
Most annotation scopes are *lazily evaluated*. This includes
annotations, the values of type aliases created through the "type"
statement, and the bounds, constraints, and default values of type
variables created through the type parameter syntax. This means that
they are not evaluated when the type alias or type variable is
created, or when the object carrying annotations is created. Instead,
they are only evaluated when necessary, for example when the
"__value__" attribute on a type alias is accessed.
Example:
>>> type Alias = 1/0
>>> Alias.__value__
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
>>> def func[T: 1/0](): pass
>>> T = func.__type_params__[0]
>>> T.__bound__
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
Here the exception is raised only when the "__value__" attribute of
the type alias or the "__bound__" attribute of the type variable is
accessed.
This behavior is primarily useful for references to types that have
not yet been defined when the type alias or type variable is created.
For example, lazy evaluation enables creation of mutually recursive
type aliases:
from typing import Literal
type SimpleExpr = int | Parenthesized
type Parenthesized = tuple[Literal["("], Expr, Literal[")"]]
type Expr = SimpleExpr | tuple[SimpleExpr, Literal["+", "-"], Expr]
Lazily evaluated values are evaluated in annotation scope, which means
that names that appear inside the lazily evaluated value are looked up
as if they were used in the immediately enclosing scope.
Added in version 3.12.
Builtins and restricted execution
---------------------------------
**CPython implementation detail:** Users should not touch
"__builtins__"; it is strictly an implementation detail. Users
wanting to override values in the builtins namespace should "import"
the "builtins" module and modify its attributes appropriately.
The builtins namespace associated with the execution of a code block
is actually found by looking up the name "__builtins__" in its global
namespace; this should be a dictionary or a module (in the latter case
the module’s dictionary is used). By default, when in the "__main__"
module, "__builtins__" is the built-in module "builtins"; when in any
other module, "__builtins__" is an alias for the dictionary of the
"builtins" module itself.
Interaction with dynamic features
---------------------------------
Name resolution of free variables occurs at runtime, not at compile
time. This means that the following code will print 42:
i = 10
def f():
print(i)
i = 42
f()
The "eval()" and "exec()" functions do not have access to the full
environment for resolving names. Names may be resolved in the local
and global namespaces of the caller. Free variables are not resolved
in the nearest enclosing namespace, but in the global namespace. [1]
The "exec()" and "eval()" functions have optional arguments to
override the global and local namespace. If only one namespace is
specified, it is used for both.
Exceptions
==========
Exceptions are a means of breaking out of the normal flow of control
of a code block in order to handle errors or other exceptional
conditions. An exception is *raised* at the point where the error is
detected; it may be *handled* by the surrounding code block or by any
code block that directly or indirectly invoked the code block where
the error occurred.
The Python interpreter raises an exception when it detects a run-time
error (such as division by zero). A Python program can also
explicitly raise an exception with the "raise" statement. Exception
handlers are specified with the "try" … "except" statement. The
"finally" clause of such a statement can be used to specify cleanup
code which does not handle the exception, but is executed whether an
exception occurred or not in the preceding code.
Python uses the “termination” model of error handling: an exception
handler can find out what happened and continue execution at an outer
level, but it cannot repair the cause of the error and retry the
failing operation (except by re-entering the offending piece of code
from the top).
When an exception is not handled at all, the interpreter terminates
execution of the program, or returns to its interactive main loop. In
either case, it prints a stack traceback, except when the exception is
"SystemExit".
Exceptions are identified by class instances. The "except" clause is
selected depending on the class of the instance: it must reference the
class of the instance or a *non-virtual base class* thereof. The
instance can be received by the handler and can carry additional
information about the exceptional condition.
Note:
Exception messages are not part of the Python API. Their contents
may change from one version of Python to the next without warning
and should not be relied on by code which will run under multiple
versions of the interpreter.
See also the description of the "try" statement in section The try
statement and "raise" statement in section The raise statement.
-[ Footnotes ]-
[1] This limitation occurs because the code that is executed by these
operations is not available at the time the module is compiled.
''',
'exprlists': r'''Expression lists
****************
starred_expression: ["*"] or_expr
flexible_expression: assignment_expression | starred_expression
flexible_expression_list: flexible_expression ("," flexible_expression)* [","]
starred_expression_list: starred_expression ("," starred_expression)* [","]
expression_list: expression ("," expression)* [","]
yield_list: expression_list | starred_expression "," [starred_expression_list]
Except when part of a list or set display, an expression list
containing at least one comma yields a tuple. The length of the tuple
is the number of expressions in the list. The expressions are
evaluated from left to right.
An asterisk "*" denotes *iterable unpacking*. Its operand must be an
*iterable*. The iterable is expanded into a sequence of items, which
are included in the new tuple, list, or set, at the site of the
unpacking.
Added in version 3.5: Iterable unpacking in expression lists,
originally proposed by **PEP 448**.
Added in version 3.11: Any item in an expression list may be starred.
See **PEP 646**.
A trailing comma is required only to create a one-item tuple, such as
"1,"; it is optional in all other cases. A single expression without a
trailing comma doesn’t create a tuple, but rather yields the value of
that expression. (To create an empty tuple, use an empty pair of
parentheses: "()".)
''',
'floating': r'''Floating-point literals
***********************
Floating-point literals are described by the following lexical
definitions:
floatnumber: pointfloat | exponentfloat
pointfloat: [digitpart] fraction | digitpart "."
exponentfloat: (digitpart | pointfloat) exponent
digitpart: digit (["_"] digit)*
fraction: "." digitpart
exponent: ("e" | "E") ["+" | "-"] digitpart
Note that the integer and exponent parts are always interpreted using
radix 10. For example, "077e010" is legal, and denotes the same number
as "77e10". The allowed range of floating-point literals is
implementation-dependent. As in integer literals, underscores are
supported for digit grouping.
Some examples of floating-point literals:
3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93
Changed in version 3.6: Underscores are now allowed for grouping
purposes in literals.
''',
'for': r'''The "for" statement
*******************
The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:
for_stmt: "for" target_list "in" starred_list ":" suite
["else" ":" suite]
The "starred_list" expression is evaluated once; it should yield an
*iterable* object. An *iterator* is created for that iterable. The
first item provided by the iterator is then assigned to the target
list using the standard rules for assignments (see Assignment
statements), and the suite is executed. This repeats for each item
provided by the iterator. When the iterator is exhausted, the suite
in the "else" clause, if present, is executed, and the loop
terminates.
A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite. A "continue" statement
executed in the first suite skips the rest of the suite and continues
with the next item, or with the "else" clause if there is no next
item.
The for-loop makes assignments to the variables in the target list.
This overwrites all previous assignments to those variables including
those made in the suite of the for-loop:
for i in range(10):
print(i)
i = 5 # this will not affect the for-loop
# because i will be overwritten with the next
# index in the range
Names in the target list are not deleted when the loop is finished,
but if the sequence is empty, they will not have been assigned to at
all by the loop. Hint: the built-in type "range()" represents
immutable arithmetic sequences of integers. For instance, iterating
"range(3)" successively yields 0, 1, and then 2.
Changed in version 3.11: Starred elements are now allowed in the
expression list.
''',
'formatstrings': r'''Format String Syntax
********************
The "str.format()" method and the "Formatter" class share the same
syntax for format strings (although in the case of "Formatter",
subclasses can define their own format string syntax). The syntax is
related to that of formatted string literals, but it is less
sophisticated and, in particular, does not support arbitrary
expressions.
Format strings contain “replacement fields” surrounded by curly braces
"{}". Anything that is not contained in braces is considered literal
text, which is copied unchanged to the output. If you need to include
a brace character in the literal text, it can be escaped by doubling:
"{{" and "}}".
The grammar for a replacement field is as follows:
replacement_field: "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name: arg_name ("." attribute_name | "[" element_index "]")*
arg_name: [identifier | digit+]
attribute_name: identifier
element_index: digit+ | index_string
index_string: <any source character except "]"> +
conversion: "r" | "s" | "a"
format_spec: format-spec:format_spec
In less formal terms, the replacement field can start with a
*field_name* that specifies the object whose value is to be formatted
and inserted into the output instead of the replacement field. The
*field_name* is optionally followed by a *conversion* field, which is
preceded by an exclamation point "'!'", and a *format_spec*, which is
preceded by a colon "':'". These specify a non-default format for the
replacement value.
See also the Format Specification Mini-Language section.
The *field_name* itself begins with an *arg_name* that is either a
number or a keyword. If it’s a number, it refers to a positional
argument, and if it’s a keyword, it refers to a named keyword
argument. An *arg_name* is treated as a number if a call to
"str.isdecimal()" on the string would return true. If the numerical
arg_names in a format string are 0, 1, 2, … in sequence, they can all
be omitted (not just some) and the numbers 0, 1, 2, … will be
automatically inserted in that order. Because *arg_name* is not quote-
delimited, it is not possible to specify arbitrary dictionary keys
(e.g., the strings "'10'" or "':-]'") within a format string. The
*arg_name* can be followed by any number of index or attribute
expressions. An expression of the form "'.name'" selects the named
attribute using "getattr()", while an expression of the form
"'[index]'" does an index lookup using "__getitem__()".
Changed in version 3.1: The positional argument specifiers can be
omitted for "str.format()", so "'{} {}'.format(a, b)" is equivalent to
"'{0} {1}'.format(a, b)".
Changed in version 3.4: The positional argument specifiers can be
omitted for "Formatter".
Some simple format string examples:
"First, thou shalt count to {0}" # References first positional argument
"Bring me a {}" # Implicitly references the first positional argument
"From {} to {}" # Same as "From {0} to {1}"
"My quest is {name}" # References keyword argument 'name'
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
The *conversion* field causes a type coercion before formatting.
Normally, the job of formatting a value is done by the "__format__()"
method of the value itself. However, in some cases it is desirable to
force a type to be formatted as a string, overriding its own
definition of formatting. By converting the value to a string before
calling "__format__()", the normal formatting logic is bypassed.
Three conversion flags are currently supported: "'!s'" which calls
"str()" on the value, "'!r'" which calls "repr()" and "'!a'" which
calls "ascii()".
Some examples:
"Harold's a clever {0!s}" # Calls str() on the argument first
"Bring out the holy {name!r}" # Calls repr() on the argument first
"More {!a}" # Calls ascii() on the argument first
The *format_spec* field contains a specification of how the value
should be presented, including such details as field width, alignment,
padding, decimal precision and so on. Each value type can define its
own “formatting mini-language” or interpretation of the *format_spec*.
Most built-in types support a common formatting mini-language, which
is described in the next section.
A *format_spec* field can also include nested replacement fields
within it. These nested replacement fields may contain a field name,
conversion flag and format specification, but deeper nesting is not
allowed. The replacement fields within the format_spec are
substituted before the *format_spec* string is interpreted. This
allows the formatting of a value to be dynamically specified.
See the Format examples section for some examples.
Format Specification Mini-Language
==================================
“Format specifications” are used within replacement fields contained
within a format string to define how individual values are presented
(see Format String Syntax and f-strings). They can also be passed
directly to the built-in "format()" function. Each formattable type
may define how the format specification is to be interpreted.
Most built-in types implement the following options for format
specifications, although some of the formatting options are only
supported by the numeric types.
A general convention is that an empty format specification produces
the same result as if you had called "str()" on the value. A non-empty
format specification typically modifies the result.
The general form of a *standard format specifier* is:
format_spec: [options][width_and_precision][type]
options: [[fill]align][sign]["z"]["#"]["0"]
fill: <any character>
align: "<" | ">" | "=" | "^"
sign: "+" | "-" | " "
width_and_precision: [width_with_grouping][precision_with_grouping]
width_with_grouping: [width][grouping]
precision_with_grouping: "." [precision][grouping]
width: digit+
precision: digit+
grouping: "," | "_"
type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g"
| "G" | "n" | "o" | "s" | "x" | "X" | "%"
If a valid *align* value is specified, it can be preceded by a *fill*
character that can be any character and defaults to a space if
omitted. It is not possible to use a literal curly brace (”"{"” or
“"}"”) as the *fill* character in a formatted string literal or when
using the "str.format()" method. However, it is possible to insert a
curly brace with a nested replacement field. This limitation doesn’t
affect the "format()" function.
The meaning of the various alignment options is as follows:
+-----------+------------------------------------------------------------+
| Option | Meaning |
|===========|============================================================|
| "'<'" | Forces the field to be left-aligned within the available |
| | space (this is the default for most objects). |
+-----------+------------------------------------------------------------+
| "'>'" | Forces the field to be right-aligned within the available |
| | space (this is the default for numbers). |
+-----------+------------------------------------------------------------+
| "'='" | Forces the padding to be placed after the sign (if any) |
| | but before the digits. This is used for printing fields |
| | in the form ‘+000000120’. This alignment option is only |
| | valid for numeric types, excluding "complex". It becomes |
| | the default for numbers when ‘0’ immediately precedes the |
| | field width. |
+-----------+------------------------------------------------------------+
| "'^'" | Forces the field to be centered within the available |
| | space. |
+-----------+------------------------------------------------------------+
Note that unless a minimum field width is defined, the field width
will always be the same size as the data to fill it, so that the
alignment option has no meaning in this case.
The *sign* option is only valid for number types, and can be one of
the following:
+-----------+------------------------------------------------------------+
| Option | Meaning |
|===========|============================================================|
| "'+'" | Indicates that a sign should be used for both positive as |
| | well as negative numbers. |
+-----------+------------------------------------------------------------+
| "'-'" | Indicates that a sign should be used only for negative |
| | numbers (this is the default behavior). |
+-----------+------------------------------------------------------------+
| space | Indicates that a leading space should be used on positive |
| | numbers, and a minus sign on negative numbers. |
+-----------+------------------------------------------------------------+
The "'z'" option coerces negative zero floating-point values to
positive zero after rounding to the format precision. This option is
only valid for floating-point presentation types.
Changed in version 3.11: Added the "'z'" option (see also **PEP
682**).
The "'#'" option causes the “alternate form” to be used for the
conversion. The alternate form is defined differently for different
types. This option is only valid for integer, float and complex
types. For integers, when binary, octal, or hexadecimal output is
used, this option adds the respective prefix "'0b'", "'0o'", "'0x'",
or "'0X'" to the output value. For float and complex the alternate
form causes the result of the conversion to always contain a decimal-
point character, even if no digits follow it. Normally, a decimal-
point character appears in the result of these conversions only if a
digit follows it. In addition, for "'g'" and "'G'" conversions,
trailing zeros are not removed from the result.
The *width* is a decimal integer defining the minimum total field
width, including any prefixes, separators, and other formatting
characters. If not specified, then the field width will be determined
by the content.
When no explicit alignment is given, preceding the *width* field by a
zero ("'0'") character enables sign-aware zero-padding for numeric
types, excluding "complex". This is equivalent to a *fill* character
of "'0'" with an *alignment* type of "'='".
Changed in version 3.10: Preceding the *width* field by "'0'" no
longer affects the default alignment for strings.
The *precision* is a decimal integer indicating how many digits should
be displayed after the decimal point for presentation types "'f'" and
"'F'", or before and after the decimal point for presentation types
"'g'" or "'G'". For string presentation types the field indicates the
maximum field size - in other words, how many characters will be used
from the field content. The *precision* is not allowed for integer
presentation types.
The *grouping* option after *width* and *precision* fields specifies a
digit group separator for the integral and fractional parts of a
number respectively. It can be one of the following:
+-----------+------------------------------------------------------------+
| Option | Meaning |
|===========|============================================================|
| "','" | Inserts a comma every 3 digits for integer presentation |
| | type "'d'" and floating-point presentation types, |
| | excluding "'n'". For other presentation types, this option |
| | is not supported. |
+-----------+------------------------------------------------------------+
| "'_'" | Inserts an underscore every 3 digits for integer |
| | presentation type "'d'" and floating-point presentation |
| | types, excluding "'n'". For integer presentation types |
| | "'b'", "'o'", "'x'", and "'X'", underscores are inserted |
| | every 4 digits. For other presentation types, this option |
| | is not supported. |
+-----------+------------------------------------------------------------+
For a locale aware separator, use the "'n'" presentation type instead.
Changed in version 3.1: Added the "','" option (see also **PEP 378**).
Changed in version 3.6: Added the "'_'" option (see also **PEP 515**).
Changed in version 3.14: Support the *grouping* option for the
fractional part.
Finally, the *type* determines how the data should be presented.
The available string presentation types are:
+-----------+------------------------------------------------------------+
| Type | Meaning |
|===========|============================================================|
| "'s'" | String format. This is the default type for strings and |
| | may be omitted. |
+-----------+------------------------------------------------------------+
| None | The same as "'s'". |
+-----------+------------------------------------------------------------+
The available integer presentation types are:
+-----------+------------------------------------------------------------+
| Type | Meaning |
|===========|============================================================|
| "'b'" | Binary format. Outputs the number in base 2. |
+-----------+------------------------------------------------------------+
| "'c'" | Character. Converts the integer to the corresponding |
| | unicode character before printing. |
+-----------+------------------------------------------------------------+
| "'d'" | Decimal Integer. Outputs the number in base 10. |
+-----------+------------------------------------------------------------+
| "'o'" | Octal format. Outputs the number in base 8. |
+-----------+------------------------------------------------------------+
| "'x'" | Hex format. Outputs the number in base 16, using lower- |
| | case letters for the digits above 9. |
+-----------+------------------------------------------------------------+
| "'X'" | Hex format. Outputs the number in base 16, using upper- |
| | case letters for the digits above 9. In case "'#'" is |
| | specified, the prefix "'0x'" will be upper-cased to "'0X'" |
| | as well. |
+-----------+------------------------------------------------------------+
| "'n'" | Number. This is the same as "'d'", except that it uses the |
| | current locale setting to insert the appropriate digit |
| | group separators. |
+-----------+------------------------------------------------------------+
| None | The same as "'d'". |
+-----------+------------------------------------------------------------+
In addition to the above presentation types, integers can be formatted
with the floating-point presentation types listed below (except "'n'"
and "None"). When doing so, "float()" is used to convert the integer
to a floating-point number before formatting.
The available presentation types for "float" and "Decimal" values are:
+-----------+------------------------------------------------------------+
| Type | Meaning |
|===========|============================================================|
| "'e'" | Scientific notation. For a given precision "p", formats |
| | the number in scientific notation with the letter ‘e’ |
| | separating the coefficient from the exponent. The |
| | coefficient has one digit before and "p" digits after the |
| | decimal point, for a total of "p + 1" significant digits. |
| | With no precision given, uses a precision of "6" digits |
| | after the decimal point for "float", and shows all |
| | coefficient digits for "Decimal". If "p=0", the decimal |
| | point is omitted unless the "#" option is used. |
+-----------+------------------------------------------------------------+
| "'E'" | Scientific notation. Same as "'e'" except it uses an upper |
| | case ‘E’ as the separator character. |
+-----------+------------------------------------------------------------+
| "'f'" | Fixed-point notation. For a given precision "p", formats |
| | the number as a decimal number with exactly "p" digits |
| | following the decimal point. With no precision given, uses |
| | a precision of "6" digits after the decimal point for |
| | "float", and uses a precision large enough to show all |
| | coefficient digits for "Decimal". If "p=0", the decimal |
| | point is omitted unless the "#" option is used. |
+-----------+------------------------------------------------------------+
| "'F'" | Fixed-point notation. Same as "'f'", but converts "nan" to |
| | "NAN" and "inf" to "INF". |
+-----------+------------------------------------------------------------+
| "'g'" | General format. For a given precision "p >= 1", this |
| | rounds the number to "p" significant digits and then |
| | formats the result in either fixed-point format or in |
| | scientific notation, depending on its magnitude. A |
| | precision of "0" is treated as equivalent to a precision |
| | of "1". The precise rules are as follows: suppose that |
| | the result formatted with presentation type "'e'" and |
| | precision "p-1" would have exponent "exp". Then, if "m <= |
| | exp < p", where "m" is -4 for floats and -6 for |
| | "Decimals", the number is formatted with presentation type |
| | "'f'" and precision "p-1-exp". Otherwise, the number is |
| | formatted with presentation type "'e'" and precision |
| | "p-1". In both cases insignificant trailing zeros are |
| | removed from the significand, and the decimal point is |
| | also removed if there are no remaining digits following |
| | it, unless the "'#'" option is used. With no precision |
| | given, uses a precision of "6" significant digits for |
| | "float". For "Decimal", the coefficient of the result is |
| | formed from the coefficient digits of the value; |
| | scientific notation is used for values smaller than "1e-6" |
| | in absolute value and values where the place value of the |
| | least significant digit is larger than 1, and fixed-point |
| | notation is used otherwise. Positive and negative |
| | infinity, positive and negative zero, and nans, are |
| | formatted as "inf", "-inf", "0", "-0" and "nan" |
| | respectively, regardless of the precision. |
+-----------+------------------------------------------------------------+
| "'G'" | General format. Same as "'g'" except switches to "'E'" if |
| | the number gets too large. The representations of infinity |
| | and NaN are uppercased, too. |
+-----------+------------------------------------------------------------+
| "'n'" | Number. This is the same as "'g'", except that it uses the |
| | current locale setting to insert the appropriate digit |
| | group separators for the integral part of a number. |
+-----------+------------------------------------------------------------+
| "'%'" | Percentage. Multiplies the number by 100 and displays in |
| | fixed ("'f'") format, followed by a percent sign. |
+-----------+------------------------------------------------------------+
| None | For "float" this is like the "'g'" type, except that when |
| | fixed- point notation is used to format the result, it |
| | always includes at least one digit past the decimal point, |
| | and switches to the scientific notation when "exp >= p - |
| | 1". When the precision is not specified, the latter will |
| | be as large as needed to represent the given value |
| | faithfully. For "Decimal", this is the same as either |
| | "'g'" or "'G'" depending on the value of |
| | "context.capitals" for the current decimal context. The |
| | overall effect is to match the output of "str()" as |
| | altered by the other format modifiers. |
+-----------+------------------------------------------------------------+
The result should be correctly rounded to a given precision "p" of
digits after the decimal point. The rounding mode for "float" matches
that of the "round()" builtin. For "Decimal", the rounding mode of
the current context will be used.
The available presentation types for "complex" are the same as those
for "float" ("'%'" is not allowed). Both the real and imaginary
components of a complex number are formatted as floating-point
numbers, according to the specified presentation type. They are
separated by the mandatory sign of the imaginary part, the latter
being terminated by a "j" suffix. If the presentation type is
missing, the result will match the output of "str()" (complex numbers
with a non-zero real part are also surrounded by parentheses),
possibly altered by other format modifiers.
Format examples
===============
This section contains examples of the "str.format()" syntax and
comparison with the old "%"-formatting.
In most of the cases the syntax is similar to the old "%"-formatting,
with the addition of the "{}" and with ":" used instead of "%". For
example, "'%03.2f'" can be translated to "'{:03.2f}'".
The new format syntax also supports new and different options, shown
in the following examples.
Accessing arguments by position:
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra'
Accessing arguments by name:
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'
Accessing arguments’ attributes:
>>> c = 3-5j
>>> ('The complex number {0} is formed from the real part {0.real} '
... 'and the imaginary part {0.imag}.').format(c)
'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
>>> class Point:
... def __init__(self, x, y):
... self.x, self.y = x, y
... def __str__(self):
... return 'Point({self.x}, {self.y})'.format(self=self)
...
>>> str(Point(4, 2))
'Point(4, 2)'
Accessing arguments’ items:
>>> coord = (3, 5)
>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)
'X: 3; Y: 5'
Replacing "%s" and "%r":
>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
"repr() shows quotes: 'test1'; str() doesn't: test2"
Aligning the text and specifying a width:
>>> '{:<30}'.format('left aligned')
'left aligned '
>>> '{:>30}'.format('right aligned')
' right aligned'
>>> '{:^30}'.format('centered')
' centered '
>>> '{:*^30}'.format('centered') # use '*' as a fill char
'***********centered***********'
Replacing "%+f", "%-f", and "% f" and specifying a sign:
>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000'
Replacing "%x" and "%o" and converting the value to different bases:
>>> # format also supports binary numbers
>>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)
'int: 42; hex: 2a; oct: 52; bin: 101010'
>>> # with 0x, 0o, or 0b as prefix:
>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
Using the comma or the underscore as a digit group separator:
>>> '{:,}'.format(1234567890)
'1,234,567,890'
>>> '{:_}'.format(1234567890)
'1_234_567_890'
>>> '{:_b}'.format(1234567890)
'100_1001_1001_0110_0000_0010_1101_0010'
>>> '{:_x}'.format(1234567890)
'4996_02d2'
>>> '{:_}'.format(123456789.123456789)
'123_456_789.12345679'
>>> '{:.,}'.format(123456789.123456789)
'123456789.123,456,79'
>>> '{:,._}'.format(123456789.123456789)
'123,456,789.123_456_79'
Expressing a percentage:
>>> points = 19
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%'
Using type-specific formatting:
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'
Nesting arguments and more complex examples:
>>> for align, text in zip('<^>', ['left', 'center', 'right']):
... '{0:{fill}{align}16}'.format(text, fill=align, align=align)
...
'left<<<<<<<<<<<<'
'^^^^^center^^^^^'
'>>>>>>>>>>>right'
>>>
>>> octets = [192, 168, 0, 1]
>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
'C0A80001'
>>> int(_, 16)
3232235521
>>>
>>> width = 5
>>> for num in range(5,12):
... for base in 'dXob':
... print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ')
... print()
...
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011
''',
'function': r'''Function definitions
********************
A function definition defines a user-defined function object (see
section The standard type hierarchy):
funcdef: [decorators] "def" funcname [type_params] "(" [parameter_list] ")"
["->" expression] ":" suite
decorators: decorator+
decorator: "@" assignment_expression NEWLINE
parameter_list: defparameter ("," defparameter)* "," "/" ["," [parameter_list_no_posonly]]
| parameter_list_no_posonly
parameter_list_no_posonly: defparameter ("," defparameter)* ["," [parameter_list_starargs]]
| parameter_list_starargs
parameter_list_starargs: "*" [star_parameter] ("," defparameter)* ["," [parameter_star_kwargs]]
| "*" ("," defparameter)+ ["," [parameter_star_kwargs]]
| parameter_star_kwargs
parameter_star_kwargs: "**" parameter [","]
parameter: identifier [":" expression]
star_parameter: identifier [":" ["*"] expression]
defparameter: parameter ["=" expression]
funcname: identifier
A function definition is an executable statement. Its execution binds
the function name in the current local namespace to a function object
(a wrapper around the executable code for the function). This
function object contains a reference to the current global namespace
as the global namespace to be used when the function is called.
The function definition does not execute the function body; this gets
executed only when the function is called. [4]
A function definition may be wrapped by one or more *decorator*
expressions. Decorator expressions are evaluated when the function is
defined, in the scope that contains the function definition. The
result must be a callable, which is invoked with the function object
as the only argument. The returned value is bound to the function name
instead of the function object. Multiple decorators are applied in
nested fashion. For example, the following code
@f1(arg)
@f2
def func(): pass
is roughly equivalent to
def func(): pass
func = f1(arg)(f2(func))
except that the original function is not temporarily bound to the name
"func".
Changed in version 3.9: Functions may be decorated with any valid
"assignment_expression". Previously, the grammar was much more
restrictive; see **PEP 614** for details.
A list of type parameters may be given in square brackets between the
function’s name and the opening parenthesis for its parameter list.
This indicates to static type checkers that the function is generic.
At runtime, the type parameters can be retrieved from the function’s
"__type_params__" attribute. See Generic functions for more.
Changed in version 3.12: Type parameter lists are new in Python 3.12.
When one or more *parameters* have the form *parameter* "="
*expression*, the function is said to have “default parameter values.”
For a parameter with a default value, the corresponding *argument* may
be omitted from a call, in which case the parameter’s default value is
substituted. If a parameter has a default value, all following
parameters up until the “"*"” must also have a default value — this is
a syntactic restriction that is not expressed by the grammar.
**Default parameter values are evaluated from left to right when the
function definition is executed.** This means that the expression is
evaluated once, when the function is defined, and that the same “pre-
computed” value is used for each call. This is especially important
to understand when a default parameter value is a mutable object, such
as a list or a dictionary: if the function modifies the object (e.g.
by appending an item to a list), the default parameter value is in
effect modified. This is generally not what was intended. A way
around this is to use "None" as the default, and explicitly test for
it in the body of the function, e.g.:
def whats_on_the_telly(penguin=None):
if penguin is None:
penguin = []
penguin.append("property of the zoo")
return penguin
Function call semantics are described in more detail in section Calls.
A function call always assigns values to all parameters mentioned in
the parameter list, either from positional arguments, from keyword
arguments, or from default values. If the form “"*identifier"” is
present, it is initialized to a tuple receiving any excess positional
parameters, defaulting to the empty tuple. If the form
“"**identifier"” is present, it is initialized to a new ordered
mapping receiving any excess keyword arguments, defaulting to a new
empty mapping of the same type. Parameters after “"*"” or
“"*identifier"” are keyword-only parameters and may only be passed by
keyword arguments. Parameters before “"/"” are positional-only
parameters and may only be passed by positional arguments.
Changed in version 3.8: The "/" function parameter syntax may be used
to indicate positional-only parameters. See **PEP 570** for details.
Parameters may have an *annotation* of the form “": expression"”
following the parameter name. Any parameter may have an annotation,
even those of the form "*identifier" or "**identifier". (As a special
case, parameters of the form "*identifier" may have an annotation “":
*expression"”.) Functions may have “return” annotation of the form
“"-> expression"” after the parameter list. These annotations can be
any valid Python expression. The presence of annotations does not
change the semantics of a function. See Annotations for more
information on annotations.
Changed in version 3.11: Parameters of the form “"*identifier"” may
have an annotation “": *expression"”. See **PEP 646**.
It is also possible to create anonymous functions (functions not bound
to a name), for immediate use in expressions. This uses lambda
expressions, described in section Lambdas. Note that the lambda
expression is merely a shorthand for a simplified function definition;
a function defined in a “"def"” statement can be passed around or
assigned to another name just like a function defined by a lambda
expression. The “"def"” form is actually more powerful since it
allows the execution of multiple statements and annotations.
**Programmer’s note:** Functions are first-class objects. A “"def"”
statement executed inside a function definition defines a local
function that can be returned or passed around. Free variables used
in the nested function can access the local variables of the function
containing the def. See section Naming and binding for details.
See also:
**PEP 3107** - Function Annotations
The original specification for function annotations.
**PEP 484** - Type Hints
Definition of a standard meaning for annotations: type hints.
**PEP 526** - Syntax for Variable Annotations
Ability to type hint variable declarations, including class
variables and instance variables.
**PEP 563** - Postponed Evaluation of Annotations
Support for forward references within annotations by preserving
annotations in a string form at runtime instead of eager
evaluation.
**PEP 318** - Decorators for Functions and Methods
Function and method decorators were introduced. Class decorators
were introduced in **PEP 3129**.
''',
'global': r'''The "global" statement
**********************
global_stmt: "global" identifier ("," identifier)*
The "global" statement causes the listed identifiers to be interpreted
as globals. It would be impossible to assign to a global variable
without "global", although free variables may refer to globals without
being declared global.
The "global" statement applies to the entire scope of a function or
class body. A "SyntaxError" is raised if a variable is used or
assigned to prior to its global declaration in the scope.
**Programmer’s note:** "global" is a directive to the parser. It
applies only to code parsed at the same time as the "global"
statement. In particular, a "global" statement contained in a string
or code object supplied to the built-in "exec()" function does not
affect the code block *containing* the function call, and code
contained in such a string is unaffected by "global" statements in the
code containing the function call. The same applies to the "eval()"
and "compile()" functions.
''',
'id-classes': r'''Reserved classes of identifiers
*******************************
Certain classes of identifiers (besides keywords) have special
meanings. These classes are identified by the patterns of leading and
trailing underscore characters:
"_*"
Not imported by "from module import *".
"_"
In a "case" pattern within a "match" statement, "_" is a soft
keyword that denotes a wildcard.
Separately, the interactive interpreter makes the result of the
last evaluation available in the variable "_". (It is stored in the
"builtins" module, alongside built-in functions like "print".)
Elsewhere, "_" is a regular identifier. It is often used to name
“special” items, but it is not special to Python itself.
Note:
The name "_" is often used in conjunction with
internationalization; refer to the documentation for the
"gettext" module for more information on this convention.It is
also commonly used for unused variables.
"__*__"
System-defined names, informally known as “dunder” names. These
names are defined by the interpreter and its implementation
(including the standard library). Current system names are
discussed in the Special method names section and elsewhere. More
will likely be defined in future versions of Python. *Any* use of
"__*__" names, in any context, that does not follow explicitly
documented use, is subject to breakage without warning.
"__*"
Class-private names. Names in this category, when used within the
context of a class definition, are re-written to use a mangled form
to help avoid name clashes between “private” attributes of base and
derived classes. See section Identifiers (Names).
''',
'identifiers': r'''Identifiers and keywords
************************
Identifiers (also referred to as *names*) are described by the
following lexical definitions.
The syntax of identifiers in Python is based on the Unicode standard
annex UAX-31, with elaboration and changes as defined below; see also
**PEP 3131** for further details.
Within the ASCII range (U+0001..U+007F), the valid characters for
identifiers include the uppercase and lowercase letters "A" through
"Z", the underscore "_" and, except for the first character, the
digits "0" through "9". Python 3.0 introduced additional characters
from outside the ASCII range (see **PEP 3131**). For these
characters, the classification uses the version of the Unicode
Character Database as included in the "unicodedata" module.
Identifiers are unlimited in length. Case is significant.
identifier: xid_start xid_continue*
id_start: <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
id_continue: <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
xid_start: <all characters in id_start whose NFKC normalization is in "id_start xid_continue*">
xid_continue: <all characters in id_continue whose NFKC normalization is in "id_continue*">
The Unicode category codes mentioned above stand for:
* *Lu* - uppercase letters
* *Ll* - lowercase letters
* *Lt* - titlecase letters
* *Lm* - modifier letters
* *Lo* - other letters
* *Nl* - letter numbers
* *Mn* - nonspacing marks
* *Mc* - spacing combining marks
* *Nd* - decimal numbers
* *Pc* - connector punctuations
* *Other_ID_Start* - explicit list of characters in PropList.txt to
support backwards compatibility
* *Other_ID_Continue* - likewise
All identifiers are converted into the normal form NFKC while parsing;
comparison of identifiers is based on NFKC.
A non-normative HTML file listing all valid identifier characters for
Unicode 16.0.0 can be found at
https://www.unicode.org/Public/16.0.0/ucd/DerivedCoreProperties.txt
Keywords
========
The following identifiers are used as reserved words, or *keywords* of
the language, and cannot be used as ordinary identifiers. They must
be spelled exactly as written here:
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
Soft Keywords
=============
Added in version 3.10.
Some identifiers are only reserved under specific contexts. These are
known as *soft keywords*. The identifiers "match", "case", "type" and
"_" can syntactically act as keywords in certain contexts, but this
distinction is done at the parser level, not when tokenizing.
As soft keywords, their use in the grammar is possible while still
preserving compatibility with existing code that uses these names as
identifier names.
"match", "case", and "_" are used in the "match" statement. "type" is
used in the "type" statement.
Changed in version 3.12: "type" is now a soft keyword.
Reserved classes of identifiers
===============================
Certain classes of identifiers (besides keywords) have special
meanings. These classes are identified by the patterns of leading and
trailing underscore characters:
"_*"
Not imported by "from module import *".
"_"
In a "case" pattern within a "match" statement, "_" is a soft
keyword that denotes a wildcard.
Separately, the interactive interpreter makes the result of the
last evaluation available in the variable "_". (It is stored in the
"builtins" module, alongside built-in functions like "print".)
Elsewhere, "_" is a regular identifier. It is often used to name
“special” items, but it is not special to Python itself.
Note:
The name "_" is often used in conjunction with
internationalization; refer to the documentation for the
"gettext" module for more information on this convention.It is
also commonly used for unused variables.
"__*__"
System-defined names, informally known as “dunder” names. These
names are defined by the interpreter and its implementation
(including the standard library). Current system names are
discussed in the Special method names section and elsewhere. More
will likely be defined in future versions of Python. *Any* use of
"__*__" names, in any context, that does not follow explicitly
documented use, is subject to breakage without warning.
"__*"
Class-private names. Names in this category, when used within the
context of a class definition, are re-written to use a mangled form
to help avoid name clashes between “private” attributes of base and
derived classes. See section Identifiers (Names).
''',
'if': r'''The "if" statement
******************
The "if" statement is used for conditional execution:
if_stmt: "if" assignment_expression ":" suite
("elif" assignment_expression ":" suite)*
["else" ":" suite]
It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section Boolean operations
for the definition of true and false); then that suite is executed
(and no other part of the "if" statement is executed or evaluated).
If all expressions are false, the suite of the "else" clause, if
present, is executed.
''',
'imaginary': r'''Imaginary literals
******************
Imaginary literals are described by the following lexical definitions:
imagnumber: (floatnumber | digitpart) ("j" | "J")
An imaginary literal yields a complex number with a real part of 0.0.
Complex numbers are represented as a pair of floating-point numbers
and have the same restrictions on their range. To create a complex
number with a nonzero real part, add a floating-point number to it,
e.g., "(3+4j)". Some examples of imaginary literals:
3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j
''',
'import': r'''The "import" statement
**********************
import_stmt: "import" module ["as" identifier] ("," module ["as" identifier])*
| "from" relative_module "import" identifier ["as" identifier]
("," identifier ["as" identifier])*
| "from" relative_module "import" "(" identifier ["as" identifier]
("," identifier ["as" identifier])* [","] ")"
| "from" relative_module "import" "*"
module: (identifier ".")* identifier
relative_module: "."* module | "."+
The basic import statement (no "from" clause) is executed in two
steps:
1. find a module, loading and initializing it if necessary
2. define a name or names in the local namespace for the scope where
the "import" statement occurs.
When the statement contains multiple clauses (separated by commas) the
two steps are carried out separately for each clause, just as though
the clauses had been separated out into individual import statements.
The details of the first step, finding and loading modules, are
described in greater detail in the section on the import system, which
also describes the various types of packages and modules that can be
imported, as well as all the hooks that can be used to customize the
import system. Note that failures in this step may indicate either
that the module could not be located, *or* that an error occurred
while initializing the module, which includes execution of the
module’s code.
If the requested module is retrieved successfully, it will be made
available in the local namespace in one of three ways:
* If the module name is followed by "as", then the name following "as"
is bound directly to the imported module.
* If no other name is specified, and the module being imported is a
top level module, the module’s name is bound in the local namespace
as a reference to the imported module
* If the module being imported is *not* a top level module, then the
name of the top level package that contains the module is bound in
the local namespace as a reference to the top level package. The
imported module must be accessed using its full qualified name
rather than directly
The "from" form uses a slightly more complex process:
1. find the module specified in the "from" clause, loading and
initializing it if necessary;
2. for each of the identifiers specified in the "import" clauses:
1. check if the imported module has an attribute by that name
2. if not, attempt to import a submodule with that name and then
check the imported module again for that attribute
3. if the attribute is not found, "ImportError" is raised.
4. otherwise, a reference to that value is stored in the local
namespace, using the name in the "as" clause if it is present,
otherwise using the attribute name
Examples:
import foo # foo imported and bound locally
import foo.bar.baz # foo, foo.bar, and foo.bar.baz imported, foo bound locally
import foo.bar.baz as fbb # foo, foo.bar, and foo.bar.baz imported, foo.bar.baz bound as fbb
from foo.bar import baz # foo, foo.bar, and foo.bar.baz imported, foo.bar.baz bound as baz
from foo import attr # foo imported and foo.attr bound as attr
If the list of identifiers is replaced by a star ("'*'"), all public
names defined in the module are bound in the local namespace for the
scope where the "import" statement occurs.
The *public names* defined by a module are determined by checking the
module’s namespace for a variable named "__all__"; if defined, it must
be a sequence of strings which are names defined or imported by that
module. The names given in "__all__" are all considered public and
are required to exist. If "__all__" is not defined, the set of public
names includes all names found in the module’s namespace which do not
begin with an underscore character ("'_'"). "__all__" should contain
the entire public API. It is intended to avoid accidentally exporting
items that are not part of the API (such as library modules which were
imported and used within the module).
The wild card form of import — "from module import *" — is only
allowed at the module level. Attempting to use it in class or
function definitions will raise a "SyntaxError".
When specifying what module to import you do not have to specify the
absolute name of the module. When a module or package is contained
within another package it is possible to make a relative import within
the same top package without having to mention the package name. By
using leading dots in the specified module or package after "from" you
can specify how high to traverse up the current package hierarchy
without specifying exact names. One leading dot means the current
package where the module making the import exists. Two dots means up
one package level. Three dots is up two levels, etc. So if you execute
"from . import mod" from a module in the "pkg" package then you will
end up importing "pkg.mod". If you execute "from ..subpkg2 import mod"
from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The
specification for relative imports is contained in the Package
Relative Imports section.
"importlib.import_module()" is provided to support applications that
determine dynamically the modules to be loaded.
Raises an auditing event "import" with arguments "module", "filename",
"sys.path", "sys.meta_path", "sys.path_hooks".
Future statements
=================
A *future statement* is a directive to the compiler that a particular
module should be compiled using syntax or semantics that will be
available in a specified future release of Python where the feature
becomes standard.
The future statement is intended to ease migration to future versions
of Python that introduce incompatible changes to the language. It
allows use of the new features on a per-module basis before the
release in which the feature becomes standard.
future_stmt: "from" "__future__" "import" feature ["as" identifier]
("," feature ["as" identifier])*
| "from" "__future__" "import" "(" feature ["as" identifier]
("," feature ["as" identifier])* [","] ")"
feature: identifier
A future statement must appear near the top of the module. The only
lines that can appear before a future statement are:
* the module docstring (if any),
* comments,
* blank lines, and
* other future statements.
The only feature that requires using the future statement is
"annotations" (see **PEP 563**).
All historical features enabled by the future statement are still
recognized by Python 3. The list includes "absolute_import",
"division", "generators", "generator_stop", "unicode_literals",
"print_function", "nested_scopes" and "with_statement". They are all
redundant because they are always enabled, and only kept for backwards
compatibility.
A future statement is recognized and treated specially at compile
time: Changes to the semantics of core constructs are often
implemented by generating different code. It may even be the case
that a new feature introduces new incompatible syntax (such as a new
reserved word), in which case the compiler may need to parse the
module differently. Such decisions cannot be pushed off until
runtime.
For any given release, the compiler knows which feature names have
been defined, and raises a compile-time error if a future statement
contains a feature not known to it.
The direct runtime semantics are the same as for any import statement:
there is a standard module "__future__", described later, and it will
be imported in the usual way at the time the future statement is
executed.
The interesting runtime semantics depend on the specific feature
enabled by the future statement.
Note that there is nothing special about the statement:
import __future__ [as name]
That is not a future statement; it’s an ordinary import statement with
no special semantics or syntax restrictions.
Code compiled by calls to the built-in functions "exec()" and
"compile()" that occur in a module "M" containing a future statement
will, by default, use the new syntax or semantics associated with the
future statement. This can be controlled by optional arguments to
"compile()" — see the documentation of that function for details.
A future statement typed at an interactive interpreter prompt will
take effect for the rest of the interpreter session. If an
interpreter is started with the "-i" option, is passed a script name
to execute, and the script includes a future statement, it will be in
effect in the interactive session started after the script is
executed.
See also:
**PEP 236** - Back to the __future__
The original proposal for the __future__ mechanism.
''',
'in': r'''Membership test operations
**************************
The operators "in" and "not in" test for membership. "x in s"
evaluates to "True" if *x* is a member of *s*, and "False" otherwise.
"x not in s" returns the negation of "x in s". All built-in sequences
and set types support this as well as dictionary, for which "in" tests
whether the dictionary has a given key. For container types such as
list, tuple, set, frozenset, dict, or collections.deque, the
expression "x in y" is equivalent to "any(x is e or x == e for e in
y)".
For the string and bytes types, "x in y" is "True" if and only if *x*
is a substring of *y*. An equivalent test is "y.find(x) != -1".
Empty strings are always considered to be a substring of any other
string, so """ in "abc"" will return "True".
For user-defined classes which define the "__contains__()" method, "x
in y" returns "True" if "y.__contains__(x)" returns a true value, and
"False" otherwise.
For user-defined classes which do not define "__contains__()" but do
define "__iter__()", "x in y" is "True" if some value "z", for which
the expression "x is z or x == z" is true, is produced while iterating
over "y". If an exception is raised during the iteration, it is as if
"in" raised that exception.
Lastly, the old-style iteration protocol is tried: if a class defines
"__getitem__()", "x in y" is "True" if and only if there is a non-
negative integer index *i* such that "x is y[i] or x == y[i]", and no
lower integer index raises the "IndexError" exception. (If any other
exception is raised, it is as if "in" raised that exception).
The operator "not in" is defined to have the inverse truth value of
"in".
''',
'integers': r'''Integer literals
****************
Integer literals are described by the following lexical definitions:
integer: decinteger | bininteger | octinteger | hexinteger
decinteger: nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")*
bininteger: "0" ("b" | "B") (["_"] bindigit)+
octinteger: "0" ("o" | "O") (["_"] octdigit)+
hexinteger: "0" ("x" | "X") (["_"] hexdigit)+
nonzerodigit: "1"..."9"
digit: "0"..."9"
bindigit: "0" | "1"
octdigit: "0"..."7"
hexdigit: digit | "a"..."f" | "A"..."F"
There is no limit for the length of integer literals apart from what
can be stored in available memory.
Underscores are ignored for determining the numeric value of the
literal. They can be used to group digits for enhanced readability.
One underscore can occur between digits, and after base specifiers
like "0x".
Note that leading zeros in a non-zero decimal number are not allowed.
This is for disambiguation with C-style octal literals, which Python
used before version 3.0.
Some examples of integer literals:
7 2147483647 0o177 0b100110111
3 79228162514264337593543950336 0o377 0xdeadbeef
100_000_000_000 0b_1110_0101
Changed in version 3.6: Underscores are now allowed for grouping
purposes in literals.
''',
'lambda': r'''Lambdas
*******
lambda_expr: "lambda" [parameter_list] ":" expression
Lambda expressions (sometimes called lambda forms) are used to create
anonymous functions. The expression "lambda parameters: expression"
yields a function object. The unnamed object behaves like a function
object defined with:
def <lambda>(parameters):
return expression
See section Function definitions for the syntax of parameter lists.
Note that functions created with lambda expressions cannot contain
statements or annotations.
''',
'lists': r'''List displays
*************
A list display is a possibly empty series of expressions enclosed in
square brackets:
list_display: "[" [flexible_expression_list | comprehension] "]"
A list display yields a new list object, the contents being specified
by either a list of expressions or a comprehension. When a comma-
separated list of expressions is supplied, its elements are evaluated
from left to right and placed into the list object in that order.
When a comprehension is supplied, the list is constructed from the
elements resulting from the comprehension.
''',
'naming': r'''Naming and binding
******************
Binding of names
================
*Names* refer to objects. Names are introduced by name binding
operations.
The following constructs bind names:
* formal parameters to functions,
* class definitions,
* function definitions,
* assignment expressions,
* targets that are identifiers if occurring in an assignment:
* "for" loop header,
* after "as" in a "with" statement, "except" clause, "except*"
clause, or in the as-pattern in structural pattern matching,
* in a capture pattern in structural pattern matching
* "import" statements.
* "type" statements.
* type parameter lists.
The "import" statement of the form "from ... import *" binds all names
defined in the imported module, except those beginning with an
underscore. This form may only be used at the module level.
A target occurring in a "del" statement is also considered bound for
this purpose (though the actual semantics are to unbind the name).
Each assignment or import statement occurs within a block defined by a
class or function definition or at the module level (the top-level
code block).
If a name is bound in a block, it is a local variable of that block,
unless declared as "nonlocal" or "global". If a name is bound at the
module level, it is a global variable. (The variables of the module
code block are local and global.) If a variable is used in a code
block but not defined there, it is a *free variable*.
Each occurrence of a name in the program text refers to the *binding*
of that name established by the following name resolution rules.
Resolution of names
===================
A *scope* defines the visibility of a name within a block. If a local
variable is defined in a block, its scope includes that block. If the
definition occurs in a function block, the scope extends to any blocks
contained within the defining one, unless a contained block introduces
a different binding for the name.
When a name is used in a code block, it is resolved using the nearest
enclosing scope. The set of all such scopes visible to a code block
is called the block’s *environment*.
When a name is not found at all, a "NameError" exception is raised. If
the current scope is a function scope, and the name refers to a local
variable that has not yet been bound to a value at the point where the
name is used, an "UnboundLocalError" exception is raised.
"UnboundLocalError" is a subclass of "NameError".
If a name binding operation occurs anywhere within a code block, all
uses of the name within the block are treated as references to the
current block. This can lead to errors when a name is used within a
block before it is bound. This rule is subtle. Python lacks
declarations and allows name binding operations to occur anywhere
within a code block. The local variables of a code block can be
determined by scanning the entire text of the block for name binding
operations. See the FAQ entry on UnboundLocalError for examples.
If the "global" statement occurs within a block, all uses of the names
specified in the statement refer to the bindings of those names in the
top-level namespace. Names are resolved in the top-level namespace by
searching the global namespace, i.e. the namespace of the module
containing the code block, and the builtins namespace, the namespace
of the module "builtins". The global namespace is searched first. If
the names are not found there, the builtins namespace is searched
next. If the names are also not found in the builtins namespace, new
variables are created in the global namespace. The global statement
must precede all uses of the listed names.
The "global" statement has the same scope as a name binding operation
in the same block. If the nearest enclosing scope for a free variable
contains a global statement, the free variable is treated as a global.
The "nonlocal" statement causes corresponding names to refer to
previously bound variables in the nearest enclosing function scope.
"SyntaxError" is raised at compile time if the given name does not
exist in any enclosing function scope. Type parameters cannot be
rebound with the "nonlocal" statement.
The namespace for a module is automatically created the first time a
module is imported. The main module for a script is always called
"__main__".
Class definition blocks and arguments to "exec()" and "eval()" are
special in the context of name resolution. A class definition is an
executable statement that may use and define names. These references
follow the normal rules for name resolution with an exception that
unbound local variables are looked up in the global namespace. The
namespace of the class definition becomes the attribute dictionary of
the class. The scope of names defined in a class block is limited to
the class block; it does not extend to the code blocks of methods.
This includes comprehensions and generator expressions, but it does
not include annotation scopes, which have access to their enclosing
class scopes. This means that the following will fail:
class A:
a = 42
b = list(a + i for i in range(10))
However, the following will succeed:
class A:
type Alias = Nested
class Nested: pass
print(A.Alias.__value__) # <type 'A.Nested'>
Annotation scopes
=================
*Annotations*, type parameter lists and "type" statements introduce
*annotation scopes*, which behave mostly like function scopes, but
with some exceptions discussed below.
Annotation scopes are used in the following contexts:
* *Function annotations*.
* *Variable annotations*.
* Type parameter lists for generic type aliases.
* Type parameter lists for generic functions. A generic function’s
annotations are executed within the annotation scope, but its
defaults and decorators are not.
* Type parameter lists for generic classes. A generic class’s base
classes and keyword arguments are executed within the annotation
scope, but its decorators are not.
* The bounds, constraints, and default values for type parameters
(lazily evaluated).
* The value of type aliases (lazily evaluated).
Annotation scopes differ from function scopes in the following ways:
* Annotation scopes have access to their enclosing class namespace. If
an annotation scope is immediately within a class scope, or within
another annotation scope that is immediately within a class scope,
the code in the annotation scope can use names defined in the class
scope as if it were executed directly within the class body. This
contrasts with regular functions defined within classes, which
cannot access names defined in the class scope.
* Expressions in annotation scopes cannot contain "yield", "yield
from", "await", or ":=" expressions. (These expressions are allowed
in other scopes contained within the annotation scope.)
* Names defined in annotation scopes cannot be rebound with "nonlocal"
statements in inner scopes. This includes only type parameters, as
no other syntactic elements that can appear within annotation scopes
can introduce new names.
* While annotation scopes have an internal name, that name is not
reflected in the *qualified name* of objects defined within the
scope. Instead, the "__qualname__" of such objects is as if the
object were defined in the enclosing scope.
Added in version 3.12: Annotation scopes were introduced in Python
3.12 as part of **PEP 695**.
Changed in version 3.13: Annotation scopes are also used for type
parameter defaults, as introduced by **PEP 696**.
Changed in version 3.14: Annotation scopes are now also used for
annotations, as specified in **PEP 649** and **PEP 749**.
Lazy evaluation
===============
Most annotation scopes are *lazily evaluated*. This includes
annotations, the values of type aliases created through the "type"
statement, and the bounds, constraints, and default values of type
variables created through the type parameter syntax. This means that
they are not evaluated when the type alias or type variable is
created, or when the object carrying annotations is created. Instead,
they are only evaluated when necessary, for example when the
"__value__" attribute on a type alias is accessed.
Example:
>>> type Alias = 1/0
>>> Alias.__value__
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
>>> def func[T: 1/0](): pass
>>> T = func.__type_params__[0]
>>> T.__bound__
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
Here the exception is raised only when the "__value__" attribute of
the type alias or the "__bound__" attribute of the type variable is
accessed.
This behavior is primarily useful for references to types that have
not yet been defined when the type alias or type variable is created.
For example, lazy evaluation enables creation of mutually recursive
type aliases:
from typing import Literal
type SimpleExpr = int | Parenthesized
type Parenthesized = tuple[Literal["("], Expr, Literal[")"]]
type Expr = SimpleExpr | tuple[SimpleExpr, Literal["+", "-"], Expr]
Lazily evaluated values are evaluated in annotation scope, which means
that names that appear inside the lazily evaluated value are looked up
as if they were used in the immediately enclosing scope.
Added in version 3.12.
Builtins and restricted execution
=================================
**CPython implementation detail:** Users should not touch
"__builtins__"; it is strictly an implementation detail. Users
wanting to override values in the builtins namespace should "import"
the "builtins" module and modify its attributes appropriately.
The builtins namespace associated with the execution of a code block
is actually found by looking up the name "__builtins__" in its global
namespace; this should be a dictionary or a module (in the latter case
the module’s dictionary is used). By default, when in the "__main__"
module, "__builtins__" is the built-in module "builtins"; when in any
other module, "__builtins__" is an alias for the dictionary of the
"builtins" module itself.
Interaction with dynamic features
=================================
Name resolution of free variables occurs at runtime, not at compile
time. This means that the following code will print 42:
i = 10
def f():
print(i)
i = 42
f()
The "eval()" and "exec()" functions do not have access to the full
environment for resolving names. Names may be resolved in the local
and global namespaces of the caller. Free variables are not resolved
in the nearest enclosing namespace, but in the global namespace. [1]
The "exec()" and "eval()" functions have optional arguments to
override the global and local namespace. If only one namespace is
specified, it is used for both.
''',
'nonlocal': r'''The "nonlocal" statement
************************
nonlocal_stmt: "nonlocal" identifier ("," identifier)*
When the definition of a function or class is nested (enclosed) within
the definitions of other functions, its nonlocal scopes are the local
scopes of the enclosing functions. The "nonlocal" statement causes the
listed identifiers to refer to names previously bound in nonlocal
scopes. It allows encapsulated code to rebind such nonlocal
identifiers. If a name is bound in more than one nonlocal scope, the
nearest binding is used. If a name is not bound in any nonlocal scope,
or if there is no nonlocal scope, a "SyntaxError" is raised.
The "nonlocal" statement applies to the entire scope of a function or
class body. A "SyntaxError" is raised if a variable is used or
assigned to prior to its nonlocal declaration in the scope.
See also:
**PEP 3104** - Access to Names in Outer Scopes
The specification for the "nonlocal" statement.
**Programmer’s note:** "nonlocal" is a directive to the parser and
applies only to code parsed along with it. See the note for the
"global" statement.
''',
'numbers': r'''Numeric literals
****************
There are three types of numeric literals: integers, floating-point
numbers, and imaginary numbers. There are no complex literals
(complex numbers can be formed by adding a real number and an
imaginary number).
Note that numeric literals do not include a sign; a phrase like "-1"
is actually an expression composed of the unary operator ‘"-"’ and the
literal "1".
''',
'numeric-types': r'''Emulating numeric types
***********************
The following methods can be defined to emulate numeric objects.
Methods corresponding to operations that are not supported by the
particular kind of number implemented (e.g., bitwise operations for
non-integral numbers) should be left undefined.
object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
object.__matmul__(self, other)
object.__truediv__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
object.__divmod__(self, other)
object.__pow__(self, other[, modulo])
object.__lshift__(self, other)
object.__rshift__(self, other)
object.__and__(self, other)
object.__xor__(self, other)
object.__or__(self, other)
These methods are called to implement the binary arithmetic
operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",
"pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to
evaluate the expression "x + y", where *x* is an instance of a
class that has an "__add__()" method, "type(x).__add__(x, y)" is
called. The "__divmod__()" method should be the equivalent to
using "__floordiv__()" and "__mod__()"; it should not be related to
"__truediv__()". Note that "__pow__()" should be defined to accept
an optional third argument if the three-argument version of the
built-in "pow()" function is to be supported.
If one of those methods does not support the operation with the
supplied arguments, it should return "NotImplemented".
object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rmatmul__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other[, modulo])
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)
These methods are called to implement the binary arithmetic
operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",
"pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped)
operands. These functions are only called if the operands are of
different types, when the left operand does not support the
corresponding operation [3], or the right operand’s class is
derived from the left operand’s class. [4] For instance, to
evaluate the expression "x - y", where *y* is an instance of a
class that has an "__rsub__()" method, "type(y).__rsub__(y, x)" is
called if "type(x).__sub__(x, y)" returns "NotImplemented" or
"type(y)" is a subclass of "type(x)". [5]
Note that "__rpow__()" should be defined to accept an optional
third argument if the three-argument version of the built-in
"pow()" function is to be supported.
Changed in version 3.14.0a7 (unreleased): Three-argument "pow()"
now try calling "__rpow__()" if necessary. Previously it was only
called in two-argument "pow()" and the binary power operator.
Note:
If the right operand’s type is a subclass of the left operand’s
type and that subclass provides a different implementation of the
reflected method for the operation, this method will be called
before the left operand’s non-reflected method. This behavior
allows subclasses to override their ancestors’ operations.
object.__iadd__(self, other)
object.__isub__(self, other)
object.__imul__(self, other)
object.__imatmul__(self, other)
object.__itruediv__(self, other)
object.__ifloordiv__(self, other)
object.__imod__(self, other)
object.__ipow__(self, other[, modulo])
object.__ilshift__(self, other)
object.__irshift__(self, other)
object.__iand__(self, other)
object.__ixor__(self, other)
object.__ior__(self, other)
These methods are called to implement the augmented arithmetic
assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=",
"<<=", ">>=", "&=", "^=", "|="). These methods should attempt to
do the operation in-place (modifying *self*) and return the result
(which could be, but does not have to be, *self*). If a specific
method is not defined, or if that method returns "NotImplemented",
the augmented assignment falls back to the normal methods. For
instance, if *x* is an instance of a class with an "__iadd__()"
method, "x += y" is equivalent to "x = x.__iadd__(y)" . If
"__iadd__()" does not exist, or if "x.__iadd__(y)" returns
"NotImplemented", "x.__add__(y)" and "y.__radd__(x)" are
considered, as with the evaluation of "x + y". In certain
situations, augmented assignment can result in unexpected errors
(see Why does a_tuple[i] += [‘item’] raise an exception when the
addition works?), but this behavior is in fact part of the data
model.
object.__neg__(self)
object.__pos__(self)
object.__abs__(self)
object.__invert__(self)
Called to implement the unary arithmetic operations ("-", "+",
"abs()" and "~").
object.__complex__(self)
object.__int__(self)
object.__float__(self)
Called to implement the built-in functions "complex()", "int()" and
"float()". Should return a value of the appropriate type.
object.__index__(self)
Called to implement "operator.index()", and whenever Python needs
to losslessly convert the numeric object to an integer object (such
as in slicing, or in the built-in "bin()", "hex()" and "oct()"
functions). Presence of this method indicates that the numeric
object is an integer type. Must return an integer.
If "__int__()", "__float__()" and "__complex__()" are not defined
then corresponding built-in functions "int()", "float()" and
"complex()" fall back to "__index__()".
object.__round__(self[, ndigits])
object.__trunc__(self)
object.__floor__(self)
object.__ceil__(self)
Called to implement the built-in function "round()" and "math"
functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is
passed to "__round__()" all these methods should return the value
of the object truncated to an "Integral" (typically an "int").
Changed in version 3.14: "int()" no longer delegates to the
"__trunc__()" method.
''',
'objects': r'''Objects, values and types
*************************
*Objects* are Python’s abstraction for data. All data in a Python
program is represented by objects or by relations between objects. (In
a sense, and in conformance to Von Neumann’s model of a “stored
program computer”, code is also represented by objects.)
Every object has an identity, a type and a value. An object’s
*identity* never changes once it has been created; you may think of it
as the object’s address in memory. The "is" operator compares the
identity of two objects; the "id()" function returns an integer
representing its identity.
**CPython implementation detail:** For CPython, "id(x)" is the memory
address where "x" is stored.
An object’s type determines the operations that the object supports
(e.g., “does it have a length?”) and also defines the possible values
for objects of that type. The "type()" function returns an object’s
type (which is an object itself). Like its identity, an object’s
*type* is also unchangeable. [1]
The *value* of some objects can change. Objects whose value can
change are said to be *mutable*; objects whose value is unchangeable
once they are created are called *immutable*. (The value of an
immutable container object that contains a reference to a mutable
object can change when the latter’s value is changed; however the
container is still considered immutable, because the collection of
objects it contains cannot be changed. So, immutability is not
strictly the same as having an unchangeable value, it is more subtle.)
An object’s mutability is determined by its type; for instance,
numbers, strings and tuples are immutable, while dictionaries and
lists are mutable.
Objects are never explicitly destroyed; however, when they become
unreachable they may be garbage-collected. An implementation is
allowed to postpone garbage collection or omit it altogether — it is a
matter of implementation quality how garbage collection is
implemented, as long as no objects are collected that are still
reachable.
**CPython implementation detail:** CPython currently uses a reference-
counting scheme with (optional) delayed detection of cyclically linked
garbage, which collects most objects as soon as they become
unreachable, but is not guaranteed to collect garbage containing
circular references. See the documentation of the "gc" module for
information on controlling the collection of cyclic garbage. Other
implementations act differently and CPython may change. Do not depend
on immediate finalization of objects when they become unreachable (so
you should always close files explicitly).
Note that the use of the implementation’s tracing or debugging
facilities may keep objects alive that would normally be collectable.
Also note that catching an exception with a "try"…"except" statement
may keep objects alive.
Some objects contain references to “external” resources such as open
files or windows. It is understood that these resources are freed
when the object is garbage-collected, but since garbage collection is
not guaranteed to happen, such objects also provide an explicit way to
release the external resource, usually a "close()" method. Programs
are strongly recommended to explicitly close such objects. The
"try"…"finally" statement and the "with" statement provide convenient
ways to do this.
Some objects contain references to other objects; these are called
*containers*. Examples of containers are tuples, lists and
dictionaries. The references are part of a container’s value. In
most cases, when we talk about the value of a container, we imply the
values, not the identities of the contained objects; however, when we
talk about the mutability of a container, only the identities of the
immediately contained objects are implied. So, if an immutable
container (like a tuple) contains a reference to a mutable object, its
value changes if that mutable object is changed.
Types affect almost all aspects of object behavior. Even the
importance of object identity is affected in some sense: for immutable
types, operations that compute new values may actually return a
reference to any existing object with the same type and value, while
for mutable objects this is not allowed. For example, after "a = 1; b
= 1", *a* and *b* may or may not refer to the same object with the
value one, depending on the implementation. This is because "int" is
an immutable type, so the reference to "1" can be reused. This
behaviour depends on the implementation used, so should not be relied
upon, but is something to be aware of when making use of object
identity tests. However, after "c = []; d = []", *c* and *d* are
guaranteed to refer to two different, unique, newly created empty
lists. (Note that "e = f = []" assigns the *same* object to both *e*
and *f*.)
''',
'operator-summary': r'''Operator precedence
*******************
The following table summarizes the operator precedence in Python, from
highest precedence (most binding) to lowest precedence (least
binding). Operators in the same box have the same precedence. Unless
the syntax is explicitly given, operators are binary. Operators in
the same box group left to right (except for exponentiation and
conditional expressions, which group from right to left).
Note that comparisons, membership tests, and identity tests, all have
the same precedence and have a left-to-right chaining feature as
described in the Comparisons section.
+-------------------------------------------------+---------------------------------------+
| Operator | Description |
|=================================================|=======================================|
| "(expressions...)", "[expressions...]", "{key: | Binding or parenthesized expression, |
| value...}", "{expressions...}" | list display, dictionary display, set |
| | display |
+-------------------------------------------------+---------------------------------------+
| "x[index]", "x[index:index]", | Subscription, slicing, call, |
| "x(arguments...)", "x.attribute" | attribute reference |
+-------------------------------------------------+---------------------------------------+
| "await x" | Await expression |
+-------------------------------------------------+---------------------------------------+
| "**" | Exponentiation [5] |
+-------------------------------------------------+---------------------------------------+
| "+x", "-x", "~x" | Positive, negative, bitwise NOT |
+-------------------------------------------------+---------------------------------------+
| "*", "@", "/", "//", "%" | Multiplication, matrix |
| | multiplication, division, floor |
| | division, remainder [6] |
+-------------------------------------------------+---------------------------------------+
| "+", "-" | Addition and subtraction |
+-------------------------------------------------+---------------------------------------+
| "<<", ">>" | Shifts |
+-------------------------------------------------+---------------------------------------+
| "&" | Bitwise AND |
+-------------------------------------------------+---------------------------------------+
| "^" | Bitwise XOR |
+-------------------------------------------------+---------------------------------------+
| "|" | Bitwise OR |
+-------------------------------------------------+---------------------------------------+
| "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership |
| ">=", "!=", "==" | tests and identity tests |
+-------------------------------------------------+---------------------------------------+
| "not x" | Boolean NOT |
+-------------------------------------------------+---------------------------------------+
| "and" | Boolean AND |
+-------------------------------------------------+---------------------------------------+
| "or" | Boolean OR |
+-------------------------------------------------+---------------------------------------+
| "if" – "else" | Conditional expression |
+-------------------------------------------------+---------------------------------------+
| "lambda" | Lambda expression |
+-------------------------------------------------+---------------------------------------+
| ":=" | Assignment expression |
+-------------------------------------------------+---------------------------------------+
-[ Footnotes ]-
[1] While "abs(x%y) < abs(y)" is true mathematically, for floats it
may not be true numerically due to roundoff. For example, and
assuming a platform on which a Python float is an IEEE 754 double-
precision number, in order that "-1e-100 % 1e100" have the same
sign as "1e100", the computed result is "-1e-100 + 1e100", which
is numerically exactly equal to "1e100". The function
"math.fmod()" returns a result whose sign matches the sign of the
first argument instead, and so returns "-1e-100" in this case.
Which approach is more appropriate depends on the application.
[2] If x is very close to an exact integer multiple of y, it’s
possible for "x//y" to be one larger than "(x-x%y)//y" due to
rounding. In such cases, Python returns the latter result, in
order to preserve that "divmod(x,y)[0] * y + x % y" be very close
to "x".
[3] The Unicode standard distinguishes between *code points* (e.g.
U+0041) and *abstract characters* (e.g. “LATIN CAPITAL LETTER A”).
While most abstract characters in Unicode are only represented
using one code point, there is a number of abstract characters
that can in addition be represented using a sequence of more than
one code point. For example, the abstract character “LATIN
CAPITAL LETTER C WITH CEDILLA” can be represented as a single
*precomposed character* at code position U+00C7, or as a sequence
of a *base character* at code position U+0043 (LATIN CAPITAL
LETTER C), followed by a *combining character* at code position
U+0327 (COMBINING CEDILLA).
The comparison operators on strings compare at the level of
Unicode code points. This may be counter-intuitive to humans. For
example, ""\u00C7" == "\u0043\u0327"" is "False", even though both
strings represent the same abstract character “LATIN CAPITAL
LETTER C WITH CEDILLA”.
To compare strings at the level of abstract characters (that is,
in a way intuitive to humans), use "unicodedata.normalize()".
[4] Due to automatic garbage-collection, free lists, and the dynamic
nature of descriptors, you may notice seemingly unusual behaviour
in certain uses of the "is" operator, like those involving
comparisons between instance methods, or constants. Check their
documentation for more info.
[5] The power operator "**" binds less tightly than an arithmetic or
bitwise unary operator on its right, that is, "2**-1" is "0.5".
[6] The "%" operator is also used for string formatting; the same
precedence applies.
''',
'pass': r'''The "pass" statement
********************
pass_stmt: "pass"
"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:
def f(arg): pass # a function that does nothing (yet)
class C: pass # a class with no methods (yet)
''',
'power': r'''The power operator
******************
The power operator binds more tightly than unary operators on its
left; it binds less tightly than unary operators on its right. The
syntax is:
power: (await_expr | primary) ["**" u_expr]
Thus, in an unparenthesized sequence of power and unary operators, the
operators are evaluated from right to left (this does not constrain
the evaluation order for the operands): "-1**2" results in "-1".
The power operator has the same semantics as the built-in "pow()"
function, when called with two arguments: it yields its left argument
raised to the power of its right argument. The numeric arguments are
first converted to a common type, and the result is of that type.
For int operands, the result has the same type as the operands unless
the second argument is negative; in that case, all arguments are
converted to float and a float result is delivered. For example,
"10**2" returns "100", but "10**-2" returns "0.01".
Raising "0.0" to a negative power results in a "ZeroDivisionError".
Raising a negative number to a fractional power results in a "complex"
number. (In earlier versions it raised a "ValueError".)
This operation can be customized using the special "__pow__()" and
"__rpow__()" methods.
''',
'raise': r'''The "raise" statement
*********************
raise_stmt: "raise" [expression ["from" expression]]
If no expressions are present, "raise" re-raises the exception that is
currently being handled, which is also known as the *active
exception*. If there isn’t currently an active exception, a
"RuntimeError" exception is raised indicating that this is an error.
Otherwise, "raise" evaluates the first expression as the exception
object. It must be either a subclass or an instance of
"BaseException". If it is a class, the exception instance will be
obtained when needed by instantiating the class with no arguments.
The *type* of the exception is the exception instance’s class, the
*value* is the instance itself.
A traceback object is normally created automatically when an exception
is raised and attached to it as the "__traceback__" attribute. You can
create an exception and set your own traceback in one step using the
"with_traceback()" exception method (which returns the same exception
instance, with its traceback set to its argument), like so:
raise Exception("foo occurred").with_traceback(tracebackobj)
The "from" clause is used for exception chaining: if given, the second
*expression* must be another exception class or instance. If the
second expression is an exception instance, it will be attached to the
raised exception as the "__cause__" attribute (which is writable). If
the expression is an exception class, the class will be instantiated
and the resulting exception instance will be attached to the raised
exception as the "__cause__" attribute. If the raised exception is not
handled, both exceptions will be printed:
>>> try:
... print(1 / 0)
... except Exception as exc:
... raise RuntimeError("Something bad happened") from exc
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
print(1 / 0)
~~^~~
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
raise RuntimeError("Something bad happened") from exc
RuntimeError: Something bad happened
A similar mechanism works implicitly if a new exception is raised when
an exception is already being handled. An exception may be handled
when an "except" or "finally" clause, or a "with" statement, is used.
The previous exception is then attached as the new exception’s
"__context__" attribute:
>>> try:
... print(1 / 0)
... except:
... raise RuntimeError("Something bad happened")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
print(1 / 0)
~~^~~
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
raise RuntimeError("Something bad happened")
RuntimeError: Something bad happened
Exception chaining can be explicitly suppressed by specifying "None"
in the "from" clause:
>>> try:
... print(1 / 0)
... except:
... raise RuntimeError("Something bad happened") from None
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened
Additional information on exceptions can be found in section
Exceptions, and information about handling exceptions is in section
The try statement.
Changed in version 3.3: "None" is now permitted as "Y" in "raise X
from Y".Added the "__suppress_context__" attribute to suppress
automatic display of the exception context.
Changed in version 3.11: If the traceback of the active exception is
modified in an "except" clause, a subsequent "raise" statement re-
raises the exception with the modified traceback. Previously, the
exception was re-raised with the traceback it had when it was caught.
''',
'return': r'''The "return" statement
**********************
return_stmt: "return" [expression_list]
"return" may only occur syntactically nested in a function definition,
not within a nested class definition.
If an expression list is present, it is evaluated, else "None" is
substituted.
"return" leaves the current function call with the expression list (or
"None") as return value.
When "return" passes control out of a "try" statement with a "finally"
clause, that "finally" clause is executed before really leaving the
function.
In a generator function, the "return" statement indicates that the
generator is done and will cause "StopIteration" to be raised. The
returned value (if any) is used as an argument to construct
"StopIteration" and becomes the "StopIteration.value" attribute.
In an asynchronous generator function, an empty "return" statement
indicates that the asynchronous generator is done and will cause
"StopAsyncIteration" to be raised. A non-empty "return" statement is
a syntax error in an asynchronous generator function.
''',
'sequence-types': r'''Emulating container types
*************************
The following methods can be defined to implement container objects.
None of them are provided by the "object" class itself. Containers
usually are *sequences* (such as "lists" or "tuples") or *mappings*
(like *dictionaries*), but can represent other containers as well.
The first set of methods is used either to emulate a sequence or to
emulate a mapping; the difference is that for a sequence, the
allowable keys should be the integers *k* for which "0 <= k < N" where
*N* is the length of the sequence, or "slice" objects, which define a
range of items. It is also recommended that mappings provide the
methods "keys()", "values()", "items()", "get()", "clear()",
"setdefault()", "pop()", "popitem()", "copy()", and "update()"
behaving similar to those for Python’s standard "dictionary" objects.
The "collections.abc" module provides a "MutableMapping" *abstract
base class* to help create those methods from a base set of
"__getitem__()", "__setitem__()", "__delitem__()", and "keys()".
Mutable sequences should provide methods "append()", "count()",
"index()", "extend()", "insert()", "pop()", "remove()", "reverse()"
and "sort()", like Python standard "list" objects. Finally, sequence
types should implement addition (meaning concatenation) and
multiplication (meaning repetition) by defining the methods
"__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and
"__imul__()" described below; they should not define other numerical
operators. It is recommended that both mappings and sequences
implement the "__contains__()" method to allow efficient use of the
"in" operator; for mappings, "in" should search the mapping’s keys;
for sequences, it should search through the values. It is further
recommended that both mappings and sequences implement the
"__iter__()" method to allow efficient iteration through the
container; for mappings, "__iter__()" should iterate through the
object’s keys; for sequences, it should iterate through the values.
object.__len__(self)
Called to implement the built-in function "len()". Should return
the length of the object, an integer ">=" 0. Also, an object that
doesn’t define a "__bool__()" method and whose "__len__()" method
returns zero is considered to be false in a Boolean context.
**CPython implementation detail:** In CPython, the length is
required to be at most "sys.maxsize". If the length is larger than
"sys.maxsize" some features (such as "len()") may raise
"OverflowError". To prevent raising "OverflowError" by truth value
testing, an object must define a "__bool__()" method.
object.__length_hint__(self)
Called to implement "operator.length_hint()". Should return an
estimated length for the object (which may be greater or less than
the actual length). The length must be an integer ">=" 0. The
return value may also be "NotImplemented", which is treated the
same as if the "__length_hint__" method didn’t exist at all. This
method is purely an optimization and is never required for
correctness.
Added in version 3.4.
Note:
Slicing is done exclusively with the following three methods. A
call like
a[1:2] = b
is translated to
a[slice(1, 2, None)] = b
and so forth. Missing slice items are always filled in with "None".
object.__getitem__(self, key)
Called to implement evaluation of "self[key]". For *sequence*
types, the accepted keys should be integers. Optionally, they may
support "slice" objects as well. Negative index support is also
optional. If *key* is of an inappropriate type, "TypeError" may be
raised; if *key* is a value outside the set of indexes for the
sequence (after any special interpretation of negative values),
"IndexError" should be raised. For *mapping* types, if *key* is
missing (not in the container), "KeyError" should be raised.
Note:
"for" loops expect that an "IndexError" will be raised for
illegal indexes to allow proper detection of the end of the
sequence.
Note:
When subscripting a *class*, the special class method
"__class_getitem__()" may be called instead of "__getitem__()".
See __class_getitem__ versus __getitem__ for more details.
object.__setitem__(self, key, value)
Called to implement assignment to "self[key]". Same note as for
"__getitem__()". This should only be implemented for mappings if
the objects support changes to the values for keys, or if new keys
can be added, or for sequences if elements can be replaced. The
same exceptions should be raised for improper *key* values as for
the "__getitem__()" method.
object.__delitem__(self, key)
Called to implement deletion of "self[key]". Same note as for
"__getitem__()". This should only be implemented for mappings if
the objects support removal of keys, or for sequences if elements
can be removed from the sequence. The same exceptions should be
raised for improper *key* values as for the "__getitem__()" method.
object.__missing__(self, key)
Called by "dict"."__getitem__()" to implement "self[key]" for dict
subclasses when key is not in the dictionary.
object.__iter__(self)
This method is called when an *iterator* is required for a
container. This method should return a new iterator object that can
iterate over all the objects in the container. For mappings, it
should iterate over the keys of the container.
object.__reversed__(self)
Called (if present) by the "reversed()" built-in to implement
reverse iteration. It should return a new iterator object that
iterates over all the objects in the container in reverse order.
If the "__reversed__()" method is not provided, the "reversed()"
built-in will fall back to using the sequence protocol ("__len__()"
and "__getitem__()"). Objects that support the sequence protocol
should only provide "__reversed__()" if they can provide an
implementation that is more efficient than the one provided by
"reversed()".
The membership test operators ("in" and "not in") are normally
implemented as an iteration through a container. However, container
objects can supply the following special method with a more efficient
implementation, which also does not require the object be iterable.
object.__contains__(self, item)
Called to implement membership test operators. Should return true
if *item* is in *self*, false otherwise. For mapping objects, this
should consider the keys of the mapping rather than the values or
the key-item pairs.
For objects that don’t define "__contains__()", the membership test
first tries iteration via "__iter__()", then the old sequence
iteration protocol via "__getitem__()", see this section in the
language reference.
''',
'shifting': r'''Shifting operations
*******************
The shifting operations have lower priority than the arithmetic
operations:
shift_expr: a_expr | shift_expr ("<<" | ">>") a_expr
These operators accept integers as arguments. They shift the first
argument to the left or right by the number of bits given by the
second argument.
The left shift operation can be customized using the special
"__lshift__()" and "__rlshift__()" methods. The right shift operation
can be customized using the special "__rshift__()" and "__rrshift__()"
methods.
A right shift by *n* bits is defined as floor division by "pow(2,n)".
A left shift by *n* bits is defined as multiplication with "pow(2,n)".
''',
'slicings': r'''Slicings
********
A slicing selects a range of items in a sequence object (e.g., a
string, tuple or list). Slicings may be used as expressions or as
targets in assignment or "del" statements. The syntax for a slicing:
slicing: primary "[" slice_list "]"
slice_list: slice_item ("," slice_item)* [","]
slice_item: expression | proper_slice
proper_slice: [lower_bound] ":" [upper_bound] [ ":" [stride] ]
lower_bound: expression
upper_bound: expression
stride: expression
There is ambiguity in the formal syntax here: anything that looks like
an expression list also looks like a slice list, so any subscription
can be interpreted as a slicing. Rather than further complicating the
syntax, this is disambiguated by defining that in this case the
interpretation as a subscription takes priority over the
interpretation as a slicing (this is the case if the slice list
contains no proper slice).
The semantics for a slicing are as follows. The primary is indexed
(using the same "__getitem__()" method as normal subscription) with a
key that is constructed from the slice list, as follows. If the slice
list contains at least one comma, the key is a tuple containing the
conversion of the slice items; otherwise, the conversion of the lone
slice item is the key. The conversion of a slice item that is an
expression is that expression. The conversion of a proper slice is a
slice object (see section The standard type hierarchy) whose "start",
"stop" and "step" attributes are the values of the expressions given
as lower bound, upper bound and stride, respectively, substituting
"None" for missing expressions.
''',
'specialattrs': r'''Special Attributes
******************
The implementation adds a few special read-only attributes to several
object types, where they are relevant. Some of these are not reported
by the "dir()" built-in function.
definition.__name__
The name of the class, function, method, descriptor, or generator
instance.
definition.__qualname__
The *qualified name* of the class, function, method, descriptor, or
generator instance.
Added in version 3.3.
definition.__module__
The name of the module in which a class or function was defined.
definition.__doc__
The documentation string of a class or function, or "None" if
undefined.
definition.__type_params__
The type parameters of generic classes, functions, and type
aliases. For classes and functions that are not generic, this will
be an empty tuple.
Added in version 3.12.
''',
'specialnames': r'''Special method names
********************
A class can implement certain operations that are invoked by special
syntax (such as arithmetic operations or subscripting and slicing) by
defining methods with special names. This is Python’s approach to
*operator overloading*, allowing classes to define their own behavior
with respect to language operators. For instance, if a class defines
a method named "__getitem__()", and "x" is an instance of this class,
then "x[i]" is roughly equivalent to "type(x).__getitem__(x, i)".
Except where mentioned, attempts to execute an operation raise an
exception when no appropriate method is defined (typically
"AttributeError" or "TypeError").
Setting a special method to "None" indicates that the corresponding
operation is not available. For example, if a class sets "__iter__()"
to "None", the class is not iterable, so calling "iter()" on its
instances will raise a "TypeError" (without falling back to
"__getitem__()"). [2]
When implementing a class that emulates any built-in type, it is
important that the emulation only be implemented to the degree that it
makes sense for the object being modelled. For example, some
sequences may work well with retrieval of individual elements, but
extracting a slice may not make sense. (One example of this is the
"NodeList" interface in the W3C’s Document Object Model.)
Basic customization
===================
object.__new__(cls[, ...])
Called to create a new instance of class *cls*. "__new__()" is a
static method (special-cased so you need not declare it as such)
that takes the class of which an instance was requested as its
first argument. The remaining arguments are those passed to the
object constructor expression (the call to the class). The return
value of "__new__()" should be the new object instance (usually an
instance of *cls*).
Typical implementations create a new instance of the class by
invoking the superclass’s "__new__()" method using
"super().__new__(cls[, ...])" with appropriate arguments and then
modifying the newly created instance as necessary before returning
it.
If "__new__()" is invoked during object construction and it returns
an instance of *cls*, then the new instance’s "__init__()" method
will be invoked like "__init__(self[, ...])", where *self* is the
new instance and the remaining arguments are the same as were
passed to the object constructor.
If "__new__()" does not return an instance of *cls*, then the new
instance’s "__init__()" method will not be invoked.
"__new__()" is intended mainly to allow subclasses of immutable
types (like int, str, or tuple) to customize instance creation. It
is also commonly overridden in custom metaclasses in order to
customize class creation.
object.__init__(self[, ...])
Called after the instance has been created (by "__new__()"), but
before it is returned to the caller. The arguments are those
passed to the class constructor expression. If a base class has an
"__init__()" method, the derived class’s "__init__()" method, if
any, must explicitly call it to ensure proper initialization of the
base class part of the instance; for example:
"super().__init__([args...])".
Because "__new__()" and "__init__()" work together in constructing
objects ("__new__()" to create it, and "__init__()" to customize
it), no non-"None" value may be returned by "__init__()"; doing so
will cause a "TypeError" to be raised at runtime.
object.__del__(self)
Called when the instance is about to be destroyed. This is also
called a finalizer or (improperly) a destructor. If a base class
has a "__del__()" method, the derived class’s "__del__()" method,
if any, must explicitly call it to ensure proper deletion of the
base class part of the instance.
It is possible (though not recommended!) for the "__del__()" method
to postpone destruction of the instance by creating a new reference
to it. This is called object *resurrection*. It is
implementation-dependent whether "__del__()" is called a second
time when a resurrected object is about to be destroyed; the
current *CPython* implementation only calls it once.
It is not guaranteed that "__del__()" methods are called for
objects that still exist when the interpreter exits.
"weakref.finalize" provides a straightforward way to register a
cleanup function to be called when an object is garbage collected.
Note:
"del x" doesn’t directly call "x.__del__()" — the former
decrements the reference count for "x" by one, and the latter is
only called when "x"’s reference count reaches zero.
**CPython implementation detail:** It is possible for a reference
cycle to prevent the reference count of an object from going to
zero. In this case, the cycle will be later detected and deleted
by the *cyclic garbage collector*. A common cause of reference
cycles is when an exception has been caught in a local variable.
The frame’s locals then reference the exception, which references
its own traceback, which references the locals of all frames caught
in the traceback.
See also: Documentation for the "gc" module.
Warning:
Due to the precarious circumstances under which "__del__()"
methods are invoked, exceptions that occur during their execution
are ignored, and a warning is printed to "sys.stderr" instead.
In particular:
* "__del__()" can be invoked when arbitrary code is being
executed, including from any arbitrary thread. If "__del__()"
needs to take a lock or invoke any other blocking resource, it
may deadlock as the resource may already be taken by the code
that gets interrupted to execute "__del__()".
* "__del__()" can be executed during interpreter shutdown. As a
consequence, the global variables it needs to access (including
other modules) may already have been deleted or set to "None".
Python guarantees that globals whose name begins with a single
underscore are deleted from their module before other globals
are deleted; if no other references to such globals exist, this
may help in assuring that imported modules are still available
at the time when the "__del__()" method is called.
object.__repr__(self)
Called by the "repr()" built-in function to compute the “official”
string representation of an object. If at all possible, this
should look like a valid Python expression that could be used to
recreate an object with the same value (given an appropriate
environment). If this is not possible, a string of the form
"<...some useful description...>" should be returned. The return
value must be a string object. If a class defines "__repr__()" but
not "__str__()", then "__repr__()" is also used when an “informal”
string representation of instances of that class is required.
This is typically used for debugging, so it is important that the
representation is information-rich and unambiguous. A default
implementation is provided by the "object" class itself.
object.__str__(self)
Called by "str(object)", the default "__format__()" implementation,
and the built-in function "print()", to compute the “informal” or
nicely printable string representation of an object. The return
value must be a str object.
This method differs from "object.__repr__()" in that there is no
expectation that "__str__()" return a valid Python expression: a
more convenient or concise representation can be used.
The default implementation defined by the built-in type "object"
calls "object.__repr__()".
object.__bytes__(self)
Called by bytes to compute a byte-string representation of an
object. This should return a "bytes" object. The "object" class
itself does not provide this method.
object.__format__(self, format_spec)
Called by the "format()" built-in function, and by extension,
evaluation of formatted string literals and the "str.format()"
method, to produce a “formatted” string representation of an
object. The *format_spec* argument is a string that contains a
description of the formatting options desired. The interpretation
of the *format_spec* argument is up to the type implementing
"__format__()", however most classes will either delegate
formatting to one of the built-in types, or use a similar
formatting option syntax.
See Format Specification Mini-Language for a description of the
standard formatting syntax.
The return value must be a string object.
The default implementation by the "object" class should be given an
empty *format_spec* string. It delegates to "__str__()".
Changed in version 3.4: The __format__ method of "object" itself
raises a "TypeError" if passed any non-empty string.
Changed in version 3.7: "object.__format__(x, '')" is now
equivalent to "str(x)" rather than "format(str(x), '')".
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
These are the so-called “rich comparison” methods. The
correspondence between operator symbols and method names is as
follows: "x<y" calls "x.__lt__(y)", "x<=y" calls "x.__le__(y)",
"x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", "x>y" calls
"x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".
A rich comparison method may return the singleton "NotImplemented"
if it does not implement the operation for a given pair of
arguments. By convention, "False" and "True" are returned for a
successful comparison. However, these methods can return any value,
so if the comparison operator is used in a Boolean context (e.g.,
in the condition of an "if" statement), Python will call "bool()"
on the value to determine if the result is true or false.
By default, "object" implements "__eq__()" by using "is", returning
"NotImplemented" in the case of a false comparison: "True if x is y
else NotImplemented". For "__ne__()", by default it delegates to
"__eq__()" and inverts the result unless it is "NotImplemented".
There are no other implied relationships among the comparison
operators or default implementations; for example, the truth of
"(x<y or x==y)" does not imply "x<=y". To automatically generate
ordering operations from a single root operation, see
"functools.total_ordering()".
By default, the "object" class provides implementations consistent
with Value comparisons: equality compares according to object
identity, and order comparisons raise "TypeError". Each default
method may generate these results directly, but may also return
"NotImplemented".
See the paragraph on "__hash__()" for some important notes on
creating *hashable* objects which support custom comparison
operations and are usable as dictionary keys.
There are no swapped-argument versions of these methods (to be used
when the left argument does not support the operation but the right
argument does); rather, "__lt__()" and "__gt__()" are each other’s
reflection, "__le__()" and "__ge__()" are each other’s reflection,
and "__eq__()" and "__ne__()" are their own reflection. If the
operands are of different types, and the right operand’s type is a
direct or indirect subclass of the left operand’s type, the
reflected method of the right operand has priority, otherwise the
left operand’s method has priority. Virtual subclassing is not
considered.
When no appropriate method returns any value other than
"NotImplemented", the "==" and "!=" operators will fall back to
"is" and "is not", respectively.
object.__hash__(self)
Called by built-in function "hash()" and for operations on members
of hashed collections including "set", "frozenset", and "dict".
The "__hash__()" method should return an integer. The only required
property is that objects which compare equal have the same hash
value; it is advised to mix together the hash values of the
components of the object that also play a part in comparison of
objects by packing them into a tuple and hashing the tuple.
Example:
def __hash__(self):
return hash((self.name, self.nick, self.color))
Note:
"hash()" truncates the value returned from an object’s custom
"__hash__()" method to the size of a "Py_ssize_t". This is
typically 8 bytes on 64-bit builds and 4 bytes on 32-bit builds.
If an object’s "__hash__()" must interoperate on builds of
different bit sizes, be sure to check the width on all supported
builds. An easy way to do this is with "python -c "import sys;
print(sys.hash_info.width)"".
If a class does not define an "__eq__()" method it should not
define a "__hash__()" operation either; if it defines "__eq__()"
but not "__hash__()", its instances will not be usable as items in
hashable collections. If a class defines mutable objects and
implements an "__eq__()" method, it should not implement
"__hash__()", since the implementation of *hashable* collections
requires that a key’s hash value is immutable (if the object’s hash
value changes, it will be in the wrong hash bucket).
User-defined classes have "__eq__()" and "__hash__()" methods by
default (inherited from the "object" class); with them, all objects
compare unequal (except with themselves) and "x.__hash__()" returns
an appropriate value such that "x == y" implies both that "x is y"
and "hash(x) == hash(y)".
A class that overrides "__eq__()" and does not define "__hash__()"
will have its "__hash__()" implicitly set to "None". When the
"__hash__()" method of a class is "None", instances of the class
will raise an appropriate "TypeError" when a program attempts to
retrieve their hash value, and will also be correctly identified as
unhashable when checking "isinstance(obj,
collections.abc.Hashable)".
If a class that overrides "__eq__()" needs to retain the
implementation of "__hash__()" from a parent class, the interpreter
must be told this explicitly by setting "__hash__ =
<ParentClass>.__hash__".
If a class that does not override "__eq__()" wishes to suppress
hash support, it should include "__hash__ = None" in the class
definition. A class which defines its own "__hash__()" that
explicitly raises a "TypeError" would be incorrectly identified as
hashable by an "isinstance(obj, collections.abc.Hashable)" call.
Note:
By default, the "__hash__()" values of str and bytes objects are
“salted” with an unpredictable random value. Although they
remain constant within an individual Python process, they are not
predictable between repeated invocations of Python.This is
intended to provide protection against a denial-of-service caused
by carefully chosen inputs that exploit the worst case
performance of a dict insertion, *O*(*n*^2) complexity. See
http://ocert.org/advisories/ocert-2011-003.html for
details.Changing hash values affects the iteration order of sets.
Python has never made guarantees about this ordering (and it
typically varies between 32-bit and 64-bit builds).See also
"PYTHONHASHSEED".
Changed in version 3.3: Hash randomization is enabled by default.
object.__bool__(self)
Called to implement truth value testing and the built-in operation
"bool()"; should return "False" or "True". When this method is not
defined, "__len__()" is called, if it is defined, and the object is
considered true if its result is nonzero. If a class defines
neither "__len__()" nor "__bool__()" (which is true of the "object"
class itself), all its instances are considered true.
Customizing attribute access
============================
The following methods can be defined to customize the meaning of
attribute access (use of, assignment to, or deletion of "x.name") for
class instances.
object.__getattr__(self, name)
Called when the default attribute access fails with an
"AttributeError" (either "__getattribute__()" raises an
"AttributeError" because *name* is not an instance attribute or an
attribute in the class tree for "self"; or "__get__()" of a *name*
property raises "AttributeError"). This method should either
return the (computed) attribute value or raise an "AttributeError"
exception. The "object" class itself does not provide this method.
Note that if the attribute is found through the normal mechanism,
"__getattr__()" is not called. (This is an intentional asymmetry
between "__getattr__()" and "__setattr__()".) This is done both for
efficiency reasons and because otherwise "__getattr__()" would have
no way to access other attributes of the instance. Note that at
least for instance variables, you can take total control by not
inserting any values in the instance attribute dictionary (but
instead inserting them in another object). See the
"__getattribute__()" method below for a way to actually get total
control over attribute access.
object.__getattribute__(self, name)
Called unconditionally to implement attribute accesses for
instances of the class. If the class also defines "__getattr__()",
the latter will not be called unless "__getattribute__()" either
calls it explicitly or raises an "AttributeError". This method
should return the (computed) attribute value or raise an
"AttributeError" exception. In order to avoid infinite recursion in
this method, its implementation should always call the base class
method with the same name to access any attributes it needs, for
example, "object.__getattribute__(self, name)".
Note:
This method may still be bypassed when looking up special methods
as the result of implicit invocation via language syntax or
built-in functions. See Special method lookup.
For certain sensitive attribute accesses, raises an auditing event
"object.__getattr__" with arguments "obj" and "name".
object.__setattr__(self, name, value)
Called when an attribute assignment is attempted. This is called
instead of the normal mechanism (i.e. store the value in the
instance dictionary). *name* is the attribute name, *value* is the
value to be assigned to it.
If "__setattr__()" wants to assign to an instance attribute, it
should call the base class method with the same name, for example,
"object.__setattr__(self, name, value)".
For certain sensitive attribute assignments, raises an auditing
event "object.__setattr__" with arguments "obj", "name", "value".
object.__delattr__(self, name)
Like "__setattr__()" but for attribute deletion instead of
assignment. This should only be implemented if "del obj.name" is
meaningful for the object.
For certain sensitive attribute deletions, raises an auditing event
"object.__delattr__" with arguments "obj" and "name".
object.__dir__(self)
Called when "dir()" is called on the object. An iterable must be
returned. "dir()" converts the returned iterable to a list and
sorts it.
Customizing module attribute access
-----------------------------------
Special names "__getattr__" and "__dir__" can be also used to
customize access to module attributes. The "__getattr__" function at
the module level should accept one argument which is the name of an
attribute and return the computed value or raise an "AttributeError".
If an attribute is not found on a module object through the normal
lookup, i.e. "object.__getattribute__()", then "__getattr__" is
searched in the module "__dict__" before raising an "AttributeError".
If found, it is called with the attribute name and the result is
returned.
The "__dir__" function should accept no arguments, and return an
iterable of strings that represents the names accessible on module. If
present, this function overrides the standard "dir()" search on a
module.
For a more fine grained customization of the module behavior (setting
attributes, properties, etc.), one can set the "__class__" attribute
of a module object to a subclass of "types.ModuleType". For example:
import sys
from types import ModuleType
class VerboseModule(ModuleType):
def __repr__(self):
return f'Verbose {self.__name__}'
def __setattr__(self, attr, value):
print(f'Setting {attr}...')
super().__setattr__(attr, value)
sys.modules[__name__].__class__ = VerboseModule
Note:
Defining module "__getattr__" and setting module "__class__" only
affect lookups made using the attribute access syntax – directly
accessing the module globals (whether by code within the module, or
via a reference to the module’s globals dictionary) is unaffected.
Changed in version 3.5: "__class__" module attribute is now writable.
Added in version 3.7: "__getattr__" and "__dir__" module attributes.
See also:
**PEP 562** - Module __getattr__ and __dir__
Describes the "__getattr__" and "__dir__" functions on modules.
Implementing Descriptors
------------------------
The following methods only apply when an instance of the class
containing the method (a so-called *descriptor* class) appears in an
*owner* class (the descriptor must be in either the owner’s class
dictionary or in the class dictionary for one of its parents). In the
examples below, “the attribute” refers to the attribute whose name is
the key of the property in the owner class’ "__dict__". The "object"
class itself does not implement any of these protocols.
object.__get__(self, instance, owner=None)
Called to get the attribute of the owner class (class attribute
access) or of an instance of that class (instance attribute
access). The optional *owner* argument is the owner class, while
*instance* is the instance that the attribute was accessed through,
or "None" when the attribute is accessed through the *owner*.
This method should return the computed attribute value or raise an
"AttributeError" exception.
**PEP 252** specifies that "__get__()" is callable with one or two
arguments. Python’s own built-in descriptors support this
specification; however, it is likely that some third-party tools
have descriptors that require both arguments. Python’s own
"__getattribute__()" implementation always passes in both arguments
whether they are required or not.
object.__set__(self, instance, value)
Called to set the attribute on an instance *instance* of the owner
class to a new value, *value*.
Note, adding "__set__()" or "__delete__()" changes the kind of
descriptor to a “data descriptor”. See Invoking Descriptors for
more details.
object.__delete__(self, instance)
Called to delete the attribute on an instance *instance* of the
owner class.
Instances of descriptors may also have the "__objclass__" attribute
present:
object.__objclass__
The attribute "__objclass__" is interpreted by the "inspect" module
as specifying the class where this object was defined (setting this
appropriately can assist in runtime introspection of dynamic class
attributes). For callables, it may indicate that an instance of the
given type (or a subclass) is expected or required as the first
positional argument (for example, CPython sets this attribute for
unbound methods that are implemented in C).
Invoking Descriptors
--------------------
In general, a descriptor is an object attribute with “binding
behavior”, one whose attribute access has been overridden by methods
in the descriptor protocol: "__get__()", "__set__()", and
"__delete__()". If any of those methods are defined for an object, it
is said to be a descriptor.
The default behavior for attribute access is to get, set, or delete
the attribute from an object’s dictionary. For instance, "a.x" has a
lookup chain starting with "a.__dict__['x']", then
"type(a).__dict__['x']", and continuing through the base classes of
"type(a)" excluding metaclasses.
However, if the looked-up value is an object defining one of the
descriptor methods, then Python may override the default behavior and
invoke the descriptor method instead. Where this occurs in the
precedence chain depends on which descriptor methods were defined and
how they were called.
The starting point for descriptor invocation is a binding, "a.x". How
the arguments are assembled depends on "a":
Direct Call
The simplest and least common call is when user code directly
invokes a descriptor method: "x.__get__(a)".
Instance Binding
If binding to an object instance, "a.x" is transformed into the
call: "type(a).__dict__['x'].__get__(a, type(a))".
Class Binding
If binding to a class, "A.x" is transformed into the call:
"A.__dict__['x'].__get__(None, A)".
Super Binding
A dotted lookup such as "super(A, a).x" searches
"a.__class__.__mro__" for a base class "B" following "A" and then
returns "B.__dict__['x'].__get__(a, A)". If not a descriptor, "x"
is returned unchanged.
For instance bindings, the precedence of descriptor invocation depends
on which descriptor methods are defined. A descriptor can define any
combination of "__get__()", "__set__()" and "__delete__()". If it
does not define "__get__()", then accessing the attribute will return
the descriptor object itself unless there is a value in the object’s
instance dictionary. If the descriptor defines "__set__()" and/or
"__delete__()", it is a data descriptor; if it defines neither, it is
a non-data descriptor. Normally, data descriptors define both
"__get__()" and "__set__()", while non-data descriptors have just the
"__get__()" method. Data descriptors with "__get__()" and "__set__()"
(and/or "__delete__()") defined always override a redefinition in an
instance dictionary. In contrast, non-data descriptors can be
overridden by instances.
Python methods (including those decorated with "@staticmethod" and
"@classmethod") are implemented as non-data descriptors. Accordingly,
instances can redefine and override methods. This allows individual
instances to acquire behaviors that differ from other instances of the
same class.
The "property()" function is implemented as a data descriptor.
Accordingly, instances cannot override the behavior of a property.
__slots__
---------
*__slots__* allow us to explicitly declare data members (like
properties) and deny the creation of "__dict__" and *__weakref__*
(unless explicitly declared in *__slots__* or available in a parent.)
The space saved over using "__dict__" can be significant. Attribute
lookup speed can be significantly improved as well.
object.__slots__
This class variable can be assigned a string, iterable, or sequence
of strings with variable names used by instances. *__slots__*
reserves space for the declared variables and prevents the
automatic creation of "__dict__" and *__weakref__* for each
instance.
Notes on using *__slots__*:
* When inheriting from a class without *__slots__*, the "__dict__" and
*__weakref__* attribute of the instances will always be accessible.
* Without a "__dict__" variable, instances cannot be assigned new
variables not listed in the *__slots__* definition. Attempts to
assign to an unlisted variable name raises "AttributeError". If
dynamic assignment of new variables is desired, then add
"'__dict__'" to the sequence of strings in the *__slots__*
declaration.
* Without a *__weakref__* variable for each instance, classes defining
*__slots__* do not support "weak references" to its instances. If
weak reference support is needed, then add "'__weakref__'" to the
sequence of strings in the *__slots__* declaration.
* *__slots__* are implemented at the class level by creating
descriptors for each variable name. As a result, class attributes
cannot be used to set default values for instance variables defined
by *__slots__*; otherwise, the class attribute would overwrite the
descriptor assignment.
* The action of a *__slots__* declaration is not limited to the class
where it is defined. *__slots__* declared in parents are available
in child classes. However, instances of a child subclass will get a
"__dict__" and *__weakref__* unless the subclass also defines
*__slots__* (which should only contain names of any *additional*
slots).
* If a class defines a slot also defined in a base class, the instance
variable defined by the base class slot is inaccessible (except by
retrieving its descriptor directly from the base class). This
renders the meaning of the program undefined. In the future, a
check may be added to prevent this.
* "TypeError" will be raised if nonempty *__slots__* are defined for a
class derived from a ""variable-length" built-in type" such as
"int", "bytes", and "tuple".
* Any non-string *iterable* may be assigned to *__slots__*.
* If a "dictionary" is used to assign *__slots__*, the dictionary keys
will be used as the slot names. The values of the dictionary can be
used to provide per-attribute docstrings that will be recognised by
"inspect.getdoc()" and displayed in the output of "help()".
* "__class__" assignment works only if both classes have the same
*__slots__*.
* Multiple inheritance with multiple slotted parent classes can be
used, but only one parent is allowed to have attributes created by
slots (the other bases must have empty slot layouts) - violations
raise "TypeError".
* If an *iterator* is used for *__slots__* then a *descriptor* is
created for each of the iterator’s values. However, the *__slots__*
attribute will be an empty iterator.
Customizing class creation
==========================
Whenever a class inherits from another class, "__init_subclass__()" is
called on the parent class. This way, it is possible to write classes
which change the behavior of subclasses. This is closely related to
class decorators, but where class decorators only affect the specific
class they’re applied to, "__init_subclass__" solely applies to future
subclasses of the class defining the method.
classmethod object.__init_subclass__(cls)
This method is called whenever the containing class is subclassed.
*cls* is then the new subclass. If defined as a normal instance
method, this method is implicitly converted to a class method.
Keyword arguments which are given to a new class are passed to the
parent class’s "__init_subclass__". For compatibility with other
classes using "__init_subclass__", one should take out the needed
keyword arguments and pass the others over to the base class, as
in:
class Philosopher:
def __init_subclass__(cls, /, default_name, **kwargs):
super().__init_subclass__(**kwargs)
cls.default_name = default_name
class AustralianPhilosopher(Philosopher, default_name="Bruce"):
pass
The default implementation "object.__init_subclass__" does nothing,
but raises an error if it is called with any arguments.
Note:
The metaclass hint "metaclass" is consumed by the rest of the
type machinery, and is never passed to "__init_subclass__"
implementations. The actual metaclass (rather than the explicit
hint) can be accessed as "type(cls)".
Added in version 3.6.
When a class is created, "type.__new__()" scans the class variables
and makes callbacks to those with a "__set_name__()" hook.
object.__set_name__(self, owner, name)
Automatically called at the time the owning class *owner* is
created. The object has been assigned to *name* in that class:
class A:
x = C() # Automatically calls: x.__set_name__(A, 'x')
If the class variable is assigned after the class is created,
"__set_name__()" will not be called automatically. If needed,
"__set_name__()" can be called directly:
class A:
pass
c = C()
A.x = c # The hook is not called
c.__set_name__(A, 'x') # Manually invoke the hook
See Creating the class object for more details.
Added in version 3.6.
Metaclasses
-----------
By default, classes are constructed using "type()". The class body is
executed in a new namespace and the class name is bound locally to the
result of "type(name, bases, namespace)".
The class creation process can be customized by passing the
"metaclass" keyword argument in the class definition line, or by
inheriting from an existing class that included such an argument. In
the following example, both "MyClass" and "MySubclass" are instances
of "Meta":
class Meta(type):
pass
class MyClass(metaclass=Meta):
pass
class MySubclass(MyClass):
pass
Any other keyword arguments that are specified in the class definition
are passed through to all metaclass operations described below.
When a class definition is executed, the following steps occur:
* MRO entries are resolved;
* the appropriate metaclass is determined;
* the class namespace is prepared;
* the class body is executed;
* the class object is created.
Resolving MRO entries
---------------------
object.__mro_entries__(self, bases)
If a base that appears in a class definition is not an instance of
"type", then an "__mro_entries__()" method is searched on the base.
If an "__mro_entries__()" method is found, the base is substituted
with the result of a call to "__mro_entries__()" when creating the
class. The method is called with the original bases tuple passed to
the *bases* parameter, and must return a tuple of classes that will
be used instead of the base. The returned tuple may be empty: in
these cases, the original base is ignored.
See also:
"types.resolve_bases()"
Dynamically resolve bases that are not instances of "type".
"types.get_original_bases()"
Retrieve a class’s “original bases” prior to modifications by
"__mro_entries__()".
**PEP 560**
Core support for typing module and generic types.
Determining the appropriate metaclass
-------------------------------------
The appropriate metaclass for a class definition is determined as
follows:
* if no bases and no explicit metaclass are given, then "type()" is
used;
* if an explicit metaclass is given and it is *not* an instance of
"type()", then it is used directly as the metaclass;
* if an instance of "type()" is given as the explicit metaclass, or
bases are defined, then the most derived metaclass is used.
The most derived metaclass is selected from the explicitly specified
metaclass (if any) and the metaclasses (i.e. "type(cls)") of all
specified base classes. The most derived metaclass is one which is a
subtype of *all* of these candidate metaclasses. If none of the
candidate metaclasses meets that criterion, then the class definition
will fail with "TypeError".
Preparing the class namespace
-----------------------------
Once the appropriate metaclass has been identified, then the class
namespace is prepared. If the metaclass has a "__prepare__" attribute,
it is called as "namespace = metaclass.__prepare__(name, bases,
**kwds)" (where the additional keyword arguments, if any, come from
the class definition). The "__prepare__" method should be implemented
as a "classmethod". The namespace returned by "__prepare__" is passed
in to "__new__", but when the final class object is created the
namespace is copied into a new "dict".
If the metaclass has no "__prepare__" attribute, then the class
namespace is initialised as an empty ordered mapping.
See also:
**PEP 3115** - Metaclasses in Python 3000
Introduced the "__prepare__" namespace hook
Executing the class body
------------------------
The class body is executed (approximately) as "exec(body, globals(),
namespace)". The key difference from a normal call to "exec()" is that
lexical scoping allows the class body (including any methods) to
reference names from the current and outer scopes when the class
definition occurs inside a function.
However, even when the class definition occurs inside the function,
methods defined inside the class still cannot see names defined at the
class scope. Class variables must be accessed through the first
parameter of instance or class methods, or through the implicit
lexically scoped "__class__" reference described in the next section.
Creating the class object
-------------------------
Once the class namespace has been populated by executing the class
body, the class object is created by calling "metaclass(name, bases,
namespace, **kwds)" (the additional keywords passed here are the same
as those passed to "__prepare__").
This class object is the one that will be referenced by the zero-
argument form of "super()". "__class__" is an implicit closure
reference created by the compiler if any methods in a class body refer
to either "__class__" or "super". This allows the zero argument form
of "super()" to correctly identify the class being defined based on
lexical scoping, while the class or instance that was used to make the
current call is identified based on the first argument passed to the
method.
**CPython implementation detail:** In CPython 3.6 and later, the
"__class__" cell is passed to the metaclass as a "__classcell__" entry
in the class namespace. If present, this must be propagated up to the
"type.__new__" call in order for the class to be initialised
correctly. Failing to do so will result in a "RuntimeError" in Python
3.8.
When using the default metaclass "type", or any metaclass that
ultimately calls "type.__new__", the following additional
customization steps are invoked after creating the class object:
1. The "type.__new__" method collects all of the attributes in the
class namespace that define a "__set_name__()" method;
2. Those "__set_name__" methods are called with the class being
defined and the assigned name of that particular attribute;
3. The "__init_subclass__()" hook is called on the immediate parent of
the new class in its method resolution order.
After the class object is created, it is passed to the class
decorators included in the class definition (if any) and the resulting
object is bound in the local namespace as the defined class.
When a new class is created by "type.__new__", the object provided as
the namespace parameter is copied to a new ordered mapping and the
original object is discarded. The new copy is wrapped in a read-only
proxy, which becomes the "__dict__" attribute of the class object.
See also:
**PEP 3135** - New super
Describes the implicit "__class__" closure reference
Uses for metaclasses
--------------------
The potential uses for metaclasses are boundless. Some ideas that have
been explored include enum, logging, interface checking, automatic
delegation, automatic property creation, proxies, frameworks, and
automatic resource locking/synchronization.
Customizing instance and subclass checks
========================================
The following methods are used to override the default behavior of the
"isinstance()" and "issubclass()" built-in functions.
In particular, the metaclass "abc.ABCMeta" implements these methods in
order to allow the addition of Abstract Base Classes (ABCs) as
“virtual base classes” to any class or type (including built-in
types), including other ABCs.
type.__instancecheck__(self, instance)
Return true if *instance* should be considered a (direct or
indirect) instance of *class*. If defined, called to implement
"isinstance(instance, class)".
type.__subclasscheck__(self, subclass)
Return true if *subclass* should be considered a (direct or
indirect) subclass of *class*. If defined, called to implement
"issubclass(subclass, class)".
Note that these methods are looked up on the type (metaclass) of a
class. They cannot be defined as class methods in the actual class.
This is consistent with the lookup of special methods that are called
on instances, only in this case the instance is itself a class.
See also:
**PEP 3119** - Introducing Abstract Base Classes
Includes the specification for customizing "isinstance()" and
"issubclass()" behavior through "__instancecheck__()" and
"__subclasscheck__()", with motivation for this functionality in
the context of adding Abstract Base Classes (see the "abc"
module) to the language.
Emulating generic types
=======================
When using *type annotations*, it is often useful to *parameterize* a
*generic type* using Python’s square-brackets notation. For example,
the annotation "list[int]" might be used to signify a "list" in which
all the elements are of type "int".
See also:
**PEP 484** - Type Hints
Introducing Python’s framework for type annotations
Generic Alias Types
Documentation for objects representing parameterized generic
classes
Generics, user-defined generics and "typing.Generic"
Documentation on how to implement generic classes that can be
parameterized at runtime and understood by static type-checkers.
A class can *generally* only be parameterized if it defines the
special class method "__class_getitem__()".
classmethod object.__class_getitem__(cls, key)
Return an object representing the specialization of a generic class
by type arguments found in *key*.
When defined on a class, "__class_getitem__()" is automatically a
class method. As such, there is no need for it to be decorated with
"@classmethod" when it is defined.
The purpose of *__class_getitem__*
----------------------------------
The purpose of "__class_getitem__()" is to allow runtime
parameterization of standard-library generic classes in order to more
easily apply *type hints* to these classes.
To implement custom generic classes that can be parameterized at
runtime and understood by static type-checkers, users should either
inherit from a standard library class that already implements
"__class_getitem__()", or inherit from "typing.Generic", which has its
own implementation of "__class_getitem__()".
Custom implementations of "__class_getitem__()" on classes defined
outside of the standard library may not be understood by third-party
type-checkers such as mypy. Using "__class_getitem__()" on any class
for purposes other than type hinting is discouraged.
*__class_getitem__* versus *__getitem__*
----------------------------------------
Usually, the subscription of an object using square brackets will call
the "__getitem__()" instance method defined on the object’s class.
However, if the object being subscribed is itself a class, the class
method "__class_getitem__()" may be called instead.
"__class_getitem__()" should return a GenericAlias object if it is
properly defined.
Presented with the *expression* "obj[x]", the Python interpreter
follows something like the following process to decide whether
"__getitem__()" or "__class_getitem__()" should be called:
from inspect import isclass
def subscribe(obj, x):
"""Return the result of the expression 'obj[x]'"""
class_of_obj = type(obj)
# If the class of obj defines __getitem__,
# call class_of_obj.__getitem__(obj, x)
if hasattr(class_of_obj, '__getitem__'):
return class_of_obj.__getitem__(obj, x)
# Else, if obj is a class and defines __class_getitem__,
# call obj.__class_getitem__(x)
elif isclass(obj) and hasattr(obj, '__class_getitem__'):
return obj.__class_getitem__(x)
# Else, raise an exception
else:
raise TypeError(
f"'{class_of_obj.__name__}' object is not subscriptable"
)
In Python, all classes are themselves instances of other classes. The
class of a class is known as that class’s *metaclass*, and most
classes have the "type" class as their metaclass. "type" does not
define "__getitem__()", meaning that expressions such as "list[int]",
"dict[str, float]" and "tuple[str, bytes]" all result in
"__class_getitem__()" being called:
>>> # list has class "type" as its metaclass, like most classes:
>>> type(list)
<class 'type'>
>>> type(dict) == type(list) == type(tuple) == type(str) == type(bytes)
True
>>> # "list[int]" calls "list.__class_getitem__(int)"
>>> list[int]
list[int]
>>> # list.__class_getitem__ returns a GenericAlias object:
>>> type(list[int])
<class 'types.GenericAlias'>
However, if a class has a custom metaclass that defines
"__getitem__()", subscribing the class may result in different
behaviour. An example of this can be found in the "enum" module:
>>> from enum import Enum
>>> class Menu(Enum):
... """A breakfast menu"""
... SPAM = 'spam'
... BACON = 'bacon'
...
>>> # Enum classes have a custom metaclass:
>>> type(Menu)
<class 'enum.EnumMeta'>
>>> # EnumMeta defines __getitem__,
>>> # so __class_getitem__ is not called,
>>> # and the result is not a GenericAlias object:
>>> Menu['SPAM']
<Menu.SPAM: 'spam'>
>>> type(Menu['SPAM'])
<enum 'Menu'>
See also:
**PEP 560** - Core Support for typing module and generic types
Introducing "__class_getitem__()", and outlining when a
subscription results in "__class_getitem__()" being called
instead of "__getitem__()"
Emulating callable objects
==========================
object.__call__(self[, args...])
Called when the instance is “called” as a function; if this method
is defined, "x(arg1, arg2, ...)" roughly translates to
"type(x).__call__(x, arg1, ...)". The "object" class itself does
not provide this method.
Emulating container types
=========================
The following methods can be defined to implement container objects.
None of them are provided by the "object" class itself. Containers
usually are *sequences* (such as "lists" or "tuples") or *mappings*
(like *dictionaries*), but can represent other containers as well.
The first set of methods is used either to emulate a sequence or to
emulate a mapping; the difference is that for a sequence, the
allowable keys should be the integers *k* for which "0 <= k < N" where
*N* is the length of the sequence, or "slice" objects, which define a
range of items. It is also recommended that mappings provide the
methods "keys()", "values()", "items()", "get()", "clear()",
"setdefault()", "pop()", "popitem()", "copy()", and "update()"
behaving similar to those for Python’s standard "dictionary" objects.
The "collections.abc" module provides a "MutableMapping" *abstract
base class* to help create those methods from a base set of
"__getitem__()", "__setitem__()", "__delitem__()", and "keys()".
Mutable sequences should provide methods "append()", "count()",
"index()", "extend()", "insert()", "pop()", "remove()", "reverse()"
and "sort()", like Python standard "list" objects. Finally, sequence
types should implement addition (meaning concatenation) and
multiplication (meaning repetition) by defining the methods
"__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and
"__imul__()" described below; they should not define other numerical
operators. It is recommended that both mappings and sequences
implement the "__contains__()" method to allow efficient use of the
"in" operator; for mappings, "in" should search the mapping’s keys;
for sequences, it should search through the values. It is further
recommended that both mappings and sequences implement the
"__iter__()" method to allow efficient iteration through the
container; for mappings, "__iter__()" should iterate through the
object’s keys; for sequences, it should iterate through the values.
object.__len__(self)
Called to implement the built-in function "len()". Should return
the length of the object, an integer ">=" 0. Also, an object that
doesn’t define a "__bool__()" method and whose "__len__()" method
returns zero is considered to be false in a Boolean context.
**CPython implementation detail:** In CPython, the length is
required to be at most "sys.maxsize". If the length is larger than
"sys.maxsize" some features (such as "len()") may raise
"OverflowError". To prevent raising "OverflowError" by truth value
testing, an object must define a "__bool__()" method.
object.__length_hint__(self)
Called to implement "operator.length_hint()". Should return an
estimated length for the object (which may be greater or less than
the actual length). The length must be an integer ">=" 0. The
return value may also be "NotImplemented", which is treated the
same as if the "__length_hint__" method didn’t exist at all. This
method is purely an optimization and is never required for
correctness.
Added in version 3.4.
Note:
Slicing is done exclusively with the following three methods. A
call like
a[1:2] = b
is translated to
a[slice(1, 2, None)] = b
and so forth. Missing slice items are always filled in with "None".
object.__getitem__(self, key)
Called to implement evaluation of "self[key]". For *sequence*
types, the accepted keys should be integers. Optionally, they may
support "slice" objects as well. Negative index support is also
optional. If *key* is of an inappropriate type, "TypeError" may be
raised; if *key* is a value outside the set of indexes for the
sequence (after any special interpretation of negative values),
"IndexError" should be raised. For *mapping* types, if *key* is
missing (not in the container), "KeyError" should be raised.
Note:
"for" loops expect that an "IndexError" will be raised for
illegal indexes to allow proper detection of the end of the
sequence.
Note:
When subscripting a *class*, the special class method
"__class_getitem__()" may be called instead of "__getitem__()".
See __class_getitem__ versus __getitem__ for more details.
object.__setitem__(self, key, value)
Called to implement assignment to "self[key]". Same note as for
"__getitem__()". This should only be implemented for mappings if
the objects support changes to the values for keys, or if new keys
can be added, or for sequences if elements can be replaced. The
same exceptions should be raised for improper *key* values as for
the "__getitem__()" method.
object.__delitem__(self, key)
Called to implement deletion of "self[key]". Same note as for
"__getitem__()". This should only be implemented for mappings if
the objects support removal of keys, or for sequences if elements
can be removed from the sequence. The same exceptions should be
raised for improper *key* values as for the "__getitem__()" method.
object.__missing__(self, key)
Called by "dict"."__getitem__()" to implement "self[key]" for dict
subclasses when key is not in the dictionary.
object.__iter__(self)
This method is called when an *iterator* is required for a
container. This method should return a new iterator object that can
iterate over all the objects in the container. For mappings, it
should iterate over the keys of the container.
object.__reversed__(self)
Called (if present) by the "reversed()" built-in to implement
reverse iteration. It should return a new iterator object that
iterates over all the objects in the container in reverse order.
If the "__reversed__()" method is not provided, the "reversed()"
built-in will fall back to using the sequence protocol ("__len__()"
and "__getitem__()"). Objects that support the sequence protocol
should only provide "__reversed__()" if they can provide an
implementation that is more efficient than the one provided by
"reversed()".
The membership test operators ("in" and "not in") are normally
implemented as an iteration through a container. However, container
objects can supply the following special method with a more efficient
implementation, which also does not require the object be iterable.
object.__contains__(self, item)
Called to implement membership test operators. Should return true
if *item* is in *self*, false otherwise. For mapping objects, this
should consider the keys of the mapping rather than the values or
the key-item pairs.
For objects that don’t define "__contains__()", the membership test
first tries iteration via "__iter__()", then the old sequence
iteration protocol via "__getitem__()", see this section in the
language reference.
Emulating numeric types
=======================
The following methods can be defined to emulate numeric objects.
Methods corresponding to operations that are not supported by the
particular kind of number implemented (e.g., bitwise operations for
non-integral numbers) should be left undefined.
object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
object.__matmul__(self, other)
object.__truediv__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
object.__divmod__(self, other)
object.__pow__(self, other[, modulo])
object.__lshift__(self, other)
object.__rshift__(self, other)
object.__and__(self, other)
object.__xor__(self, other)
object.__or__(self, other)
These methods are called to implement the binary arithmetic
operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",
"pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to
evaluate the expression "x + y", where *x* is an instance of a
class that has an "__add__()" method, "type(x).__add__(x, y)" is
called. The "__divmod__()" method should be the equivalent to
using "__floordiv__()" and "__mod__()"; it should not be related to
"__truediv__()". Note that "__pow__()" should be defined to accept
an optional third argument if the three-argument version of the
built-in "pow()" function is to be supported.
If one of those methods does not support the operation with the
supplied arguments, it should return "NotImplemented".
object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rmatmul__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other[, modulo])
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)
These methods are called to implement the binary arithmetic
operations ("+", "-", "*", "@", "/", "//", "%", "divmod()",
"pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped)
operands. These functions are only called if the operands are of
different types, when the left operand does not support the
corresponding operation [3], or the right operand’s class is
derived from the left operand’s class. [4] For instance, to
evaluate the expression "x - y", where *y* is an instance of a
class that has an "__rsub__()" method, "type(y).__rsub__(y, x)" is
called if "type(x).__sub__(x, y)" returns "NotImplemented" or
"type(y)" is a subclass of "type(x)". [5]
Note that "__rpow__()" should be defined to accept an optional
third argument if the three-argument version of the built-in
"pow()" function is to be supported.
Changed in version 3.14.0a7 (unreleased): Three-argument "pow()"
now try calling "__rpow__()" if necessary. Previously it was only
called in two-argument "pow()" and the binary power operator.
Note:
If the right operand’s type is a subclass of the left operand’s
type and that subclass provides a different implementation of the
reflected method for the operation, this method will be called
before the left operand’s non-reflected method. This behavior
allows subclasses to override their ancestors’ operations.
object.__iadd__(self, other)
object.__isub__(self, other)
object.__imul__(self, other)
object.__imatmul__(self, other)
object.__itruediv__(self, other)
object.__ifloordiv__(self, other)
object.__imod__(self, other)
object.__ipow__(self, other[, modulo])
object.__ilshift__(self, other)
object.__irshift__(self, other)
object.__iand__(self, other)
object.__ixor__(self, other)
object.__ior__(self, other)
These methods are called to implement the augmented arithmetic
assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=",
"<<=", ">>=", "&=", "^=", "|="). These methods should attempt to
do the operation in-place (modifying *self*) and return the result
(which could be, but does not have to be, *self*). If a specific
method is not defined, or if that method returns "NotImplemented",
the augmented assignment falls back to the normal methods. For
instance, if *x* is an instance of a class with an "__iadd__()"
method, "x += y" is equivalent to "x = x.__iadd__(y)" . If
"__iadd__()" does not exist, or if "x.__iadd__(y)" returns
"NotImplemented", "x.__add__(y)" and "y.__radd__(x)" are
considered, as with the evaluation of "x + y". In certain
situations, augmented assignment can result in unexpected errors
(see Why does a_tuple[i] += [‘item’] raise an exception when the
addition works?), but this behavior is in fact part of the data
model.
object.__neg__(self)
object.__pos__(self)
object.__abs__(self)
object.__invert__(self)
Called to implement the unary arithmetic operations ("-", "+",
"abs()" and "~").
object.__complex__(self)
object.__int__(self)
object.__float__(self)
Called to implement the built-in functions "complex()", "int()" and
"float()". Should return a value of the appropriate type.
object.__index__(self)
Called to implement "operator.index()", and whenever Python needs
to losslessly convert the numeric object to an integer object (such
as in slicing, or in the built-in "bin()", "hex()" and "oct()"
functions). Presence of this method indicates that the numeric
object is an integer type. Must return an integer.
If "__int__()", "__float__()" and "__complex__()" are not defined
then corresponding built-in functions "int()", "float()" and
"complex()" fall back to "__index__()".
object.__round__(self[, ndigits])
object.__trunc__(self)
object.__floor__(self)
object.__ceil__(self)
Called to implement the built-in function "round()" and "math"
functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is
passed to "__round__()" all these methods should return the value
of the object truncated to an "Integral" (typically an "int").
Changed in version 3.14: "int()" no longer delegates to the
"__trunc__()" method.
With Statement Context Managers
===============================
A *context manager* is an object that defines the runtime context to
be established when executing a "with" statement. The context manager
handles the entry into, and the exit from, the desired runtime context
for the execution of the block of code. Context managers are normally
invoked using the "with" statement (described in section The with
statement), but can also be used by directly invoking their methods.
Typical uses of context managers include saving and restoring various
kinds of global state, locking and unlocking resources, closing opened
files, etc.
For more information on context managers, see Context Manager Types.
The "object" class itself does not provide the context manager
methods.
object.__enter__(self)
Enter the runtime context related to this object. The "with"
statement will bind this method’s return value to the target(s)
specified in the "as" clause of the statement, if any.
object.__exit__(self, exc_type, exc_value, traceback)
Exit the runtime context related to this object. The parameters
describe the exception that caused the context to be exited. If the
context was exited without an exception, all three arguments will
be "None".
If an exception is supplied, and the method wishes to suppress the
exception (i.e., prevent it from being propagated), it should
return a true value. Otherwise, the exception will be processed
normally upon exit from this method.
Note that "__exit__()" methods should not reraise the passed-in
exception; this is the caller’s responsibility.
See also:
**PEP 343** - The “with” statement
The specification, background, and examples for the Python "with"
statement.
Customizing positional arguments in class pattern matching
==========================================================
When using a class name in a pattern, positional arguments in the
pattern are not allowed by default, i.e. "case MyClass(x, y)" is
typically invalid without special support in "MyClass". To be able to
use that kind of pattern, the class needs to define a *__match_args__*
attribute.
object.__match_args__
This class variable can be assigned a tuple of strings. When this
class is used in a class pattern with positional arguments, each
positional argument will be converted into a keyword argument,
using the corresponding value in *__match_args__* as the keyword.
The absence of this attribute is equivalent to setting it to "()".
For example, if "MyClass.__match_args__" is "("left", "center",
"right")" that means that "case MyClass(x, y)" is equivalent to "case
MyClass(left=x, center=y)". Note that the number of arguments in the
pattern must be smaller than or equal to the number of elements in
*__match_args__*; if it is larger, the pattern match attempt will
raise a "TypeError".
Added in version 3.10.
See also:
**PEP 634** - Structural Pattern Matching
The specification for the Python "match" statement.
Emulating buffer types
======================
The buffer protocol provides a way for Python objects to expose
efficient access to a low-level memory array. This protocol is
implemented by builtin types such as "bytes" and "memoryview", and
third-party libraries may define additional buffer types.
While buffer types are usually implemented in C, it is also possible
to implement the protocol in Python.
object.__buffer__(self, flags)
Called when a buffer is requested from *self* (for example, by the
"memoryview" constructor). The *flags* argument is an integer
representing the kind of buffer requested, affecting for example
whether the returned buffer is read-only or writable.
"inspect.BufferFlags" provides a convenient way to interpret the
flags. The method must return a "memoryview" object.
object.__release_buffer__(self, buffer)
Called when a buffer is no longer needed. The *buffer* argument is
a "memoryview" object that was previously returned by
"__buffer__()". The method must release any resources associated
with the buffer. This method should return "None". Buffer objects
that do not need to perform any cleanup are not required to
implement this method.
Added in version 3.12.
See also:
**PEP 688** - Making the buffer protocol accessible in Python
Introduces the Python "__buffer__" and "__release_buffer__"
methods.
"collections.abc.Buffer"
ABC for buffer types.
Annotations
===========
Functions, classes, and modules may contain *annotations*, which are a
way to associate information (usually *type hints*) with a symbol.
object.__annotations__
This attribute contains the annotations for an object. It is lazily
evaluated, so accessing the attribute may execute arbitrary code
and raise exceptions. If evaluation is successful, the attribute is
set to a dictionary mapping from variable names to annotations.
Changed in version 3.14: Annotations are now lazily evaluated.
object.__annotate__(format)
An *annotate function*. Returns a new dictionary object mapping
attribute/parameter names to their annotation values.
Takes a format parameter specifying the format in which annotations
values should be provided. It must be a member of the
"annotationlib.Format" enum, or an integer with a value
corresponding to a member of the enum.
If an annotate function doesn’t support the requested format, it
must raise "NotImplementedError". Annotate functions must always
support "VALUE" format; they must not raise "NotImplementedError()"
when called with this format.
When called with "VALUE" format, an annotate function may raise
"NameError"; it must not raise "NameError" when called requesting
any other format.
If an object does not have any annotations, "__annotate__" should
preferably be set to "None" (it can’t be deleted), rather than set
to a function that returns an empty dict.
Added in version 3.14.
See also:
**PEP 649** — Deferred evaluation of annotation using descriptors
Introduces lazy evaluation of annotations and the "__annotate__"
function.
Special method lookup
=====================
For custom classes, implicit invocations of special methods are only
guaranteed to work correctly if defined on an object’s type, not in
the object’s instance dictionary. That behaviour is the reason why
the following code raises an exception:
>>> class C:
... pass
...
>>> c = C()
>>> c.__len__ = lambda: 5
>>> len(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'C' has no len()
The rationale behind this behaviour lies with a number of special
methods such as "__hash__()" and "__repr__()" that are implemented by
all objects, including type objects. If the implicit lookup of these
methods used the conventional lookup process, they would fail when
invoked on the type object itself:
>>> 1 .__hash__() == hash(1)
True
>>> int.__hash__() == hash(int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor '__hash__' of 'int' object needs an argument
Incorrectly attempting to invoke an unbound method of a class in this
way is sometimes referred to as ‘metaclass confusion’, and is avoided
by bypassing the instance when looking up special methods:
>>> type(1).__hash__(1) == hash(1)
True
>>> type(int).__hash__(int) == hash(int)
True
In addition to bypassing any instance attributes in the interest of
correctness, implicit special method lookup generally also bypasses
the "__getattribute__()" method even of the object’s metaclass:
>>> class Meta(type):
... def __getattribute__(*args):
... print("Metaclass getattribute invoked")
... return type.__getattribute__(*args)
...
>>> class C(object, metaclass=Meta):
... def __len__(self):
... return 10
... def __getattribute__(*args):
... print("Class getattribute invoked")
... return object.__getattribute__(*args)
...
>>> c = C()
>>> c.__len__() # Explicit lookup via instance
Class getattribute invoked
10
>>> type(c).__len__(c) # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c) # Implicit lookup
10
Bypassing the "__getattribute__()" machinery in this fashion provides
significant scope for speed optimisations within the interpreter, at
the cost of some flexibility in the handling of special methods (the
special method *must* be set on the class object itself in order to be
consistently invoked by the interpreter).
''',
'string-methods': r'''String Methods
**************
Strings implement all of the common sequence operations, along with
the additional methods described below.
Strings also support two styles of string formatting, one providing a
large degree of flexibility and customization (see "str.format()",
Format String Syntax and Custom String Formatting) and the other based
on C "printf" style formatting that handles a narrower range of types
and is slightly harder to use correctly, but is often faster for the
cases it can handle (printf-style String Formatting).
The Text Processing Services section of the standard library covers a
number of other modules that provide various text related utilities
(including regular expression support in the "re" module).
str.capitalize()
Return a copy of the string with its first character capitalized
and the rest lowercased.
Changed in version 3.8: The first character is now put into
titlecase rather than uppercase. This means that characters like
digraphs will only have their first letter capitalized, instead of
the full character.
str.casefold()
Return a casefolded copy of the string. Casefolded strings may be
used for caseless matching.
Casefolding is similar to lowercasing but more aggressive because
it is intended to remove all case distinctions in a string. For
example, the German lowercase letter "'ß'" is equivalent to ""ss"".
Since it is already lowercase, "lower()" would do nothing to "'ß'";
"casefold()" converts it to ""ss"".
The casefolding algorithm is described in section 3.13 ‘Default
Case Folding’ of the Unicode Standard.
Added in version 3.3.
str.center(width[, fillchar])
Return centered in a string of length *width*. Padding is done
using the specified *fillchar* (default is an ASCII space). The
original string is returned if *width* is less than or equal to
"len(s)".
str.count(sub[, start[, end]])
Return the number of non-overlapping occurrences of substring *sub*
in the range [*start*, *end*]. Optional arguments *start* and
*end* are interpreted as in slice notation.
If *sub* is empty, returns the number of empty strings between
characters which is the length of the string plus one.
str.encode(encoding='utf-8', errors='strict')
Return the string encoded to "bytes".
*encoding* defaults to "'utf-8'"; see Standard Encodings for
possible values.
*errors* controls how encoding errors are handled. If "'strict'"
(the default), a "UnicodeError" exception is raised. Other possible
values are "'ignore'", "'replace'", "'xmlcharrefreplace'",
"'backslashreplace'" and any other name registered via
"codecs.register_error()". See Error Handlers for details.
For performance reasons, the value of *errors* is not checked for
validity unless an encoding error actually occurs, Python
Development Mode is enabled or a debug build is used.
Changed in version 3.1: Added support for keyword arguments.
Changed in version 3.9: The value of the *errors* argument is now
checked in Python Development Mode and in debug mode.
str.endswith(suffix[, start[, end]])
Return "True" if the string ends with the specified *suffix*,
otherwise return "False". *suffix* can also be a tuple of suffixes
to look for. With optional *start*, test beginning at that
position. With optional *end*, stop comparing at that position.
str.expandtabs(tabsize=8)
Return a copy of the string where all tab characters are replaced
by one or more spaces, depending on the current column and the
given tab size. Tab positions occur every *tabsize* characters
(default is 8, giving tab positions at columns 0, 8, 16 and so on).
To expand the string, the current column is set to zero and the
string is examined character by character. If the character is a
tab ("\t"), one or more space characters are inserted in the result
until the current column is equal to the next tab position. (The
tab character itself is not copied.) If the character is a newline
("\n") or return ("\r"), it is copied and the current column is
reset to zero. Any other character is copied unchanged and the
current column is incremented by one regardless of how the
character is represented when printed.
>>> '01\t012\t0123\t01234'.expandtabs()
'01 012 0123 01234'
>>> '01\t012\t0123\t01234'.expandtabs(4)
'01 012 0123 01234'
str.find(sub[, start[, end]])
Return the lowest index in the string where substring *sub* is
found within the slice "s[start:end]". Optional arguments *start*
and *end* are interpreted as in slice notation. Return "-1" if
*sub* is not found.
Note:
The "find()" method should be used only if you need to know the
position of *sub*. To check if *sub* is a substring or not, use
the "in" operator:
>>> 'Py' in 'Python'
True
str.format(*args, **kwargs)
Perform a string formatting operation. The string on which this
method is called can contain literal text or replacement fields
delimited by braces "{}". Each replacement field contains either
the numeric index of a positional argument, or the name of a
keyword argument. Returns a copy of the string where each
replacement field is replaced with the string value of the
corresponding argument.
>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'
See Format String Syntax for a description of the various
formatting options that can be specified in format strings.
Note:
When formatting a number ("int", "float", "complex",
"decimal.Decimal" and subclasses) with the "n" type (ex:
"'{:n}'.format(1234)"), the function temporarily sets the
"LC_CTYPE" locale to the "LC_NUMERIC" locale to decode
"decimal_point" and "thousands_sep" fields of "localeconv()" if
they are non-ASCII or longer than 1 byte, and the "LC_NUMERIC"
locale is different than the "LC_CTYPE" locale. This temporary
change affects other threads.
Changed in version 3.7: When formatting a number with the "n" type,
the function sets temporarily the "LC_CTYPE" locale to the
"LC_NUMERIC" locale in some cases.
str.format_map(mapping, /)
Similar to "str.format(**mapping)", except that "mapping" is used
directly and not copied to a "dict". This is useful if for example
"mapping" is a dict subclass:
>>> class Default(dict):
... def __missing__(self, key):
... return key
...
>>> '{name} was born in {country}'.format_map(Default(name='Guido'))
'Guido was born in country'
Added in version 3.2.
str.index(sub[, start[, end]])
Like "find()", but raise "ValueError" when the substring is not
found.
str.isalnum()
Return "True" if all characters in the string are alphanumeric and
there is at least one character, "False" otherwise. A character
"c" is alphanumeric if one of the following returns "True":
"c.isalpha()", "c.isdecimal()", "c.isdigit()", or "c.isnumeric()".
str.isalpha()
Return "True" if all characters in the string are alphabetic and
there is at least one character, "False" otherwise. Alphabetic
characters are those characters defined in the Unicode character
database as “Letter”, i.e., those with general category property
being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is
different from the Alphabetic property defined in the section 4.10
‘Letters, Alphabetic, and Ideographic’ of the Unicode Standard.
str.isascii()
Return "True" if the string is empty or all characters in the
string are ASCII, "False" otherwise. ASCII characters have code
points in the range U+0000-U+007F.
Added in version 3.7.
str.isdecimal()
Return "True" if all characters in the string are decimal
characters and there is at least one character, "False" otherwise.
Decimal characters are those that can be used to form numbers in
base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal
character is a character in the Unicode General Category “Nd”.
str.isdigit()
Return "True" if all characters in the string are digits and there
is at least one character, "False" otherwise. Digits include
decimal characters and digits that need special handling, such as
the compatibility superscript digits. This covers digits which
cannot be used to form numbers in base 10, like the Kharosthi
numbers. Formally, a digit is a character that has the property
value Numeric_Type=Digit or Numeric_Type=Decimal.
str.isidentifier()
Return "True" if the string is a valid identifier according to the
language definition, section Identifiers and keywords.
"keyword.iskeyword()" can be used to test whether string "s" is a
reserved identifier, such as "def" and "class".
Example:
>>> from keyword import iskeyword
>>> 'hello'.isidentifier(), iskeyword('hello')
(True, False)
>>> 'def'.isidentifier(), iskeyword('def')
(True, True)
str.islower()
Return "True" if all cased characters [4] in the string are
lowercase and there is at least one cased character, "False"
otherwise.
str.isnumeric()
Return "True" if all characters in the string are numeric
characters, and there is at least one character, "False" otherwise.
Numeric characters include digit characters, and all characters
that have the Unicode numeric value property, e.g. U+2155, VULGAR
FRACTION ONE FIFTH. Formally, numeric characters are those with
the property value Numeric_Type=Digit, Numeric_Type=Decimal or
Numeric_Type=Numeric.
str.isprintable()
Return true if all characters in the string are printable, false if
it contains at least one non-printable character.
Here “printable” means the character is suitable for "repr()" to
use in its output; “non-printable” means that "repr()" on built-in
types will hex-escape the character. It has no bearing on the
handling of strings written to "sys.stdout" or "sys.stderr".
The printable characters are those which in the Unicode character
database (see "unicodedata") have a general category in group
Letter, Mark, Number, Punctuation, or Symbol (L, M, N, P, or S);
plus the ASCII space 0x20. Nonprintable characters are those in
group Separator or Other (Z or C), except the ASCII space.
str.isspace()
Return "True" if there are only whitespace characters in the string
and there is at least one character, "False" otherwise.
A character is *whitespace* if in the Unicode character database
(see "unicodedata"), either its general category is "Zs"
(“Separator, space”), or its bidirectional class is one of "WS",
"B", or "S".
str.istitle()
Return "True" if the string is a titlecased string and there is at
least one character, for example uppercase characters may only
follow uncased characters and lowercase characters only cased ones.
Return "False" otherwise.
str.isupper()
Return "True" if all cased characters [4] in the string are
uppercase and there is at least one cased character, "False"
otherwise.
>>> 'BANANA'.isupper()
True
>>> 'banana'.isupper()
False
>>> 'baNana'.isupper()
False
>>> ' '.isupper()
False
str.join(iterable)
Return a string which is the concatenation of the strings in
*iterable*. A "TypeError" will be raised if there are any non-
string values in *iterable*, including "bytes" objects. The
separator between elements is the string providing this method.
str.ljust(width[, fillchar])
Return the string left justified in a string of length *width*.
Padding is done using the specified *fillchar* (default is an ASCII
space). The original string is returned if *width* is less than or
equal to "len(s)".
str.lower()
Return a copy of the string with all the cased characters [4]
converted to lowercase.
The lowercasing algorithm used is described in section 3.13
‘Default Case Folding’ of the Unicode Standard.
str.lstrip([chars])
Return a copy of the string with leading characters removed. The
*chars* argument is a string specifying the set of characters to be
removed. If omitted or "None", the *chars* argument defaults to
removing whitespace. The *chars* argument is not a prefix; rather,
all combinations of its values are stripped:
>>> ' spacious '.lstrip()
'spacious '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'
See "str.removeprefix()" for a method that will remove a single
prefix string rather than all of a set of characters. For example:
>>> 'Arthur: three!'.lstrip('Arthur: ')
'ee!'
>>> 'Arthur: three!'.removeprefix('Arthur: ')
'three!'
static str.maketrans(x[, y[, z]])
This static method returns a translation table usable for
"str.translate()".
If there is only one argument, it must be a dictionary mapping
Unicode ordinals (integers) or characters (strings of length 1) to
Unicode ordinals, strings (of arbitrary lengths) or "None".
Character keys will then be converted to ordinals.
If there are two arguments, they must be strings of equal length,
and in the resulting dictionary, each character in x will be mapped
to the character at the same position in y. If there is a third
argument, it must be a string, whose characters will be mapped to
"None" in the result.
str.partition(sep)
Split the string at the first occurrence of *sep*, and return a
3-tuple containing the part before the separator, the separator
itself, and the part after the separator. If the separator is not
found, return a 3-tuple containing the string itself, followed by
two empty strings.
str.removeprefix(prefix, /)
If the string starts with the *prefix* string, return
"string[len(prefix):]". Otherwise, return a copy of the original
string:
>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'BaseTestCase'.removeprefix('Test')
'BaseTestCase'
Added in version 3.9.
str.removesuffix(suffix, /)
If the string ends with the *suffix* string and that *suffix* is
not empty, return "string[:-len(suffix)]". Otherwise, return a copy
of the original string:
>>> 'MiscTests'.removesuffix('Tests')
'Misc'
>>> 'TmpDirMixin'.removesuffix('Tests')
'TmpDirMixin'
Added in version 3.9.
str.replace(old, new, count=-1)
Return a copy of the string with all occurrences of substring *old*
replaced by *new*. If *count* is given, only the first *count*
occurrences are replaced. If *count* is not specified or "-1", then
all occurrences are replaced.
Changed in version 3.13: *count* is now supported as a keyword
argument.
str.rfind(sub[, start[, end]])
Return the highest index in the string where substring *sub* is
found, such that *sub* is contained within "s[start:end]".
Optional arguments *start* and *end* are interpreted as in slice
notation. Return "-1" on failure.
str.rindex(sub[, start[, end]])
Like "rfind()" but raises "ValueError" when the substring *sub* is
not found.
str.rjust(width[, fillchar])
Return the string right justified in a string of length *width*.
Padding is done using the specified *fillchar* (default is an ASCII
space). The original string is returned if *width* is less than or
equal to "len(s)".
str.rpartition(sep)
Split the string at the last occurrence of *sep*, and return a
3-tuple containing the part before the separator, the separator
itself, and the part after the separator. If the separator is not
found, return a 3-tuple containing two empty strings, followed by
the string itself.
str.rsplit(sep=None, maxsplit=-1)
Return a list of the words in the string, using *sep* as the
delimiter string. If *maxsplit* is given, at most *maxsplit* splits
are done, the *rightmost* ones. If *sep* is not specified or
"None", any whitespace string is a separator. Except for splitting
from the right, "rsplit()" behaves like "split()" which is
described in detail below.
str.rstrip([chars])
Return a copy of the string with trailing characters removed. The
*chars* argument is a string specifying the set of characters to be
removed. If omitted or "None", the *chars* argument defaults to
removing whitespace. The *chars* argument is not a suffix; rather,
all combinations of its values are stripped:
>>> ' spacious '.rstrip()
' spacious'
>>> 'mississippi'.rstrip('ipz')
'mississ'
See "str.removesuffix()" for a method that will remove a single
suffix string rather than all of a set of characters. For example:
>>> 'Monty Python'.rstrip(' Python')
'M'
>>> 'Monty Python'.removesuffix(' Python')
'Monty'
str.split(sep=None, maxsplit=-1)
Return a list of the words in the string, using *sep* as the
delimiter string. If *maxsplit* is given, at most *maxsplit*
splits are done (thus, the list will have at most "maxsplit+1"
elements). If *maxsplit* is not specified or "-1", then there is
no limit on the number of splits (all possible splits are made).
If *sep* is given, consecutive delimiters are not grouped together
and are deemed to delimit empty strings (for example,
"'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument
may consist of multiple characters as a single delimiter (to split
with multiple delimiters, use "re.split()"). Splitting an empty
string with a specified separator returns "['']".
For example:
>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']
>>> '1<>2<>3<4'.split('<>')
['1', '2', '3<4']
If *sep* is not specified or is "None", a different splitting
algorithm is applied: runs of consecutive whitespace are regarded
as a single separator, and the result will contain no empty strings
at the start or end if the string has leading or trailing
whitespace. Consequently, splitting an empty string or a string
consisting of just whitespace with a "None" separator returns "[]".
For example:
>>> '1 2 3'.split()
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
>>> ' 1 2 3 '.split()
['1', '2', '3']
str.splitlines(keepends=False)
Return a list of the lines in the string, breaking at line
boundaries. Line breaks are not included in the resulting list
unless *keepends* is given and true.
This method splits on the following line boundaries. In
particular, the boundaries are a superset of *universal newlines*.
+-------------------------+-------------------------------+
| Representation | Description |
|=========================|===============================|
| "\n" | Line Feed |
+-------------------------+-------------------------------+
| "\r" | Carriage Return |
+-------------------------+-------------------------------+
| "\r\n" | Carriage Return + Line Feed |
+-------------------------+-------------------------------+
| "\v" or "\x0b" | Line Tabulation |
+-------------------------+-------------------------------+
| "\f" or "\x0c" | Form Feed |
+-------------------------+-------------------------------+
| "\x1c" | File Separator |
+-------------------------+-------------------------------+
| "\x1d" | Group Separator |
+-------------------------+-------------------------------+
| "\x1e" | Record Separator |
+-------------------------+-------------------------------+
| "\x85" | Next Line (C1 Control Code) |
+-------------------------+-------------------------------+
| "\u2028" | Line Separator |
+-------------------------+-------------------------------+
| "\u2029" | Paragraph Separator |
+-------------------------+-------------------------------+
Changed in version 3.2: "\v" and "\f" added to list of line
boundaries.
For example:
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
Unlike "split()" when a delimiter string *sep* is given, this
method returns an empty list for the empty string, and a terminal
line break does not result in an extra line:
>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']
For comparison, "split('\n')" gives:
>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']
str.startswith(prefix[, start[, end]])
Return "True" if string starts with the *prefix*, otherwise return
"False". *prefix* can also be a tuple of prefixes to look for.
With optional *start*, test string beginning at that position.
With optional *end*, stop comparing string at that position.
str.strip([chars])
Return a copy of the string with the leading and trailing
characters removed. The *chars* argument is a string specifying the
set of characters to be removed. If omitted or "None", the *chars*
argument defaults to removing whitespace. The *chars* argument is
not a prefix or suffix; rather, all combinations of its values are
stripped:
>>> ' spacious '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'
The outermost leading and trailing *chars* argument values are
stripped from the string. Characters are removed from the leading
end until reaching a string character that is not contained in the
set of characters in *chars*. A similar action takes place on the
trailing end. For example:
>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'
str.swapcase()
Return a copy of the string with uppercase characters converted to
lowercase and vice versa. Note that it is not necessarily true that
"s.swapcase().swapcase() == s".
str.title()
Return a titlecased version of the string where words start with an
uppercase character and the remaining characters are lowercase.
For example:
>>> 'Hello world'.title()
'Hello World'
The algorithm uses a simple language-independent definition of a
word as groups of consecutive letters. The definition works in
many contexts but it means that apostrophes in contractions and
possessives form word boundaries, which may not be the desired
result:
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
The "string.capwords()" function does not have this problem, as it
splits words on spaces only.
Alternatively, a workaround for apostrophes can be constructed
using regular expressions:
>>> import re
>>> def titlecase(s):
... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
... lambda mo: mo.group(0).capitalize(),
... s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."
str.translate(table)
Return a copy of the string in which each character has been mapped
through the given translation table. The table must be an object
that implements indexing via "__getitem__()", typically a *mapping*
or *sequence*. When indexed by a Unicode ordinal (an integer), the
table object can do any of the following: return a Unicode ordinal
or a string, to map the character to one or more other characters;
return "None", to delete the character from the return string; or
raise a "LookupError" exception, to map the character to itself.
You can use "str.maketrans()" to create a translation map from
character-to-character mappings in different formats.
See also the "codecs" module for a more flexible approach to custom
character mappings.
str.upper()
Return a copy of the string with all the cased characters [4]
converted to uppercase. Note that "s.upper().isupper()" might be
"False" if "s" contains uncased characters or if the Unicode
category of the resulting character(s) is not “Lu” (Letter,
uppercase), but e.g. “Lt” (Letter, titlecase).
The uppercasing algorithm used is described in section 3.13
‘Default Case Folding’ of the Unicode Standard.
str.zfill(width)
Return a copy of the string left filled with ASCII "'0'" digits to
make a string of length *width*. A leading sign prefix
("'+'"/"'-'") is handled by inserting the padding *after* the sign
character rather than before. The original string is returned if
*width* is less than or equal to "len(s)".
For example:
>>> "42".zfill(5)
'00042'
>>> "-42".zfill(5)
'-0042'
''',
'strings': '''String and Bytes literals
*************************
String literals are described by the following lexical definitions:
stringliteral: [stringprefix](shortstring | longstring)
stringprefix: "r" | "u" | "R" | "U" | "f" | "F"
| "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF"
shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring: "\'\'\'" longstringitem* "\'\'\'" | '"""' longstringitem* '"""'
shortstringitem: shortstringchar | stringescapeseq
longstringitem: longstringchar | stringescapeseq
shortstringchar: <any source character except "\\" or newline or the quote>
longstringchar: <any source character except "\\">
stringescapeseq: "\\" <any source character>
bytesliteral: bytesprefix(shortbytes | longbytes)
bytesprefix: "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"
shortbytes: "'" shortbytesitem* "'" | '"' shortbytesitem* '"'
longbytes: "\'\'\'" longbytesitem* "\'\'\'" | '"""' longbytesitem* '"""'
shortbytesitem: shortbyteschar | bytesescapeseq
longbytesitem: longbyteschar | bytesescapeseq
shortbyteschar: <any ASCII character except "\\" or newline or the quote>
longbyteschar: <any ASCII character except "\\">
bytesescapeseq: "\\" <any ASCII character>
One syntactic restriction not indicated by these productions is that
whitespace is not allowed between the "stringprefix" or "bytesprefix"
and the rest of the literal. The source character set is defined by
the encoding declaration; it is UTF-8 if no encoding declaration is
given in the source file; see section Encoding declarations.
In plain English: Both types of literals can be enclosed in matching
single quotes ("'") or double quotes ("""). They can also be enclosed
in matching groups of three single or double quotes (these are
generally referred to as *triple-quoted strings*). The backslash ("\\")
character is used to give special meaning to otherwise ordinary
characters like "n", which means ‘newline’ when escaped ("\\n"). It can
also be used to escape characters that otherwise have a special
meaning, such as newline, backslash itself, or the quote character.
See escape sequences below for examples.
Bytes literals are always prefixed with "'b'" or "'B'"; they produce
an instance of the "bytes" type instead of the "str" type. They may
only contain ASCII characters; bytes with a numeric value of 128 or
greater must be expressed with escapes.
Both string and bytes literals may optionally be prefixed with a
letter "'r'" or "'R'"; such constructs are called *raw string
literals* and *raw bytes literals* respectively and treat backslashes
as literal characters. As a result, in raw string literals, "'\\U'"
and "'\\u'" escapes are not treated specially.
Added in version 3.3: The "'rb'" prefix of raw bytes literals has been
added as a synonym of "'br'".Support for the unicode legacy literal
("u'value'") was reintroduced to simplify the maintenance of dual
Python 2.x and 3.x codebases. See **PEP 414** for more information.
A string literal with "'f'" or "'F'" in its prefix is a *formatted
string literal*; see f-strings. The "'f'" may be combined with "'r'",
but not with "'b'" or "'u'", therefore raw formatted strings are
possible, but formatted bytes literals are not.
In triple-quoted literals, unescaped newlines and quotes are allowed
(and are retained), except that three unescaped quotes in a row
terminate the literal. (A “quote” is the character used to open the
literal, i.e. either "'" or """.)
Escape sequences
================
Unless an "'r'" or "'R'" prefix is present, escape sequences in string
and bytes literals are interpreted according to rules similar to those
used by Standard C. The recognized escape sequences are:
+---------------------------+-----------------------------------+---------+
| Escape Sequence | Meaning | Notes |
|===========================|===================================|=========|
| "\\"<newline> | Backslash and newline ignored | (1) |
+---------------------------+-----------------------------------+---------+
| "\\\\" | Backslash ("\\") | |
+---------------------------+-----------------------------------+---------+
| "\\'" | Single quote ("'") | |
+---------------------------+-----------------------------------+---------+
| "\\"" | Double quote (""") | |
+---------------------------+-----------------------------------+---------+
| "\\a" | ASCII Bell (BEL) | |
+---------------------------+-----------------------------------+---------+
| "\\b" | ASCII Backspace (BS) | |
+---------------------------+-----------------------------------+---------+
| "\\f" | ASCII Formfeed (FF) | |
+---------------------------+-----------------------------------+---------+
| "\\n" | ASCII Linefeed (LF) | |
+---------------------------+-----------------------------------+---------+
| "\\r" | ASCII Carriage Return (CR) | |
+---------------------------+-----------------------------------+---------+
| "\\t" | ASCII Horizontal Tab (TAB) | |
+---------------------------+-----------------------------------+---------+
| "\\v" | ASCII Vertical Tab (VT) | |
+---------------------------+-----------------------------------+---------+
| "\\*ooo*" | Character with octal value *ooo* | (2,4) |
+---------------------------+-----------------------------------+---------+
| "\\x*hh*" | Character with hex value *hh* | (3,4) |
+---------------------------+-----------------------------------+---------+
Escape sequences only recognized in string literals are:
+---------------------------+-----------------------------------+---------+
| Escape Sequence | Meaning | Notes |
|===========================|===================================|=========|
| "\\N{*name*}" | Character named *name* in the | (5) |
| | Unicode database | |
+---------------------------+-----------------------------------+---------+
| "\\u*xxxx*" | Character with 16-bit hex value | (6) |
| | *xxxx* | |
+---------------------------+-----------------------------------+---------+
| "\\U*xxxxxxxx*" | Character with 32-bit hex value | (7) |
| | *xxxxxxxx* | |
+---------------------------+-----------------------------------+---------+
Notes:
1. A backslash can be added at the end of a line to ignore the
newline:
>>> 'This string will not include \\
... backslashes or newline characters.'
'This string will not include backslashes or newline characters.'
The same result can be achieved using triple-quoted strings, or
parentheses and string literal concatenation.
2. As in Standard C, up to three octal digits are accepted.
Changed in version 3.11: Octal escapes with value larger than
"0o377" produce a "DeprecationWarning".
Changed in version 3.12: Octal escapes with value larger than
"0o377" produce a "SyntaxWarning". In a future Python version they
will be eventually a "SyntaxError".
3. Unlike in Standard C, exactly two hex digits are required.
4. In a bytes literal, hexadecimal and octal escapes denote the byte
with the given value. In a string literal, these escapes denote a
Unicode character with the given value.
5. Changed in version 3.3: Support for name aliases [1] has been
added.
6. Exactly four hex digits are required.
7. Any Unicode character can be encoded this way. Exactly eight hex
digits are required.
Unlike Standard C, all unrecognized escape sequences are left in the
string unchanged, i.e., *the backslash is left in the result*. (This
behavior is useful when debugging: if an escape sequence is mistyped,
the resulting output is more easily recognized as broken.) It is also
important to note that the escape sequences only recognized in string
literals fall into the category of unrecognized escapes for bytes
literals.
Changed in version 3.6: Unrecognized escape sequences produce a
"DeprecationWarning".
Changed in version 3.12: Unrecognized escape sequences produce a
"SyntaxWarning". In a future Python version they will be eventually a
"SyntaxError".
Even in a raw literal, quotes can be escaped with a backslash, but the
backslash remains in the result; for example, "r"\\""" is a valid
string literal consisting of two characters: a backslash and a double
quote; "r"\\"" is not a valid string literal (even a raw string cannot
end in an odd number of backslashes). Specifically, *a raw literal
cannot end in a single backslash* (since the backslash would escape
the following quote character). Note also that a single backslash
followed by a newline is interpreted as those two characters as part
of the literal, *not* as a line continuation.
''',
'subscriptions': r'''Subscriptions
*************
The subscription of an instance of a container class will generally
select an element from the container. The subscription of a *generic
class* will generally return a GenericAlias object.
subscription: primary "[" flexible_expression_list "]"
When an object is subscripted, the interpreter will evaluate the
primary and the expression list.
The primary must evaluate to an object that supports subscription. An
object may support subscription through defining one or both of
"__getitem__()" and "__class_getitem__()". When the primary is
subscripted, the evaluated result of the expression list will be
passed to one of these methods. For more details on when
"__class_getitem__" is called instead of "__getitem__", see
__class_getitem__ versus __getitem__.
If the expression list contains at least one comma, or if any of the
expressions are starred, the expression list will evaluate to a
"tuple" containing the items of the expression list. Otherwise, the
expression list will evaluate to the value of the list’s sole member.
Changed in version 3.11: Expressions in an expression list may be
starred. See **PEP 646**.
For built-in objects, there are two types of objects that support
subscription via "__getitem__()":
1. Mappings. If the primary is a *mapping*, the expression list must
evaluate to an object whose value is one of the keys of the
mapping, and the subscription selects the value in the mapping that
corresponds to that key. An example of a builtin mapping class is
the "dict" class.
2. Sequences. If the primary is a *sequence*, the expression list must
evaluate to an "int" or a "slice" (as discussed in the following
section). Examples of builtin sequence classes include the "str",
"list" and "tuple" classes.
The formal syntax makes no special provision for negative indices in
*sequences*. However, built-in sequences all provide a "__getitem__()"
method that interprets negative indices by adding the length of the
sequence to the index so that, for example, "x[-1]" selects the last
item of "x". The resulting value must be a nonnegative integer less
than the number of items in the sequence, and the subscription selects
the item whose index is that value (counting from zero). Since the
support for negative indices and slicing occurs in the object’s
"__getitem__()" method, subclasses overriding this method will need to
explicitly add that support.
A "string" is a special kind of sequence whose items are *characters*.
A character is not a separate data type but a string of exactly one
character.
''',
'truth': r'''Truth Value Testing
*******************
Any object can be tested for truth value, for use in an "if" or
"while" condition or as operand of the Boolean operations below.
By default, an object is considered true unless its class defines
either a "__bool__()" method that returns "False" or a "__len__()"
method that returns zero, when called with the object. [1] Here are
most of the built-in objects considered false:
* constants defined to be false: "None" and "False"
* zero of any numeric type: "0", "0.0", "0j", "Decimal(0)",
"Fraction(0, 1)"
* empty sequences and collections: "''", "()", "[]", "{}", "set()",
"range(0)"
Operations and built-in functions that have a Boolean result always
return "0" or "False" for false and "1" or "True" for true, unless
otherwise stated. (Important exception: the Boolean operations "or"
and "and" always return one of their operands.)
''',
'try': r'''The "try" statement
*******************
The "try" statement specifies exception handlers and/or cleanup code
for a group of statements:
try_stmt: try1_stmt | try2_stmt | try3_stmt
try1_stmt: "try" ":" suite
("except" [expression ["as" identifier]] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try2_stmt: "try" ":" suite
("except" "*" expression ["as" identifier] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try3_stmt: "try" ":" suite
"finally" ":" suite
Additional information on exceptions can be found in section
Exceptions, and information on using the "raise" statement to generate
exceptions may be found in section The raise statement.
Changed in version 3.14: Support for optionally dropping grouping
parentheses when using multiple exception types. See **PEP 758**.
"except" clause
===============
The "except" clause(s) specify one or more exception handlers. When no
exception occurs in the "try" clause, no exception handler is
executed. When an exception occurs in the "try" suite, a search for an
exception handler is started. This search inspects the "except"
clauses in turn until one is found that matches the exception. An
expression-less "except" clause, if present, must be last; it matches
any exception.
For an "except" clause with an expression, the expression must
evaluate to an exception type or a tuple of exception types.
Parentheses can be dropped if multiple exception types are provided
and the "as" clause is not used. The raised exception matches an
"except" clause whose expression evaluates to the class or a *non-
virtual base class* of the exception object, or to a tuple that
contains such a class.
If no "except" clause matches the exception, the search for an
exception handler continues in the surrounding code and on the
invocation stack. [1]
If the evaluation of an expression in the header of an "except" clause
raises an exception, the original search for a handler is canceled and
a search starts for the new exception in the surrounding code and on
the call stack (it is treated as if the entire "try" statement raised
the exception).
When a matching "except" clause is found, the exception is assigned to
the target specified after the "as" keyword in that "except" clause,
if present, and the "except" clause’s suite is executed. All "except"
clauses must have an executable block. When the end of this block is
reached, execution continues normally after the entire "try"
statement. (This means that if two nested handlers exist for the same
exception, and the exception occurs in the "try" clause of the inner
handler, the outer handler will not handle the exception.)
When an exception has been assigned using "as target", it is cleared
at the end of the "except" clause. This is as if
except E as N:
foo
was translated to
except E as N:
try:
foo
finally:
del N
This means the exception must be assigned to a different name to be
able to refer to it after the "except" clause. Exceptions are cleared
because with the traceback attached to them, they form a reference
cycle with the stack frame, keeping all locals in that frame alive
until the next garbage collection occurs.
Before an "except" clause’s suite is executed, the exception is stored
in the "sys" module, where it can be accessed from within the body of
the "except" clause by calling "sys.exception()". When leaving an
exception handler, the exception stored in the "sys" module is reset
to its previous value:
>>> print(sys.exception())
None
>>> try:
... raise TypeError
... except:
... print(repr(sys.exception()))
... try:
... raise ValueError
... except:
... print(repr(sys.exception()))
... print(repr(sys.exception()))
...
TypeError()
ValueError()
TypeError()
>>> print(sys.exception())
None
"except*" clause
================
The "except*" clause(s) are used for handling "ExceptionGroup"s. The
exception type for matching is interpreted as in the case of "except",
but in the case of exception groups we can have partial matches when
the type matches some of the exceptions in the group. This means that
multiple "except*" clauses can execute, each handling part of the
exception group. Each clause executes at most once and handles an
exception group of all matching exceptions. Each exception in the
group is handled by at most one "except*" clause, the first that
matches it.
>>> try:
... raise ExceptionGroup("eg",
... [ValueError(1), TypeError(2), OSError(3), OSError(4)])
... except* TypeError as e:
... print(f'caught {type(e)} with nested {e.exceptions}')
... except* OSError as e:
... print(f'caught {type(e)} with nested {e.exceptions}')
...
caught <class 'ExceptionGroup'> with nested (TypeError(2),)
caught <class 'ExceptionGroup'> with nested (OSError(3), OSError(4))
+ Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module>
| ExceptionGroup: eg
+-+---------------- 1 ----------------
| ValueError: 1
+------------------------------------
Any remaining exceptions that were not handled by any "except*" clause
are re-raised at the end, along with all exceptions that were raised
from within the "except*" clauses. If this list contains more than one
exception to reraise, they are combined into an exception group.
If the raised exception is not an exception group and its type matches
one of the "except*" clauses, it is caught and wrapped by an exception
group with an empty message string.
>>> try:
... raise BlockingIOError
... except* BlockingIOError as e:
... print(repr(e))
...
ExceptionGroup('', (BlockingIOError()))
An "except*" clause must have a matching expression; it cannot be
"except*:". Furthermore, this expression cannot contain exception
group types, because that would have ambiguous semantics.
It is not possible to mix "except" and "except*" in the same "try".
"break", "continue" and "return" cannot appear in an "except*" clause.
"else" clause
=============
The optional "else" clause is executed if the control flow leaves the
"try" suite, no exception was raised, and no "return", "continue", or
"break" statement was executed. Exceptions in the "else" clause are
not handled by the preceding "except" clauses.
"finally" clause
================
If "finally" is present, it specifies a ‘cleanup’ handler. The "try"
clause is executed, including any "except" and "else" clauses. If an
exception occurs in any of the clauses and is not handled, the
exception is temporarily saved. The "finally" clause is executed. If
there is a saved exception it is re-raised at the end of the "finally"
clause. If the "finally" clause raises another exception, the saved
exception is set as the context of the new exception. If the "finally"
clause executes a "return", "break" or "continue" statement, the saved
exception is discarded. For example, this function returns 42.
def f():
try:
1/0
finally:
return 42
The exception information is not available to the program during
execution of the "finally" clause.
When a "return", "break" or "continue" statement is executed in the
"try" suite of a "try"…"finally" statement, the "finally" clause is
also executed ‘on the way out.’
The return value of a function is determined by the last "return"
statement executed. Since the "finally" clause always executes, a
"return" statement executed in the "finally" clause will always be the
last one executed. The following function returns ‘finally’.
def foo():
try:
return 'try'
finally:
return 'finally'
Changed in version 3.8: Prior to Python 3.8, a "continue" statement
was illegal in the "finally" clause due to a problem with the
implementation.
Changed in version 3.14: The compiler emits a "SyntaxWarning" when a
"return", "break" or "continue" appears in a "finally" block (see
**PEP 765**).
''',
'types': r'''The standard type hierarchy
***************************
Below is a list of the types that are built into Python. Extension
modules (written in C, Java, or other languages, depending on the
implementation) can define additional types. Future versions of
Python may add types to the type hierarchy (e.g., rational numbers,
efficiently stored arrays of integers, etc.), although such additions
will often be provided via the standard library instead.
Some of the type descriptions below contain a paragraph listing
‘special attributes.’ These are attributes that provide access to the
implementation and are not intended for general use. Their definition
may change in the future.
None
====
This type has a single value. There is a single object with this
value. This object is accessed through the built-in name "None". It is
used to signify the absence of a value in many situations, e.g., it is
returned from functions that don’t explicitly return anything. Its
truth value is false.
NotImplemented
==============
This type has a single value. There is a single object with this
value. This object is accessed through the built-in name
"NotImplemented". Numeric methods and rich comparison methods should
return this value if they do not implement the operation for the
operands provided. (The interpreter will then try the reflected
operation, or some other fallback, depending on the operator.) It
should not be evaluated in a boolean context.
See Implementing the arithmetic operations for more details.
Changed in version 3.9: Evaluating "NotImplemented" in a boolean
context was deprecated.
Changed in version 3.14: Evaluating "NotImplemented" in a boolean
context now raises a "TypeError". It previously evaluated to "True"
and emitted a "DeprecationWarning" since Python 3.9.
Ellipsis
========
This type has a single value. There is a single object with this
value. This object is accessed through the literal "..." or the built-
in name "Ellipsis". Its truth value is true.
"numbers.Number"
================
These are created by numeric literals and returned as results by
arithmetic operators and arithmetic built-in functions. Numeric
objects are immutable; once created their value never changes. Python
numbers are of course strongly related to mathematical numbers, but
subject to the limitations of numerical representation in computers.
The string representations of the numeric classes, computed by
"__repr__()" and "__str__()", have the following properties:
* They are valid numeric literals which, when passed to their class
constructor, produce an object having the value of the original
numeric.
* The representation is in base 10, when possible.
* Leading zeros, possibly excepting a single zero before a decimal
point, are not shown.
* Trailing zeros, possibly excepting a single zero after a decimal
point, are not shown.
* A sign is shown only when the number is negative.
Python distinguishes between integers, floating-point numbers, and
complex numbers:
"numbers.Integral"
------------------
These represent elements from the mathematical set of integers
(positive and negative).
Note:
The rules for integer representation are intended to give the most
meaningful interpretation of shift and mask operations involving
negative integers.
There are two types of integers:
Integers ("int")
These represent numbers in an unlimited range, subject to available
(virtual) memory only. For the purpose of shift and mask
operations, a binary representation is assumed, and negative
numbers are represented in a variant of 2’s complement which gives
the illusion of an infinite string of sign bits extending to the
left.
Booleans ("bool")
These represent the truth values False and True. The two objects
representing the values "False" and "True" are the only Boolean
objects. The Boolean type is a subtype of the integer type, and
Boolean values behave like the values 0 and 1, respectively, in
almost all contexts, the exception being that when converted to a
string, the strings ""False"" or ""True"" are returned,
respectively.
"numbers.Real" ("float")
------------------------
These represent machine-level double precision floating-point numbers.
You are at the mercy of the underlying machine architecture (and C or
Java implementation) for the accepted range and handling of overflow.
Python does not support single-precision floating-point numbers; the
savings in processor and memory usage that are usually the reason for
using these are dwarfed by the overhead of using objects in Python, so
there is no reason to complicate the language with two kinds of
floating-point numbers.
"numbers.Complex" ("complex")
-----------------------------
These represent complex numbers as a pair of machine-level double
precision floating-point numbers. The same caveats apply as for
floating-point numbers. The real and imaginary parts of a complex
number "z" can be retrieved through the read-only attributes "z.real"
and "z.imag".
Sequences
=========
These represent finite ordered sets indexed by non-negative numbers.
The built-in function "len()" returns the number of items of a
sequence. When the length of a sequence is *n*, the index set contains
the numbers 0, 1, …, *n*-1. Item *i* of sequence *a* is selected by
"a[i]". Some sequences, including built-in sequences, interpret
negative subscripts by adding the sequence length. For example,
"a[-2]" equals "a[n-2]", the second to last item of sequence a with
length "n".
Sequences also support slicing: "a[i:j]" selects all items with index
*k* such that *i* "<=" *k* "<" *j*. When used as an expression, a
slice is a sequence of the same type. The comment above about negative
indexes also applies to negative slice positions.
Some sequences also support “extended slicing” with a third “step”
parameter: "a[i:j:k]" selects all items of *a* with index *x* where "x
= i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.
Sequences are distinguished according to their mutability:
Immutable sequences
-------------------
An object of an immutable sequence type cannot change once it is
created. (If the object contains references to other objects, these
other objects may be mutable and may be changed; however, the
collection of objects directly referenced by an immutable object
cannot change.)
The following types are immutable sequences:
Strings
A string is a sequence of values that represent Unicode code
points. All the code points in the range "U+0000 - U+10FFFF" can be
represented in a string. Python doesn’t have a char type; instead,
every code point in the string is represented as a string object
with length "1". The built-in function "ord()" converts a code
point from its string form to an integer in the range "0 - 10FFFF";
"chr()" converts an integer in the range "0 - 10FFFF" to the
corresponding length "1" string object. "str.encode()" can be used
to convert a "str" to "bytes" using the given text encoding, and
"bytes.decode()" can be used to achieve the opposite.
Tuples
The items of a tuple are arbitrary Python objects. Tuples of two or
more items are formed by comma-separated lists of expressions. A
tuple of one item (a ‘singleton’) can be formed by affixing a comma
to an expression (an expression by itself does not create a tuple,
since parentheses must be usable for grouping of expressions). An
empty tuple can be formed by an empty pair of parentheses.
Bytes
A bytes object is an immutable array. The items are 8-bit bytes,
represented by integers in the range 0 <= x < 256. Bytes literals
(like "b'abc'") and the built-in "bytes()" constructor can be used
to create bytes objects. Also, bytes objects can be decoded to
strings via the "decode()" method.
Mutable sequences
-----------------
Mutable sequences can be changed after they are created. The
subscription and slicing notations can be used as the target of
assignment and "del" (delete) statements.
Note:
The "collections" and "array" module provide additional examples of
mutable sequence types.
There are currently two intrinsic mutable sequence types:
Lists
The items of a list are arbitrary Python objects. Lists are formed
by placing a comma-separated list of expressions in square
brackets. (Note that there are no special cases needed to form
lists of length 0 or 1.)
Byte Arrays
A bytearray object is a mutable array. They are created by the
built-in "bytearray()" constructor. Aside from being mutable (and
hence unhashable), byte arrays otherwise provide the same interface
and functionality as immutable "bytes" objects.
Set types
=========
These represent unordered, finite sets of unique, immutable objects.
As such, they cannot be indexed by any subscript. However, they can be
iterated over, and the built-in function "len()" returns the number of
items in a set. Common uses for sets are fast membership testing,
removing duplicates from a sequence, and computing mathematical
operations such as intersection, union, difference, and symmetric
difference.
For set elements, the same immutability rules apply as for dictionary
keys. Note that numeric types obey the normal rules for numeric
comparison: if two numbers compare equal (e.g., "1" and "1.0"), only
one of them can be contained in a set.
There are currently two intrinsic set types:
Sets
These represent a mutable set. They are created by the built-in
"set()" constructor and can be modified afterwards by several
methods, such as "add()".
Frozen sets
These represent an immutable set. They are created by the built-in
"frozenset()" constructor. As a frozenset is immutable and
*hashable*, it can be used again as an element of another set, or
as a dictionary key.
Mappings
========
These represent finite sets of objects indexed by arbitrary index
sets. The subscript notation "a[k]" selects the item indexed by "k"
from the mapping "a"; this can be used in expressions and as the
target of assignments or "del" statements. The built-in function
"len()" returns the number of items in a mapping.
There is currently a single intrinsic mapping type:
Dictionaries
------------
These represent finite sets of objects indexed by nearly arbitrary
values. The only types of values not acceptable as keys are values
containing lists or dictionaries or other mutable types that are
compared by value rather than by object identity, the reason being
that the efficient implementation of dictionaries requires a key’s
hash value to remain constant. Numeric types used for keys obey the
normal rules for numeric comparison: if two numbers compare equal
(e.g., "1" and "1.0") then they can be used interchangeably to index
the same dictionary entry.
Dictionaries preserve insertion order, meaning that keys will be
produced in the same order they were added sequentially over the
dictionary. Replacing an existing key does not change the order,
however removing a key and re-inserting it will add it to the end
instead of keeping its old place.
Dictionaries are mutable; they can be created by the "{}" notation
(see section Dictionary displays).
The extension modules "dbm.ndbm" and "dbm.gnu" provide additional
examples of mapping types, as does the "collections" module.
Changed in version 3.7: Dictionaries did not preserve insertion order
in versions of Python before 3.6. In CPython 3.6, insertion order was
preserved, but it was considered an implementation detail at that time
rather than a language guarantee.
Callable types
==============
These are the types to which the function call operation (see section
Calls) can be applied:
User-defined functions
----------------------
A user-defined function object is created by a function definition
(see section Function definitions). It should be called with an
argument list containing the same number of items as the function’s
formal parameter list.
Special read-only attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+----------------------------------------------------+----------------------------------------------------+
| Attribute | Meaning |
|====================================================|====================================================|
| function.__globals__ | A reference to the "dictionary" that holds the |
| | function’s global variables – the global namespace |
| | of the module in which the function was defined. |
+----------------------------------------------------+----------------------------------------------------+
| function.__closure__ | "None" or a "tuple" of cells that contain bindings |
| | for the names specified in the "co_freevars" |
| | attribute of the function’s "code object". A cell |
| | object has the attribute "cell_contents". This can |
| | be used to get the value of the cell, as well as |
| | set the value. |
+----------------------------------------------------+----------------------------------------------------+
Special writable attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Most of these attributes check the type of the assigned value:
+----------------------------------------------------+----------------------------------------------------+
| Attribute | Meaning |
|====================================================|====================================================|
| function.__doc__ | The function’s documentation string, or "None" if |
| | unavailable. |
+----------------------------------------------------+----------------------------------------------------+
| function.__name__ | The function’s name. See also: "__name__ |
| | attributes". |
+----------------------------------------------------+----------------------------------------------------+
| function.__qualname__ | The function’s *qualified name*. See also: |
| | "__qualname__ attributes". Added in version 3.3. |
+----------------------------------------------------+----------------------------------------------------+
| function.__module__ | The name of the module the function was defined |
| | in, or "None" if unavailable. |
+----------------------------------------------------+----------------------------------------------------+
| function.__defaults__ | A "tuple" containing default *parameter* values |
| | for those parameters that have defaults, or "None" |
| | if no parameters have a default value. |
+----------------------------------------------------+----------------------------------------------------+
| function.__code__ | The code object representing the compiled function |
| | body. |
+----------------------------------------------------+----------------------------------------------------+
| function.__dict__ | The namespace supporting arbitrary function |
| | attributes. See also: "__dict__ attributes". |
+----------------------------------------------------+----------------------------------------------------+
| function.__annotations__ | A "dictionary" containing annotations of |
| | *parameters*. The keys of the dictionary are the |
| | parameter names, and "'return'" for the return |
| | annotation, if provided. See also: |
| | "object.__annotations__". Changed in version |
| | 3.14: Annotations are now lazily evaluated. See |
| | **PEP 649**. |
+----------------------------------------------------+----------------------------------------------------+
| function.__annotate__ | The *annotate function* for this function, or |
| | "None" if the function has no annotations. See |
| | "object.__annotate__". Added in version 3.14. |
+----------------------------------------------------+----------------------------------------------------+
| function.__kwdefaults__ | A "dictionary" containing defaults for keyword- |
| | only *parameters*. |
+----------------------------------------------------+----------------------------------------------------+
| function.__type_params__ | A "tuple" containing the type parameters of a |
| | generic function. Added in version 3.12. |
+----------------------------------------------------+----------------------------------------------------+
Function objects also support getting and setting arbitrary
attributes, which can be used, for example, to attach metadata to
functions. Regular attribute dot-notation is used to get and set such
attributes.
**CPython implementation detail:** CPython’s current implementation
only supports function attributes on user-defined functions. Function
attributes on built-in functions may be supported in the future.
Additional information about a function’s definition can be retrieved
from its code object (accessible via the "__code__" attribute).
Instance methods
----------------
An instance method object combines a class, a class instance and any
callable object (normally a user-defined function).
Special read-only attributes:
+----------------------------------------------------+----------------------------------------------------+
| method.__self__ | Refers to the class instance object to which the |
| | method is bound |
+----------------------------------------------------+----------------------------------------------------+
| method.__func__ | Refers to the original function object |
+----------------------------------------------------+----------------------------------------------------+
| method.__doc__ | The method’s documentation (same as |
| | "method.__func__.__doc__"). A "string" if the |
| | original function had a docstring, else "None". |
+----------------------------------------------------+----------------------------------------------------+
| method.__name__ | The name of the method (same as |
| | "method.__func__.__name__") |
+----------------------------------------------------+----------------------------------------------------+
| method.__module__ | The name of the module the method was defined in, |
| | or "None" if unavailable. |
+----------------------------------------------------+----------------------------------------------------+
Methods also support accessing (but not setting) the arbitrary
function attributes on the underlying function object.
User-defined method objects may be created when getting an attribute
of a class (perhaps via an instance of that class), if that attribute
is a user-defined function object or a "classmethod" object.
When an instance method object is created by retrieving a user-defined
function object from a class via one of its instances, its "__self__"
attribute is the instance, and the method object is said to be
*bound*. The new method’s "__func__" attribute is the original
function object.
When an instance method object is created by retrieving a
"classmethod" object from a class or instance, its "__self__"
attribute is the class itself, and its "__func__" attribute is the
function object underlying the class method.
When an instance method object is called, the underlying function
("__func__") is called, inserting the class instance ("__self__") in
front of the argument list. For instance, when "C" is a class which
contains a definition for a function "f()", and "x" is an instance of
"C", calling "x.f(1)" is equivalent to calling "C.f(x, 1)".
When an instance method object is derived from a "classmethod" object,
the “class instance” stored in "__self__" will actually be the class
itself, so that calling either "x.f(1)" or "C.f(1)" is equivalent to
calling "f(C,1)" where "f" is the underlying function.
It is important to note that user-defined functions which are
attributes of a class instance are not converted to bound methods;
this *only* happens when the function is an attribute of the class.
Generator functions
-------------------
A function or method which uses the "yield" statement (see section The
yield statement) is called a *generator function*. Such a function,
when called, always returns an *iterator* object which can be used to
execute the body of the function: calling the iterator’s
"iterator.__next__()" method will cause the function to execute until
it provides a value using the "yield" statement. When the function
executes a "return" statement or falls off the end, a "StopIteration"
exception is raised and the iterator will have reached the end of the
set of values to be returned.
Coroutine functions
-------------------
A function or method which is defined using "async def" is called a
*coroutine function*. Such a function, when called, returns a
*coroutine* object. It may contain "await" expressions, as well as
"async with" and "async for" statements. See also the Coroutine
Objects section.
Asynchronous generator functions
--------------------------------
A function or method which is defined using "async def" and which uses
the "yield" statement is called a *asynchronous generator function*.
Such a function, when called, returns an *asynchronous iterator*
object which can be used in an "async for" statement to execute the
body of the function.
Calling the asynchronous iterator’s "aiterator.__anext__" method will
return an *awaitable* which when awaited will execute until it
provides a value using the "yield" expression. When the function
executes an empty "return" statement or falls off the end, a
"StopAsyncIteration" exception is raised and the asynchronous iterator
will have reached the end of the set of values to be yielded.
Built-in functions
------------------
A built-in function object is a wrapper around a C function. Examples
of built-in functions are "len()" and "math.sin()" ("math" is a
standard built-in module). The number and type of the arguments are
determined by the C function. Special read-only attributes:
* "__doc__" is the function’s documentation string, or "None" if
unavailable. See "function.__doc__".
* "__name__" is the function’s name. See "function.__name__".
* "__self__" is set to "None" (but see the next item).
* "__module__" is the name of the module the function was defined in
or "None" if unavailable. See "function.__module__".
Built-in methods
----------------
This is really a different disguise of a built-in function, this time
containing an object passed to the C function as an implicit extra
argument. An example of a built-in method is "alist.append()",
assuming *alist* is a list object. In this case, the special read-only
attribute "__self__" is set to the object denoted by *alist*. (The
attribute has the same semantics as it does with "other instance
methods".)
Classes
-------
Classes are callable. These objects normally act as factories for new
instances of themselves, but variations are possible for class types
that override "__new__()". The arguments of the call are passed to
"__new__()" and, in the typical case, to "__init__()" to initialize
the new instance.
Class Instances
---------------
Instances of arbitrary classes can be made callable by defining a
"__call__()" method in their class.
Modules
=======
Modules are a basic organizational unit of Python code, and are
created by the import system as invoked either by the "import"
statement, or by calling functions such as "importlib.import_module()"
and built-in "__import__()". A module object has a namespace
implemented by a "dictionary" object (this is the dictionary
referenced by the "__globals__" attribute of functions defined in the
module). Attribute references are translated to lookups in this
dictionary, e.g., "m.x" is equivalent to "m.__dict__["x"]". A module
object does not contain the code object used to initialize the module
(since it isn’t needed once the initialization is done).
Attribute assignment updates the module’s namespace dictionary, e.g.,
"m.x = 1" is equivalent to "m.__dict__["x"] = 1".
Import-related attributes on module objects
-------------------------------------------
Module objects have the following attributes that relate to the import
system. When a module is created using the machinery associated with
the import system, these attributes are filled in based on the
module’s *spec*, before the *loader* executes and loads the module.
To create a module dynamically rather than using the import system,
it’s recommended to use "importlib.util.module_from_spec()", which
will set the various import-controlled attributes to appropriate
values. It’s also possible to use the "types.ModuleType" constructor
to create modules directly, but this technique is more error-prone, as
most attributes must be manually set on the module object after it has
been created when using this approach.
Caution:
With the exception of "__name__", it is **strongly** recommended
that you rely on "__spec__" and its attributes instead of any of the
other individual attributes listed in this subsection. Note that
updating an attribute on "__spec__" will not update the
corresponding attribute on the module itself:
>>> import typing
>>> typing.__name__, typing.__spec__.name
('typing', 'typing')
>>> typing.__spec__.name = 'spelling'
>>> typing.__name__, typing.__spec__.name
('typing', 'spelling')
>>> typing.__name__ = 'keyboard_smashing'
>>> typing.__name__, typing.__spec__.name
('keyboard_smashing', 'spelling')
module.__name__
The name used to uniquely identify the module in the import system.
For a directly executed module, this will be set to ""__main__"".
This attribute must be set to the fully qualified name of the
module. It is expected to match the value of
"module.__spec__.name".
module.__spec__
A record of the module’s import-system-related state.
Set to the "module spec" that was used when importing the module.
See Module specs for more details.
Added in version 3.4.
module.__package__
The *package* a module belongs to.
If the module is top-level (that is, not a part of any specific
package) then the attribute should be set to "''" (the empty
string). Otherwise, it should be set to the name of the module’s
package (which can be equal to "module.__name__" if the module
itself is a package). See **PEP 366** for further details.
This attribute is used instead of "__name__" to calculate explicit
relative imports for main modules. It defaults to "None" for
modules created dynamically using the "types.ModuleType"
constructor; use "importlib.util.module_from_spec()" instead to
ensure the attribute is set to a "str".
It is **strongly** recommended that you use
"module.__spec__.parent" instead of "module.__package__".
"__package__" is now only used as a fallback if "__spec__.parent"
is not set, and this fallback path is deprecated.
Changed in version 3.4: This attribute now defaults to "None" for
modules created dynamically using the "types.ModuleType"
constructor. Previously the attribute was optional.
Changed in version 3.6: The value of "__package__" is expected to
be the same as "__spec__.parent". "__package__" is now only used as
a fallback during import resolution if "__spec__.parent" is not
defined.
Changed in version 3.10: "ImportWarning" is raised if an import
resolution falls back to "__package__" instead of
"__spec__.parent".
Changed in version 3.12: Raise "DeprecationWarning" instead of
"ImportWarning" when falling back to "__package__" during import
resolution.
Deprecated since version 3.13, will be removed in version 3.15:
"__package__" will cease to be set or taken into consideration by
the import system or standard library.
module.__loader__
The *loader* object that the import machinery used to load the
module.
This attribute is mostly useful for introspection, but can be used
for additional loader-specific functionality, for example getting
data associated with a loader.
"__loader__" defaults to "None" for modules created dynamically
using the "types.ModuleType" constructor; use
"importlib.util.module_from_spec()" instead to ensure the attribute
is set to a *loader* object.
It is **strongly** recommended that you use
"module.__spec__.loader" instead of "module.__loader__".
Changed in version 3.4: This attribute now defaults to "None" for
modules created dynamically using the "types.ModuleType"
constructor. Previously the attribute was optional.
Deprecated since version 3.12, will be removed in version 3.16:
Setting "__loader__" on a module while failing to set
"__spec__.loader" is deprecated. In Python 3.16, "__loader__" will
cease to be set or taken into consideration by the import system or
the standard library.
module.__path__
A (possibly empty) *sequence* of strings enumerating the locations
where the package’s submodules will be found. Non-package modules
should not have a "__path__" attribute. See __path__ attributes on
modules for more details.
It is **strongly** recommended that you use
"module.__spec__.submodule_search_locations" instead of
"module.__path__".
module.__file__
module.__cached__
"__file__" and "__cached__" are both optional attributes that may
or may not be set. Both attributes should be a "str" when they are
available.
"__file__" indicates the pathname of the file from which the module
was loaded (if loaded from a file), or the pathname of the shared
library file for extension modules loaded dynamically from a shared
library. It might be missing for certain types of modules, such as
C modules that are statically linked into the interpreter, and the
import system may opt to leave it unset if it has no semantic
meaning (for example, a module loaded from a database).
If "__file__" is set then the "__cached__" attribute might also be
set, which is the path to any compiled version of the code (for
example, a byte-compiled file). The file does not need to exist to
set this attribute; the path can simply point to where the compiled
file *would* exist (see **PEP 3147**).
Note that "__cached__" may be set even if "__file__" is not set.
However, that scenario is quite atypical. Ultimately, the *loader*
is what makes use of the module spec provided by the *finder* (from
which "__file__" and "__cached__" are derived). So if a loader can
load from a cached module but otherwise does not load from a file,
that atypical scenario may be appropriate.
It is **strongly** recommended that you use
"module.__spec__.cached" instead of "module.__cached__".
Deprecated since version 3.13, will be removed in version 3.15:
Setting "__cached__" on a module while failing to set
"__spec__.cached" is deprecated. In Python 3.15, "__cached__" will
cease to be set or taken into consideration by the import system or
standard library.
Other writable attributes on module objects
-------------------------------------------
As well as the import-related attributes listed above, module objects
also have the following writable attributes:
module.__doc__
The module’s documentation string, or "None" if unavailable. See
also: "__doc__ attributes".
module.__annotations__
A dictionary containing *variable annotations* collected during
module body execution. For best practices on working with
"__annotations__", see "annotationlib".
Changed in version 3.14: Annotations are now lazily evaluated. See
**PEP 649**.
module.__annotate__
The *annotate function* for this module, or "None" if the module
has no annotations. See also: "__annotate__" attributes.
Added in version 3.14.
Module dictionaries
-------------------
Module objects also have the following special read-only attribute:
module.__dict__
The module’s namespace as a dictionary object. Uniquely among the
attributes listed here, "__dict__" cannot be accessed as a global
variable from within a module; it can only be accessed as an
attribute on module objects.
**CPython implementation detail:** Because of the way CPython
clears module dictionaries, the module dictionary will be cleared
when the module falls out of scope even if the dictionary still has
live references. To avoid this, copy the dictionary or keep the
module around while using its dictionary directly.
Custom classes
==============
Custom class types are typically created by class definitions (see
section Class definitions). A class has a namespace implemented by a
dictionary object. Class attribute references are translated to
lookups in this dictionary, e.g., "C.x" is translated to
"C.__dict__["x"]" (although there are a number of hooks which allow
for other means of locating attributes). When the attribute name is
not found there, the attribute search continues in the base classes.
This search of the base classes uses the C3 method resolution order
which behaves correctly even in the presence of ‘diamond’ inheritance
structures where there are multiple inheritance paths leading back to
a common ancestor. Additional details on the C3 MRO used by Python can
be found at The Python 2.3 Method Resolution Order.
When a class attribute reference (for class "C", say) would yield a
class method object, it is transformed into an instance method object
whose "__self__" attribute is "C". When it would yield a
"staticmethod" object, it is transformed into the object wrapped by
the static method object. See section Implementing Descriptors for
another way in which attributes retrieved from a class may differ from
those actually contained in its "__dict__".
Class attribute assignments update the class’s dictionary, never the
dictionary of a base class.
A class object can be called (see above) to yield a class instance
(see below).
Special attributes
------------------
+----------------------------------------------------+----------------------------------------------------+
| Attribute | Meaning |
|====================================================|====================================================|
| type.__name__ | The class’s name. See also: "__name__ attributes". |
+----------------------------------------------------+----------------------------------------------------+
| type.__qualname__ | The class’s *qualified name*. See also: |
| | "__qualname__ attributes". |
+----------------------------------------------------+----------------------------------------------------+
| type.__module__ | The name of the module in which the class was |
| | defined. |
+----------------------------------------------------+----------------------------------------------------+
| type.__dict__ | A "mapping proxy" providing a read-only view of |
| | the class’s namespace. See also: "__dict__ |
| | attributes". |
+----------------------------------------------------+----------------------------------------------------+
| type.__bases__ | A "tuple" containing the class’s bases. In most |
| | cases, for a class defined as "class X(A, B, C)", |
| | "X.__bases__" will be exactly equal to "(A, B, |
| | C)". |
+----------------------------------------------------+----------------------------------------------------+
| type.__doc__ | The class’s documentation string, or "None" if |
| | undefined. Not inherited by subclasses. |
+----------------------------------------------------+----------------------------------------------------+
| type.__annotations__ | A dictionary containing *variable annotations* |
| | collected during class body execution. See also: |
| | "__annotations__ attributes". For best practices |
| | on working with "__annotations__", please see |
| | "annotationlib". Where possible, use |
| | "annotationlib.get_annotations()" instead of |
| | accessing this attribute directly. Changed in |
| | version 3.14: Annotations are now lazily |
| | evaluated. See **PEP 649**. |
+----------------------------------------------------+----------------------------------------------------+
| type.__annotate__() | The *annotate function* for this class, or "None" |
| | if the class has no annotations. See also: |
| | "__annotate__ attributes". Added in version 3.14. |
+----------------------------------------------------+----------------------------------------------------+
| type.__type_params__ | A "tuple" containing the type parameters of a |
| | generic class. Added in version 3.12. |
+----------------------------------------------------+----------------------------------------------------+
| type.__static_attributes__ | A "tuple" containing names of attributes of this |
| | class which are assigned through "self.X" from any |
| | function in its body. Added in version 3.13. |
+----------------------------------------------------+----------------------------------------------------+
| type.__firstlineno__ | The line number of the first line of the class |
| | definition, including decorators. Setting the |
| | "__module__" attribute removes the |
| | "__firstlineno__" item from the type’s dictionary. |
| | Added in version 3.13. |
+----------------------------------------------------+----------------------------------------------------+
| type.__mro__ | The "tuple" of classes that are considered when |
| | looking for base classes during method resolution. |
+----------------------------------------------------+----------------------------------------------------+
Special methods
---------------
In addition to the special attributes described above, all Python
classes also have the following two methods available:
type.mro()
This method can be overridden by a metaclass to customize the
method resolution order for its instances. It is called at class
instantiation, and its result is stored in "__mro__".
type.__subclasses__()
Each class keeps a list of weak references to its immediate
subclasses. This method returns a list of all those references
still alive. The list is in definition order. Example:
>>> class A: pass
>>> class B(A): pass
>>> A.__subclasses__()
[<class 'B'>]
Class instances
===============
A class instance is created by calling a class object (see above). A
class instance has a namespace implemented as a dictionary which is
the first place in which attribute references are searched. When an
attribute is not found there, and the instance’s class has an
attribute by that name, the search continues with the class
attributes. If a class attribute is found that is a user-defined
function object, it is transformed into an instance method object
whose "__self__" attribute is the instance. Static method and class
method objects are also transformed; see above under “Classes”. See
section Implementing Descriptors for another way in which attributes
of a class retrieved via its instances may differ from the objects
actually stored in the class’s "__dict__". If no class attribute is
found, and the object’s class has a "__getattr__()" method, that is
called to satisfy the lookup.
Attribute assignments and deletions update the instance’s dictionary,
never a class’s dictionary. If the class has a "__setattr__()" or
"__delattr__()" method, this is called instead of updating the
instance dictionary directly.
Class instances can pretend to be numbers, sequences, or mappings if
they have methods with certain special names. See section Special
method names.
Special attributes
------------------
object.__class__
The class to which a class instance belongs.
object.__dict__
A dictionary or other mapping object used to store an object’s
(writable) attributes. Not all instances have a "__dict__"
attribute; see the section on __slots__ for more details.
I/O objects (also known as file objects)
========================================
A *file object* represents an open file. Various shortcuts are
available to create file objects: the "open()" built-in function, and
also "os.popen()", "os.fdopen()", and the "makefile()" method of
socket objects (and perhaps by other functions or methods provided by
extension modules).
The objects "sys.stdin", "sys.stdout" and "sys.stderr" are initialized
to file objects corresponding to the interpreter’s standard input,
output and error streams; they are all open in text mode and therefore
follow the interface defined by the "io.TextIOBase" abstract class.
Internal types
==============
A few types used internally by the interpreter are exposed to the
user. Their definitions may change with future versions of the
interpreter, but they are mentioned here for completeness.
Code objects
------------
Code objects represent *byte-compiled* executable Python code, or
*bytecode*. The difference between a code object and a function object
is that the function object contains an explicit reference to the
function’s globals (the module in which it was defined), while a code
object contains no context; also the default argument values are
stored in the function object, not in the code object (because they
represent values calculated at run-time). Unlike function objects,
code objects are immutable and contain no references (directly or
indirectly) to mutable objects.
Special read-only attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_name | The function name |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_qualname | The fully qualified function name Added in |
| | version 3.11. |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_argcount | The total number of positional *parameters* |
| | (including positional-only parameters and |
| | parameters with default values) that the function |
| | has |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_posonlyargcount | The number of positional-only *parameters* |
| | (including arguments with default values) that the |
| | function has |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_kwonlyargcount | The number of keyword-only *parameters* (including |
| | arguments with default values) that the function |
| | has |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_nlocals | The number of local variables used by the function |
| | (including parameters) |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_varnames | A "tuple" containing the names of the local |
| | variables in the function (starting with the |
| | parameter names) |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_cellvars | A "tuple" containing the names of local variables |
| | that are referenced from at least one *nested |
| | scope* inside the function |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_freevars | A "tuple" containing the names of *free (closure) |
| | variables* that a *nested scope* references in an |
| | outer scope. See also "function.__closure__". |
| | Note: references to global and builtin names are |
| | *not* included. |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_code | A string representing the sequence of *bytecode* |
| | instructions in the function |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_consts | A "tuple" containing the literals used by the |
| | *bytecode* in the function |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_names | A "tuple" containing the names used by the |
| | *bytecode* in the function |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_filename | The name of the file from which the code was |
| | compiled |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_firstlineno | The line number of the first line of the function |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_lnotab | A string encoding the mapping from *bytecode* |
| | offsets to line numbers. For details, see the |
| | source code of the interpreter. Deprecated since |
| | version 3.12: This attribute of code objects is |
| | deprecated, and may be removed in Python 3.15. |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_stacksize | The required stack size of the code object |
+----------------------------------------------------+----------------------------------------------------+
| codeobject.co_flags | An "integer" encoding a number of flags for the |
| | interpreter. |
+----------------------------------------------------+----------------------------------------------------+
The following flag bits are defined for "co_flags": bit "0x04" is set
if the function uses the "*arguments" syntax to accept an arbitrary
number of positional arguments; bit "0x08" is set if the function uses
the "**keywords" syntax to accept arbitrary keyword arguments; bit
"0x20" is set if the function is a generator. See Code Objects Bit
Flags for details on the semantics of each flags that might be
present.
Future feature declarations (for example, "from __future__ import
division") also use bits in "co_flags" to indicate whether a code
object was compiled with a particular feature enabled. See
"compiler_flag".
Other bits in "co_flags" are reserved for internal use.
If a code object represents a function and has a docstring, the
"CO_HAS_DOCSTRING" bit is set in "co_flags" and the first item in
"co_consts" is the docstring of the function.
Methods on code objects
~~~~~~~~~~~~~~~~~~~~~~~
codeobject.co_positions()
Returns an iterable over the source code positions of each
*bytecode* instruction in the code object.
The iterator returns "tuple"s containing the "(start_line,
end_line, start_column, end_column)". The *i-th* tuple corresponds
to the position of the source code that compiled to the *i-th* code
unit. Column information is 0-indexed utf-8 byte offsets on the
given source line.
This positional information can be missing. A non-exhaustive lists
of cases where this may happen:
* Running the interpreter with "-X" "no_debug_ranges".
* Loading a pyc file compiled while using "-X" "no_debug_ranges".
* Position tuples corresponding to artificial instructions.
* Line and column numbers that can’t be represented due to
implementation specific limitations.
When this occurs, some or all of the tuple elements can be "None".
Added in version 3.11.
Note:
This feature requires storing column positions in code objects
which may result in a small increase of disk usage of compiled
Python files or interpreter memory usage. To avoid storing the
extra information and/or deactivate printing the extra traceback
information, the "-X" "no_debug_ranges" command line flag or the
"PYTHONNODEBUGRANGES" environment variable can be used.
codeobject.co_lines()
Returns an iterator that yields information about successive ranges
of *bytecode*s. Each item yielded is a "(start, end, lineno)"
"tuple":
* "start" (an "int") represents the offset (inclusive) of the start
of the *bytecode* range
* "end" (an "int") represents the offset (exclusive) of the end of
the *bytecode* range
* "lineno" is an "int" representing the line number of the
*bytecode* range, or "None" if the bytecodes in the given range
have no line number
The items yielded will have the following properties:
* The first range yielded will have a "start" of 0.
* The "(start, end)" ranges will be non-decreasing and consecutive.
That is, for any pair of "tuple"s, the "start" of the second will
be equal to the "end" of the first.
* No range will be backwards: "end >= start" for all triples.
* The last "tuple" yielded will have "end" equal to the size of the
*bytecode*.
Zero-width ranges, where "start == end", are allowed. Zero-width
ranges are used for lines that are present in the source code, but
have been eliminated by the *bytecode* compiler.
Added in version 3.10.
See also:
**PEP 626** - Precise line numbers for debugging and other tools.
The PEP that introduced the "co_lines()" method.
codeobject.replace(**kwargs)
Return a copy of the code object with new values for the specified
fields.
Code objects are also supported by the generic function
"copy.replace()".
Added in version 3.8.
Frame objects
-------------
Frame objects represent execution frames. They may occur in traceback
objects, and are also passed to registered trace functions.
Special read-only attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+----------------------------------------------------+----------------------------------------------------+
| frame.f_back | Points to the previous stack frame (towards the |
| | caller), or "None" if this is the bottom stack |
| | frame |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_code | The code object being executed in this frame. |
| | Accessing this attribute raises an auditing event |
| | "object.__getattr__" with arguments "obj" and |
| | ""f_code"". |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_locals | The mapping used by the frame to look up local |
| | variables. If the frame refers to an *optimized |
| | scope*, this may return a write-through proxy |
| | object. Changed in version 3.13: Return a proxy |
| | for optimized scopes. |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_globals | The dictionary used by the frame to look up global |
| | variables |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_builtins | The dictionary used by the frame to look up built- |
| | in (intrinsic) names |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_lasti | The “precise instruction” of the frame object |
| | (this is an index into the *bytecode* string of |
| | the code object) |
+----------------------------------------------------+----------------------------------------------------+
Special writable attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~
+----------------------------------------------------+----------------------------------------------------+
| frame.f_trace | If not "None", this is a function called for |
| | various events during code execution (this is used |
| | by debuggers). Normally an event is triggered for |
| | each new source line (see "f_trace_lines"). |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_trace_lines | Set this attribute to "False" to disable |
| | triggering a tracing event for each source line. |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_trace_opcodes | Set this attribute to "True" to allow per-opcode |
| | events to be requested. Note that this may lead to |
| | undefined interpreter behaviour if exceptions |
| | raised by the trace function escape to the |
| | function being traced. |
+----------------------------------------------------+----------------------------------------------------+
| frame.f_lineno | The current line number of the frame – writing to |
| | this from within a trace function jumps to the |
| | given line (only for the bottom-most frame). A |
| | debugger can implement a Jump command (aka Set |
| | Next Statement) by writing to this attribute. |
+----------------------------------------------------+----------------------------------------------------+
Frame object methods
~~~~~~~~~~~~~~~~~~~~
Frame objects support one method:
frame.clear()
This method clears all references to local variables held by the
frame. Also, if the frame belonged to a *generator*, the generator
is finalized. This helps break reference cycles involving frame
objects (for example when catching an exception and storing its
traceback for later use).
"RuntimeError" is raised if the frame is currently executing or
suspended.
Added in version 3.4.
Changed in version 3.13: Attempting to clear a suspended frame
raises "RuntimeError" (as has always been the case for executing
frames).
Traceback objects
-----------------
Traceback objects represent the stack trace of an exception. A
traceback object is implicitly created when an exception occurs, and
may also be explicitly created by calling "types.TracebackType".
Changed in version 3.7: Traceback objects can now be explicitly
instantiated from Python code.
For implicitly created tracebacks, when the search for an exception
handler unwinds the execution stack, at each unwound level a traceback
object is inserted in front of the current traceback. When an
exception handler is entered, the stack trace is made available to the
program. (See section The try statement.) It is accessible as the
third item of the tuple returned by "sys.exc_info()", and as the
"__traceback__" attribute of the caught exception.
When the program contains no suitable handler, the stack trace is
written (nicely formatted) to the standard error stream; if the
interpreter is interactive, it is also made available to the user as
"sys.last_traceback".
For explicitly created tracebacks, it is up to the creator of the
traceback to determine how the "tb_next" attributes should be linked
to form a full stack trace.
Special read-only attributes:
+----------------------------------------------------+----------------------------------------------------+
| traceback.tb_frame | Points to the execution frame of the current |
| | level. Accessing this attribute raises an |
| | auditing event "object.__getattr__" with arguments |
| | "obj" and ""tb_frame"". |
+----------------------------------------------------+----------------------------------------------------+
| traceback.tb_lineno | Gives the line number where the exception occurred |
+----------------------------------------------------+----------------------------------------------------+
| traceback.tb_lasti | Indicates the “precise instruction”. |
+----------------------------------------------------+----------------------------------------------------+
The line number and last instruction in the traceback may differ from
the line number of its frame object if the exception occurred in a
"try" statement with no matching except clause or with a "finally"
clause.
traceback.tb_next
The special writable attribute "tb_next" is the next level in the
stack trace (towards the frame where the exception occurred), or
"None" if there is no next level.
Changed in version 3.7: This attribute is now writable
Slice objects
-------------
Slice objects are used to represent slices for "__getitem__()"
methods. They are also created by the built-in "slice()" function.
Special read-only attributes: "start" is the lower bound; "stop" is
the upper bound; "step" is the step value; each is "None" if omitted.
These attributes can have any type.
Slice objects support one method:
slice.indices(self, length)
This method takes a single integer argument *length* and computes
information about the slice that the slice object would describe if
applied to a sequence of *length* items. It returns a tuple of
three integers; respectively these are the *start* and *stop*
indices and the *step* or stride length of the slice. Missing or
out-of-bounds indices are handled in a manner consistent with
regular slices.
Static method objects
---------------------
Static method objects provide a way of defeating the transformation of
function objects to method objects described above. A static method
object is a wrapper around any other object, usually a user-defined
method object. When a static method object is retrieved from a class
or a class instance, the object actually returned is the wrapped
object, which is not subject to any further transformation. Static
method objects are also callable. Static method objects are created by
the built-in "staticmethod()" constructor.
Class method objects
--------------------
A class method object, like a static method object, is a wrapper
around another object that alters the way in which that object is
retrieved from classes and class instances. The behaviour of class
method objects upon such retrieval is described above, under “instance
methods”. Class method objects are created by the built-in
"classmethod()" constructor.
''',
'typesfunctions': r'''Functions
*********
Function objects are created by function definitions. The only
operation on a function object is to call it: "func(argument-list)".
There are really two flavors of function objects: built-in functions
and user-defined functions. Both support the same operation (to call
the function), but the implementation is different, hence the
different object types.
See Function definitions for more information.
''',
'typesmapping': r'''Mapping Types — "dict"
**********************
A *mapping* object maps *hashable* values to arbitrary objects.
Mappings are mutable objects. There is currently only one standard
mapping type, the *dictionary*. (For other containers see the built-
in "list", "set", and "tuple" classes, and the "collections" module.)
A dictionary’s keys are *almost* arbitrary values. Values that are
not *hashable*, that is, values containing lists, dictionaries or
other mutable types (that are compared by value rather than by object
identity) may not be used as keys. Values that compare equal (such as
"1", "1.0", and "True") can be used interchangeably to index the same
dictionary entry.
class dict(**kwargs)
class dict(mapping, **kwargs)
class dict(iterable, **kwargs)
Return a new dictionary initialized from an optional positional
argument and a possibly empty set of keyword arguments.
Dictionaries can be created by several means:
* Use a comma-separated list of "key: value" pairs within braces:
"{'jack': 4098, 'sjoerd': 4127}" or "{4098: 'jack', 4127:
'sjoerd'}"
* Use a dict comprehension: "{}", "{x: x ** 2 for x in range(10)}"
* Use the type constructor: "dict()", "dict([('foo', 100), ('bar',
200)])", "dict(foo=100, bar=200)"
If no positional argument is given, an empty dictionary is created.
If a positional argument is given and it defines a "keys()" method,
a dictionary is created by calling "__getitem__()" on the argument
with each returned key from the method. Otherwise, the positional
argument must be an *iterable* object. Each item in the iterable
must itself be an iterable with exactly two elements. The first
element of each item becomes a key in the new dictionary, and the
second element the corresponding value. If a key occurs more than
once, the last value for that key becomes the corresponding value
in the new dictionary.
If keyword arguments are given, the keyword arguments and their
values are added to the dictionary created from the positional
argument. If a key being added is already present, the value from
the keyword argument replaces the value from the positional
argument.
To illustrate, the following examples all return a dictionary equal
to "{"one": 1, "two": 2, "three": 3}":
>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> f = dict({'one': 1, 'three': 3}, two=2)
>>> a == b == c == d == e == f
True
Providing keyword arguments as in the first example only works for
keys that are valid Python identifiers. Otherwise, any valid keys
can be used.
These are the operations that dictionaries support (and therefore,
custom mapping types should support too):
list(d)
Return a list of all the keys used in the dictionary *d*.
len(d)
Return the number of items in the dictionary *d*.
d[key]
Return the item of *d* with key *key*. Raises a "KeyError" if
*key* is not in the map.
If a subclass of dict defines a method "__missing__()" and *key*
is not present, the "d[key]" operation calls that method with
the key *key* as argument. The "d[key]" operation then returns
or raises whatever is returned or raised by the
"__missing__(key)" call. No other operations or methods invoke
"__missing__()". If "__missing__()" is not defined, "KeyError"
is raised. "__missing__()" must be a method; it cannot be an
instance variable:
>>> class Counter(dict):
... def __missing__(self, key):
... return 0
...
>>> c = Counter()
>>> c['red']
0
>>> c['red'] += 1
>>> c['red']
1
The example above shows part of the implementation of
"collections.Counter". A different "__missing__" method is used
by "collections.defaultdict".
d[key] = value
Set "d[key]" to *value*.
del d[key]
Remove "d[key]" from *d*. Raises a "KeyError" if *key* is not
in the map.
key in d
Return "True" if *d* has a key *key*, else "False".
key not in d
Equivalent to "not key in d".
iter(d)
Return an iterator over the keys of the dictionary. This is a
shortcut for "iter(d.keys())".
clear()
Remove all items from the dictionary.
copy()
Return a shallow copy of the dictionary.
classmethod fromkeys(iterable, value=None, /)
Create a new dictionary with keys from *iterable* and values set
to *value*.
"fromkeys()" is a class method that returns a new dictionary.
*value* defaults to "None". All of the values refer to just a
single instance, so it generally doesn’t make sense for *value*
to be a mutable object such as an empty list. To get distinct
values, use a dict comprehension instead.
get(key, default=None, /)
Return the value for *key* if *key* is in the dictionary, else
*default*. If *default* is not given, it defaults to "None", so
that this method never raises a "KeyError".
items()
Return a new view of the dictionary’s items ("(key, value)"
pairs). See the documentation of view objects.
keys()
Return a new view of the dictionary’s keys. See the
documentation of view objects.
pop(key[, default])
If *key* is in the dictionary, remove it and return its value,
else return *default*. If *default* is not given and *key* is
not in the dictionary, a "KeyError" is raised.
popitem()
Remove and return a "(key, value)" pair from the dictionary.
Pairs are returned in LIFO (last-in, first-out) order.
"popitem()" is useful to destructively iterate over a
dictionary, as often used in set algorithms. If the dictionary
is empty, calling "popitem()" raises a "KeyError".
Changed in version 3.7: LIFO order is now guaranteed. In prior
versions, "popitem()" would return an arbitrary key/value pair.
reversed(d)
Return a reverse iterator over the keys of the dictionary. This
is a shortcut for "reversed(d.keys())".
Added in version 3.8.
setdefault(key, default=None, /)
If *key* is in the dictionary, return its value. If not, insert
*key* with a value of *default* and return *default*. *default*
defaults to "None".
update([other])
Update the dictionary with the key/value pairs from *other*,
overwriting existing keys. Return "None".
"update()" accepts either another object with a "keys()" method
(in which case "__getitem__()" is called with every key returned
from the method) or an iterable of key/value pairs (as tuples or
other iterables of length two). If keyword arguments are
specified, the dictionary is then updated with those key/value
pairs: "d.update(red=1, blue=2)".
values()
Return a new view of the dictionary’s values. See the
documentation of view objects.
An equality comparison between one "dict.values()" view and
another will always return "False". This also applies when
comparing "dict.values()" to itself:
>>> d = {'a': 1}
>>> d.values() == d.values()
False
d | other
Create a new dictionary with the merged keys and values of *d*
and *other*, which must both be dictionaries. The values of
*other* take priority when *d* and *other* share keys.
Added in version 3.9.
d |= other
Update the dictionary *d* with keys and values from *other*,
which may be either a *mapping* or an *iterable* of key/value
pairs. The values of *other* take priority when *d* and *other*
share keys.
Added in version 3.9.
Dictionaries compare equal if and only if they have the same "(key,
value)" pairs (regardless of ordering). Order comparisons (‘<’,
‘<=’, ‘>=’, ‘>’) raise "TypeError".
Dictionaries preserve insertion order. Note that updating a key
does not affect the order. Keys added after deletion are inserted
at the end.
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
>>> d
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
>>> list(d)
['one', 'two', 'three', 'four']
>>> list(d.values())
[1, 2, 3, 4]
>>> d["one"] = 42
>>> d
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
>>> del d["two"]
>>> d["two"] = None
>>> d
{'one': 42, 'three': 3, 'four': 4, 'two': None}
Changed in version 3.7: Dictionary order is guaranteed to be
insertion order. This behavior was an implementation detail of
CPython from 3.6.
Dictionaries and dictionary views are reversible.
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
>>> d
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
>>> list(reversed(d))
['four', 'three', 'two', 'one']
>>> list(reversed(d.values()))
[4, 3, 2, 1]
>>> list(reversed(d.items()))
[('four', 4), ('three', 3), ('two', 2), ('one', 1)]
Changed in version 3.8: Dictionaries are now reversible.
See also:
"types.MappingProxyType" can be used to create a read-only view of a
"dict".
Dictionary view objects
=======================
The objects returned by "dict.keys()", "dict.values()" and
"dict.items()" are *view objects*. They provide a dynamic view on the
dictionary’s entries, which means that when the dictionary changes,
the view reflects these changes.
Dictionary views can be iterated over to yield their respective data,
and support membership tests:
len(dictview)
Return the number of entries in the dictionary.
iter(dictview)
Return an iterator over the keys, values or items (represented as
tuples of "(key, value)") in the dictionary.
Keys and values are iterated over in insertion order. This allows
the creation of "(value, key)" pairs using "zip()": "pairs =
zip(d.values(), d.keys())". Another way to create the same list is
"pairs = [(v, k) for (k, v) in d.items()]".
Iterating views while adding or deleting entries in the dictionary
may raise a "RuntimeError" or fail to iterate over all entries.
Changed in version 3.7: Dictionary order is guaranteed to be
insertion order.
x in dictview
Return "True" if *x* is in the underlying dictionary’s keys, values
or items (in the latter case, *x* should be a "(key, value)"
tuple).
reversed(dictview)
Return a reverse iterator over the keys, values or items of the
dictionary. The view will be iterated in reverse order of the
insertion.
Changed in version 3.8: Dictionary views are now reversible.
dictview.mapping
Return a "types.MappingProxyType" that wraps the original
dictionary to which the view refers.
Added in version 3.10.
Keys views are set-like since their entries are unique and *hashable*.
Items views also have set-like operations since the (key, value) pairs
are unique and the keys are hashable. If all values in an items view
are hashable as well, then the items view can interoperate with other
sets. (Values views are not treated as set-like since the entries are
generally not unique.) For set-like views, all of the operations
defined for the abstract base class "collections.abc.Set" are
available (for example, "==", "<", or "^"). While using set
operators, set-like views accept any iterable as the other operand,
unlike sets which only accept sets as the input.
An example of dictionary view usage:
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.keys()
>>> values = dishes.values()
>>> # iteration
>>> n = 0
>>> for val in values:
... n += val
...
>>> print(n)
504
>>> # keys and values are iterated over in the same order (insertion order)
>>> list(keys)
['eggs', 'sausage', 'bacon', 'spam']
>>> list(values)
[2, 1, 1, 500]
>>> # view objects are dynamic and reflect dict changes
>>> del dishes['eggs']
>>> del dishes['sausage']
>>> list(keys)
['bacon', 'spam']
>>> # set operations
>>> keys & {'eggs', 'bacon', 'salad'}
{'bacon'}
>>> keys ^ {'sausage', 'juice'} == {'juice', 'sausage', 'bacon', 'spam'}
True
>>> keys | ['juice', 'juice', 'juice'] == {'bacon', 'spam', 'juice'}
True
>>> # get back a read-only proxy for the original dictionary
>>> values.mapping
mappingproxy({'bacon': 1, 'spam': 500})
>>> values.mapping['spam']
500
''',
'typesmethods': r'''Methods
*******
Methods are functions that are called using the attribute notation.
There are two flavors: built-in methods (such as "append()" on lists)
and class instance method. Built-in methods are described with the
types that support them.
If you access a method (a function defined in a class namespace)
through an instance, you get a special object: a *bound method* (also
called instance method) object. When called, it will add the "self"
argument to the argument list. Bound methods have two special read-
only attributes: "m.__self__" is the object on which the method
operates, and "m.__func__" is the function implementing the method.
Calling "m(arg-1, arg-2, ..., arg-n)" is completely equivalent to
calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)".
Like function objects, bound method objects support getting arbitrary
attributes. However, since method attributes are actually stored on
the underlying function object ("method.__func__"), setting method
attributes on bound methods is disallowed. Attempting to set an
attribute on a method results in an "AttributeError" being raised. In
order to set a method attribute, you need to explicitly set it on the
underlying function object:
>>> class C:
... def method(self):
... pass
...
>>> c = C()
>>> c.method.whoami = 'my name is method' # can't set on the method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method' object has no attribute 'whoami'
>>> c.method.__func__.whoami = 'my name is method'
>>> c.method.whoami
'my name is method'
See Instance methods for more information.
''',
'typesmodules': r'''Modules
*******
The only special operation on a module is attribute access: "m.name",
where *m* is a module and *name* accesses a name defined in *m*’s
symbol table. Module attributes can be assigned to. (Note that the
"import" statement is not, strictly speaking, an operation on a module
object; "import foo" does not require a module object named *foo* to
exist, rather it requires an (external) *definition* for a module
named *foo* somewhere.)
A special attribute of every module is "__dict__". This is the
dictionary containing the module’s symbol table. Modifying this
dictionary will actually change the module’s symbol table, but direct
assignment to the "__dict__" attribute is not possible (you can write
"m.__dict__['a'] = 1", which defines "m.a" to be "1", but you can’t
write "m.__dict__ = {}"). Modifying "__dict__" directly is not
recommended.
Modules built into the interpreter are written like this: "<module
'sys' (built-in)>". If loaded from a file, they are written as
"<module 'os' from '/usr/local/lib/pythonX.Y/os.pyc'>".
''',
'typesseq': r'''Sequence Types — "list", "tuple", "range"
*****************************************
There are three basic sequence types: lists, tuples, and range
objects. Additional sequence types tailored for processing of binary
data and text strings are described in dedicated sections.
Common Sequence Operations
==========================
The operations in the following table are supported by most sequence
types, both mutable and immutable. The "collections.abc.Sequence" ABC
is provided to make it easier to correctly implement these operations
on custom sequence types.
This table lists the sequence operations sorted in ascending priority.
In the table, *s* and *t* are sequences of the same type, *n*, *i*,
*j* and *k* are integers and *x* is an arbitrary object that meets any
type and value restrictions imposed by *s*.
The "in" and "not in" operations have the same priorities as the
comparison operations. The "+" (concatenation) and "*" (repetition)
operations have the same priority as the corresponding numeric
operations. [3]
+----------------------------+----------------------------------+------------+
| Operation | Result | Notes |
|============================|==================================|============|
| "x in s" | "True" if an item of *s* is | (1) |
| | equal to *x*, else "False" | |
+----------------------------+----------------------------------+------------+
| "x not in s" | "False" if an item of *s* is | (1) |
| | equal to *x*, else "True" | |
+----------------------------+----------------------------------+------------+
| "s + t" | the concatenation of *s* and *t* | (6)(7) |
+----------------------------+----------------------------------+------------+
| "s * n" or "n * s" | equivalent to adding *s* to | (2)(7) |
| | itself *n* times | |
+----------------------------+----------------------------------+------------+
| "s[i]" | *i*th item of *s*, origin 0 | (3) |
+----------------------------+----------------------------------+------------+
| "s[i:j]" | slice of *s* from *i* to *j* | (3)(4) |
+----------------------------+----------------------------------+------------+
| "s[i:j:k]" | slice of *s* from *i* to *j* | (3)(5) |
| | with step *k* | |
+----------------------------+----------------------------------+------------+
| "len(s)" | length of *s* | |
+----------------------------+----------------------------------+------------+
| "min(s)" | smallest item of *s* | |
+----------------------------+----------------------------------+------------+
| "max(s)" | largest item of *s* | |
+----------------------------+----------------------------------+------------+
| "s.index(x[, i[, j]])" | index of the first occurrence of | (8) |
| | *x* in *s* (at or after index | |
| | *i* and before index *j*) | |
+----------------------------+----------------------------------+------------+
| "s.count(x)" | total number of occurrences of | |
| | *x* in *s* | |
+----------------------------+----------------------------------+------------+
Sequences of the same type also support comparisons. In particular,
tuples and lists are compared lexicographically by comparing
corresponding elements. This means that to compare equal, every
element must compare equal and the two sequences must be of the same
type and have the same length. (For full details see Comparisons in
the language reference.)
Forward and reversed iterators over mutable sequences access values
using an index. That index will continue to march forward (or
backward) even if the underlying sequence is mutated. The iterator
terminates only when an "IndexError" or a "StopIteration" is
encountered (or when the index drops below zero).
Notes:
1. While the "in" and "not in" operations are used only for simple
containment testing in the general case, some specialised sequences
(such as "str", "bytes" and "bytearray") also use them for
subsequence testing:
>>> "gg" in "eggs"
True
2. Values of *n* less than "0" are treated as "0" (which yields an
empty sequence of the same type as *s*). Note that items in the
sequence *s* are not copied; they are referenced multiple times.
This often haunts new Python programmers; consider:
>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]
What has happened is that "[[]]" is a one-element list containing
an empty list, so all three elements of "[[]] * 3" are references
to this single empty list. Modifying any of the elements of
"lists" modifies this single list. You can create a list of
different lists this way:
>>> lists = [[] for i in range(3)]
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]
Further explanation is available in the FAQ entry How do I create a
multidimensional list?.
3. If *i* or *j* is negative, the index is relative to the end of
sequence *s*: "len(s) + i" or "len(s) + j" is substituted. But
note that "-0" is still "0".
4. The slice of *s* from *i* to *j* is defined as the sequence of
items with index *k* such that "i <= k < j". If *i* or *j* is
greater than "len(s)", use "len(s)". If *i* is omitted or "None",
use "0". If *j* is omitted or "None", use "len(s)". If *i* is
greater than or equal to *j*, the slice is empty.
5. The slice of *s* from *i* to *j* with step *k* is defined as the
sequence of items with index "x = i + n*k" such that "0 <= n <
(j-i)/k". In other words, the indices are "i", "i+k", "i+2*k",
"i+3*k" and so on, stopping when *j* is reached (but never
including *j*). When *k* is positive, *i* and *j* are reduced to
"len(s)" if they are greater. When *k* is negative, *i* and *j* are
reduced to "len(s) - 1" if they are greater. If *i* or *j* are
omitted or "None", they become “end” values (which end depends on
the sign of *k*). Note, *k* cannot be zero. If *k* is "None", it
is treated like "1".
6. Concatenating immutable sequences always results in a new object.
This means that building up a sequence by repeated concatenation
will have a quadratic runtime cost in the total sequence length.
To get a linear runtime cost, you must switch to one of the
alternatives below:
* if concatenating "str" objects, you can build a list and use
"str.join()" at the end or else write to an "io.StringIO"
instance and retrieve its value when complete
* if concatenating "bytes" objects, you can similarly use
"bytes.join()" or "io.BytesIO", or you can do in-place
concatenation with a "bytearray" object. "bytearray" objects are
mutable and have an efficient overallocation mechanism
* if concatenating "tuple" objects, extend a "list" instead
* for other types, investigate the relevant class documentation
7. Some sequence types (such as "range") only support item sequences
that follow specific patterns, and hence don’t support sequence
concatenation or repetition.
8. "index" raises "ValueError" when *x* is not found in *s*. Not all
implementations support passing the additional arguments *i* and
*j*. These arguments allow efficient searching of subsections of
the sequence. Passing the extra arguments is roughly equivalent to
using "s[i:j].index(x)", only without copying any data and with the
returned index being relative to the start of the sequence rather
than the start of the slice.
Immutable Sequence Types
========================
The only operation that immutable sequence types generally implement
that is not also implemented by mutable sequence types is support for
the "hash()" built-in.
This support allows immutable sequences, such as "tuple" instances, to
be used as "dict" keys and stored in "set" and "frozenset" instances.
Attempting to hash an immutable sequence that contains unhashable
values will result in "TypeError".
Mutable Sequence Types
======================
The operations in the following table are defined on mutable sequence
types. The "collections.abc.MutableSequence" ABC is provided to make
it easier to correctly implement these operations on custom sequence
types.
In the table *s* is an instance of a mutable sequence type, *t* is any
iterable object and *x* is an arbitrary object that meets any type and
value restrictions imposed by *s* (for example, "bytearray" only
accepts integers that meet the value restriction "0 <= x <= 255").
+--------------------------------+----------------------------------+-----------------------+
| Operation | Result | Notes |
|================================|==================================|=======================|
| "s[i] = x" | item *i* of *s* is replaced by | |
| | *x* | |
+--------------------------------+----------------------------------+-----------------------+
| "s[i:j] = t" | slice of *s* from *i* to *j* is | |
| | replaced by the contents of the | |
| | iterable *t* | |
+--------------------------------+----------------------------------+-----------------------+
| "del s[i:j]" | same as "s[i:j] = []" | |
+--------------------------------+----------------------------------+-----------------------+
| "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) |
| | replaced by those of *t* | |
+--------------------------------+----------------------------------+-----------------------+
| "del s[i:j:k]" | removes the elements of | |
| | "s[i:j:k]" from the list | |
+--------------------------------+----------------------------------+-----------------------+
| "s.append(x)" | appends *x* to the end of the | |
| | sequence (same as | |
| | "s[len(s):len(s)] = [x]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.clear()" | removes all items from *s* (same | (5) |
| | as "del s[:]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.copy()" | creates a shallow copy of *s* | (5) |
| | (same as "s[:]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.extend(t)" or "s += t" | extends *s* with the contents of | |
| | *t* (for the most part the same | |
| | as "s[len(s):len(s)] = t") | |
+--------------------------------+----------------------------------+-----------------------+
| "s *= n" | updates *s* with its contents | (6) |
| | repeated *n* times | |
+--------------------------------+----------------------------------+-----------------------+
| "s.insert(i, x)" | inserts *x* into *s* at the | |
| | index given by *i* (same as | |
| | "s[i:i] = [x]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.pop()" or "s.pop(i)" | retrieves the item at *i* and | (2) |
| | also removes it from *s* | |
+--------------------------------+----------------------------------+-----------------------+
| "s.remove(x)" | removes the first item from *s* | (3) |
| | where "s[i]" is equal to *x* | |
+--------------------------------+----------------------------------+-----------------------+
| "s.reverse()" | reverses the items of *s* in | (4) |
| | place | |
+--------------------------------+----------------------------------+-----------------------+
Notes:
1. If *k* is not equal to "1", *t* must have the same length as the
slice it is replacing.
2. The optional argument *i* defaults to "-1", so that by default the
last item is removed and returned.
3. "remove()" raises "ValueError" when *x* is not found in *s*.
4. The "reverse()" method modifies the sequence in place for economy
of space when reversing a large sequence. To remind users that it
operates by side effect, it does not return the reversed sequence.
5. "clear()" and "copy()" are included for consistency with the
interfaces of mutable containers that don’t support slicing
operations (such as "dict" and "set"). "copy()" is not part of the
"collections.abc.MutableSequence" ABC, but most concrete mutable
sequence classes provide it.
Added in version 3.3: "clear()" and "copy()" methods.
6. The value *n* is an integer, or an object implementing
"__index__()". Zero and negative values of *n* clear the sequence.
Items in the sequence are not copied; they are referenced multiple
times, as explained for "s * n" under Common Sequence Operations.
Lists
=====
Lists are mutable sequences, typically used to store collections of
homogeneous items (where the precise degree of similarity will vary by
application).
class list([iterable])
Lists may be constructed in several ways:
* Using a pair of square brackets to denote the empty list: "[]"
* Using square brackets, separating items with commas: "[a]", "[a,
b, c]"
* Using a list comprehension: "[x for x in iterable]"
* Using the type constructor: "list()" or "list(iterable)"
The constructor builds a list whose items are the same and in the
same order as *iterable*’s items. *iterable* may be either a
sequence, a container that supports iteration, or an iterator
object. If *iterable* is already a list, a copy is made and
returned, similar to "iterable[:]". For example, "list('abc')"
returns "['a', 'b', 'c']" and "list( (1, 2, 3) )" returns "[1, 2,
3]". If no argument is given, the constructor creates a new empty
list, "[]".
Many other operations also produce lists, including the "sorted()"
built-in.
Lists implement all of the common and mutable sequence operations.
Lists also provide the following additional method:
sort(*, key=None, reverse=False)
This method sorts the list in place, using only "<" comparisons
between items. Exceptions are not suppressed - if any comparison
operations fail, the entire sort operation will fail (and the
list will likely be left in a partially modified state).
"sort()" accepts two arguments that can only be passed by
keyword (keyword-only arguments):
*key* specifies a function of one argument that is used to
extract a comparison key from each list element (for example,
"key=str.lower"). The key corresponding to each item in the list
is calculated once and then used for the entire sorting process.
The default value of "None" means that list items are sorted
directly without calculating a separate key value.
The "functools.cmp_to_key()" utility is available to convert a
2.x style *cmp* function to a *key* function.
*reverse* is a boolean value. If set to "True", then the list
elements are sorted as if each comparison were reversed.
This method modifies the sequence in place for economy of space
when sorting a large sequence. To remind users that it operates
by side effect, it does not return the sorted sequence (use
"sorted()" to explicitly request a new sorted list instance).
The "sort()" method is guaranteed to be stable. A sort is
stable if it guarantees not to change the relative order of
elements that compare equal — this is helpful for sorting in
multiple passes (for example, sort by department, then by salary
grade).
For sorting examples and a brief sorting tutorial, see Sorting
Techniques.
**CPython implementation detail:** While a list is being sorted,
the effect of attempting to mutate, or even inspect, the list is
undefined. The C implementation of Python makes the list appear
empty for the duration, and raises "ValueError" if it can detect
that the list has been mutated during a sort.
Tuples
======
Tuples are immutable sequences, typically used to store collections of
heterogeneous data (such as the 2-tuples produced by the "enumerate()"
built-in). Tuples are also used for cases where an immutable sequence
of homogeneous data is needed (such as allowing storage in a "set" or
"dict" instance).
class tuple([iterable])
Tuples may be constructed in a number of ways:
* Using a pair of parentheses to denote the empty tuple: "()"
* Using a trailing comma for a singleton tuple: "a," or "(a,)"
* Separating items with commas: "a, b, c" or "(a, b, c)"
* Using the "tuple()" built-in: "tuple()" or "tuple(iterable)"
The constructor builds a tuple whose items are the same and in the
same order as *iterable*’s items. *iterable* may be either a
sequence, a container that supports iteration, or an iterator
object. If *iterable* is already a tuple, it is returned
unchanged. For example, "tuple('abc')" returns "('a', 'b', 'c')"
and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument is
given, the constructor creates a new empty tuple, "()".
Note that it is actually the comma which makes a tuple, not the
parentheses. The parentheses are optional, except in the empty
tuple case, or when they are needed to avoid syntactic ambiguity.
For example, "f(a, b, c)" is a function call with three arguments,
while "f((a, b, c))" is a function call with a 3-tuple as the sole
argument.
Tuples implement all of the common sequence operations.
For heterogeneous collections of data where access by name is clearer
than access by index, "collections.namedtuple()" may be a more
appropriate choice than a simple tuple object.
Ranges
======
The "range" type represents an immutable sequence of numbers and is
commonly used for looping a specific number of times in "for" loops.
class range(stop)
class range(start, stop[, step])
The arguments to the range constructor must be integers (either
built-in "int" or any object that implements the "__index__()"
special method). If the *step* argument is omitted, it defaults to
"1". If the *start* argument is omitted, it defaults to "0". If
*step* is zero, "ValueError" is raised.
For a positive *step*, the contents of a range "r" are determined
by the formula "r[i] = start + step*i" where "i >= 0" and "r[i] <
stop".
For a negative *step*, the contents of the range are still
determined by the formula "r[i] = start + step*i", but the
constraints are "i >= 0" and "r[i] > stop".
A range object will be empty if "r[0]" does not meet the value
constraint. Ranges do support negative indices, but these are
interpreted as indexing from the end of the sequence determined by
the positive indices.
Ranges containing absolute values larger than "sys.maxsize" are
permitted but some features (such as "len()") may raise
"OverflowError".
Range examples:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]
Ranges implement all of the common sequence operations except
concatenation and repetition (due to the fact that range objects
can only represent sequences that follow a strict pattern and
repetition and concatenation will usually violate that pattern).
start
The value of the *start* parameter (or "0" if the parameter was
not supplied)
stop
The value of the *stop* parameter
step
The value of the *step* parameter (or "1" if the parameter was
not supplied)
The advantage of the "range" type over a regular "list" or "tuple" is
that a "range" object will always take the same (small) amount of
memory, no matter the size of the range it represents (as it only
stores the "start", "stop" and "step" values, calculating individual
items and subranges as needed).
Range objects implement the "collections.abc.Sequence" ABC, and
provide features such as containment tests, element index lookup,
slicing and support for negative indices (see Sequence Types — list,
tuple, range):
>>> r = range(0, 20, 2)
>>> r
range(0, 20, 2)
>>> 11 in r
False
>>> 10 in r
True
>>> r.index(10)
5
>>> r[5]
10
>>> r[:5]
range(0, 10, 2)
>>> r[-1]
18
Testing range objects for equality with "==" and "!=" compares them as
sequences. That is, two range objects are considered equal if they
represent the same sequence of values. (Note that two range objects
that compare equal might have different "start", "stop" and "step"
attributes, for example "range(0) == range(2, 1, 3)" or "range(0, 3,
2) == range(0, 4, 2)".)
Changed in version 3.2: Implement the Sequence ABC. Support slicing
and negative indices. Test "int" objects for membership in constant
time instead of iterating through all items.
Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range objects
based on the sequence of values they define (instead of comparing
based on object identity).Added the "start", "stop" and "step"
attributes.
See also:
* The linspace recipe shows how to implement a lazy version of range
suitable for floating-point applications.
''',
'typesseq-mutable': r'''Mutable Sequence Types
**********************
The operations in the following table are defined on mutable sequence
types. The "collections.abc.MutableSequence" ABC is provided to make
it easier to correctly implement these operations on custom sequence
types.
In the table *s* is an instance of a mutable sequence type, *t* is any
iterable object and *x* is an arbitrary object that meets any type and
value restrictions imposed by *s* (for example, "bytearray" only
accepts integers that meet the value restriction "0 <= x <= 255").
+--------------------------------+----------------------------------+-----------------------+
| Operation | Result | Notes |
|================================|==================================|=======================|
| "s[i] = x" | item *i* of *s* is replaced by | |
| | *x* | |
+--------------------------------+----------------------------------+-----------------------+
| "s[i:j] = t" | slice of *s* from *i* to *j* is | |
| | replaced by the contents of the | |
| | iterable *t* | |
+--------------------------------+----------------------------------+-----------------------+
| "del s[i:j]" | same as "s[i:j] = []" | |
+--------------------------------+----------------------------------+-----------------------+
| "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) |
| | replaced by those of *t* | |
+--------------------------------+----------------------------------+-----------------------+
| "del s[i:j:k]" | removes the elements of | |
| | "s[i:j:k]" from the list | |
+--------------------------------+----------------------------------+-----------------------+
| "s.append(x)" | appends *x* to the end of the | |
| | sequence (same as | |
| | "s[len(s):len(s)] = [x]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.clear()" | removes all items from *s* (same | (5) |
| | as "del s[:]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.copy()" | creates a shallow copy of *s* | (5) |
| | (same as "s[:]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.extend(t)" or "s += t" | extends *s* with the contents of | |
| | *t* (for the most part the same | |
| | as "s[len(s):len(s)] = t") | |
+--------------------------------+----------------------------------+-----------------------+
| "s *= n" | updates *s* with its contents | (6) |
| | repeated *n* times | |
+--------------------------------+----------------------------------+-----------------------+
| "s.insert(i, x)" | inserts *x* into *s* at the | |
| | index given by *i* (same as | |
| | "s[i:i] = [x]") | |
+--------------------------------+----------------------------------+-----------------------+
| "s.pop()" or "s.pop(i)" | retrieves the item at *i* and | (2) |
| | also removes it from *s* | |
+--------------------------------+----------------------------------+-----------------------+
| "s.remove(x)" | removes the first item from *s* | (3) |
| | where "s[i]" is equal to *x* | |
+--------------------------------+----------------------------------+-----------------------+
| "s.reverse()" | reverses the items of *s* in | (4) |
| | place | |
+--------------------------------+----------------------------------+-----------------------+
Notes:
1. If *k* is not equal to "1", *t* must have the same length as the
slice it is replacing.
2. The optional argument *i* defaults to "-1", so that by default the
last item is removed and returned.
3. "remove()" raises "ValueError" when *x* is not found in *s*.
4. The "reverse()" method modifies the sequence in place for economy
of space when reversing a large sequence. To remind users that it
operates by side effect, it does not return the reversed sequence.
5. "clear()" and "copy()" are included for consistency with the
interfaces of mutable containers that don’t support slicing
operations (such as "dict" and "set"). "copy()" is not part of the
"collections.abc.MutableSequence" ABC, but most concrete mutable
sequence classes provide it.
Added in version 3.3: "clear()" and "copy()" methods.
6. The value *n* is an integer, or an object implementing
"__index__()". Zero and negative values of *n* clear the sequence.
Items in the sequence are not copied; they are referenced multiple
times, as explained for "s * n" under Common Sequence Operations.
''',
'unary': r'''Unary arithmetic and bitwise operations
***************************************
All unary arithmetic and bitwise operations have the same priority:
u_expr: power | "-" u_expr | "+" u_expr | "~" u_expr
The unary "-" (minus) operator yields the negation of its numeric
argument; the operation can be overridden with the "__neg__()" special
method.
The unary "+" (plus) operator yields its numeric argument unchanged;
the operation can be overridden with the "__pos__()" special method.
The unary "~" (invert) operator yields the bitwise inversion of its
integer argument. The bitwise inversion of "x" is defined as
"-(x+1)". It only applies to integral numbers or to custom objects
that override the "__invert__()" special method.
In all three cases, if the argument does not have the proper type, a
"TypeError" exception is raised.
''',
'while': r'''The "while" statement
*********************
The "while" statement is used for repeated execution as long as an
expression is true:
while_stmt: "while" assignment_expression ":" suite
["else" ":" suite]
This repeatedly tests the expression and, if it is true, executes the
first suite; if the expression is false (which may be the first time
it is tested) the suite of the "else" clause, if present, is executed
and the loop terminates.
A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite. A "continue" statement
executed in the first suite skips the rest of the suite and goes back
to testing the expression.
''',
'with': r'''The "with" statement
********************
The "with" statement is used to wrap the execution of a block with
methods defined by a context manager (see section With Statement
Context Managers). This allows common "try"…"except"…"finally" usage
patterns to be encapsulated for convenient reuse.
with_stmt: "with" ( "(" with_stmt_contents ","? ")" | with_stmt_contents ) ":" suite
with_stmt_contents: with_item ("," with_item)*
with_item: expression ["as" target]
The execution of the "with" statement with one “item” proceeds as
follows:
1. The context expression (the expression given in the "with_item") is
evaluated to obtain a context manager.
2. The context manager’s "__enter__()" is loaded for later use.
3. The context manager’s "__exit__()" is loaded for later use.
4. The context manager’s "__enter__()" method is invoked.
5. If a target was included in the "with" statement, the return value
from "__enter__()" is assigned to it.
Note:
The "with" statement guarantees that if the "__enter__()" method
returns without an error, then "__exit__()" will always be
called. Thus, if an error occurs during the assignment to the
target list, it will be treated the same as an error occurring
within the suite would be. See step 7 below.
6. The suite is executed.
7. The context manager’s "__exit__()" method is invoked. If an
exception caused the suite to be exited, its type, value, and
traceback are passed as arguments to "__exit__()". Otherwise, three
"None" arguments are supplied.
If the suite was exited due to an exception, and the return value
from the "__exit__()" method was false, the exception is reraised.
If the return value was true, the exception is suppressed, and
execution continues with the statement following the "with"
statement.
If the suite was exited for any reason other than an exception, the
return value from "__exit__()" is ignored, and execution proceeds
at the normal location for the kind of exit that was taken.
The following code:
with EXPRESSION as TARGET:
SUITE
is semantically equivalent to:
manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager)
hit_except = False
try:
TARGET = value
SUITE
except:
hit_except = True
if not exit(manager, *sys.exc_info()):
raise
finally:
if not hit_except:
exit(manager, None, None, None)
With more than one item, the context managers are processed as if
multiple "with" statements were nested:
with A() as a, B() as b:
SUITE
is semantically equivalent to:
with A() as a:
with B() as b:
SUITE
You can also write multi-item context managers in multiple lines if
the items are surrounded by parentheses. For example:
with (
A() as a,
B() as b,
):
SUITE
Changed in version 3.1: Support for multiple context expressions.
Changed in version 3.10: Support for using grouping parentheses to
break the statement in multiple lines.
See also:
**PEP 343** - The “with” statement
The specification, background, and examples for the Python "with"
statement.
''',
'yield': r'''The "yield" statement
*********************
yield_stmt: yield_expression
A "yield" statement is semantically equivalent to a yield expression.
The "yield" statement can be used to omit the parentheses that would
otherwise be required in the equivalent yield expression statement.
For example, the yield statements
yield <expr>
yield from <expr>
are equivalent to the yield expression statements
(yield <expr>)
(yield from <expr>)
Yield expressions and statements are only used when defining a
*generator* function, and are only used in the body of the generator
function. Using "yield" in a function definition is sufficient to
cause that definition to create a generator function instead of a
normal function.
For full details of "yield" semantics, refer to the Yield expressions
section.
''',
}
|