forked from python/python-docs-tr
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3.10.html
More file actions
2535 lines (2486 loc) · 302 KB
/
3.10.html
File metadata and controls
2535 lines (2486 loc) · 302 KB
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
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />
<meta property="og:title" content="What’s New In Python 3.10" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/whatsnew/3.10.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="Editor, Pablo Galindo Salgado,. This article explains the new features in Python 3.10, compared to 3.9. Python 3.10 was released on October 4, 2021. For full details, see the changelog. Summary – R..." />
<meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="Editor, Pablo Galindo Salgado,. This article explains the new features in Python 3.10, compared to 3.9. Python 3.10 was released on October 4, 2021. For full details, see the changelog. Summary – R..." />
<meta property="og:image:width" content="200" />
<meta property="og:image:height" content="200" />
<meta name="theme-color" content="#3776ab" />
<title>What’s New In Python 3.10 — Python 3.11.5 belgelendirmesi</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Python 3.11.5 belgelendirmesi içinde ara"
href="../_static/opensearch.xml"/>
<link rel="author" title="Bu belgeler hakkında" href="../about.html" />
<link rel="index" title="Dizin" href="../genindex.html" />
<link rel="search" title="Ara" href="../search.html" />
<link rel="copyright" title="Telif Hakkı" href="../copyright.html" />
<link rel="next" title="Python 3.9’daki Yenilikler" href="3.9.html" />
<link rel="prev" title="What’s New In Python 3.11" href="3.11.html" />
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.10.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
<input placeholder="Hızlı Arama" aria-label="Hızlı Arama" type="search" name="q" />
<input type="submit" value="Git"/>
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">İçindekiler</a></h3>
<ul>
<li><a class="reference internal" href="#">What’s New In Python 3.10</a><ul>
<li><a class="reference internal" href="#summary-release-highlights">Summary – Release highlights</a></li>
<li><a class="reference internal" href="#new-features">New Features</a><ul>
<li><a class="reference internal" href="#parenthesized-context-managers">Parenthesized context managers</a></li>
<li><a class="reference internal" href="#better-error-messages">Better error messages</a><ul>
<li><a class="reference internal" href="#syntaxerrors">SyntaxErrors</a></li>
<li><a class="reference internal" href="#indentationerrors">IndentationErrors</a></li>
<li><a class="reference internal" href="#attributeerrors">AttributeErrors</a></li>
<li><a class="reference internal" href="#nameerrors">NameErrors</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-626-precise-line-numbers-for-debugging-and-other-tools">PEP 626: Precise line numbers for debugging and other tools</a></li>
<li><a class="reference internal" href="#pep-634-structural-pattern-matching">PEP 634: Structural Pattern Matching</a><ul>
<li><a class="reference internal" href="#syntax-and-operations">Syntax and operations</a></li>
<li><a class="reference internal" href="#declarative-approach">Declarative approach</a></li>
<li><a class="reference internal" href="#simple-pattern-match-to-a-literal">Simple pattern: match to a literal</a><ul>
<li><a class="reference internal" href="#behavior-without-the-wildcard">Behavior without the wildcard</a></li>
</ul>
</li>
<li><a class="reference internal" href="#patterns-with-a-literal-and-variable">Patterns with a literal and variable</a></li>
<li><a class="reference internal" href="#patterns-and-classes">Patterns and classes</a><ul>
<li><a class="reference internal" href="#patterns-with-positional-parameters">Patterns with positional parameters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#nested-patterns">Nested patterns</a></li>
<li><a class="reference internal" href="#complex-patterns-and-the-wildcard">Complex patterns and the wildcard</a></li>
<li><a class="reference internal" href="#guard">Guard</a></li>
<li><a class="reference internal" href="#other-key-features">Other Key Features</a></li>
</ul>
</li>
<li><a class="reference internal" href="#optional-encodingwarning-and-encoding-locale-option">Optional <code class="docutils literal notranslate"><span class="pre">EncodingWarning</span></code> and <code class="docutils literal notranslate"><span class="pre">encoding="locale"</span></code> option</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-features-related-to-type-hints">New Features Related to Type Hints</a><ul>
<li><a class="reference internal" href="#pep-604-new-type-union-operator">PEP 604: New Type Union Operator</a></li>
<li><a class="reference internal" href="#pep-612-parameter-specification-variables">PEP 612: Parameter Specification Variables</a></li>
<li><a class="reference internal" href="#pep-613-typealias">PEP 613: TypeAlias</a></li>
<li><a class="reference internal" href="#pep-647-user-defined-type-guards">PEP 647: User-Defined Type Guards</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a></li>
<li><a class="reference internal" href="#new-modules">New Modules</a></li>
<li><a class="reference internal" href="#improved-modules">Improved Modules</a><ul>
<li><a class="reference internal" href="#asyncio">asyncio</a></li>
<li><a class="reference internal" href="#argparse">argparse</a></li>
<li><a class="reference internal" href="#array">array</a></li>
<li><a class="reference internal" href="#asynchat-asyncore-smtpd">asynchat, asyncore, smtpd</a></li>
<li><a class="reference internal" href="#base64">base64</a></li>
<li><a class="reference internal" href="#bdb">bdb</a></li>
<li><a class="reference internal" href="#bisect">bisect</a></li>
<li><a class="reference internal" href="#codecs">codecs</a></li>
<li><a class="reference internal" href="#collections-abc">collections.abc</a></li>
<li><a class="reference internal" href="#contextlib">contextlib</a></li>
<li><a class="reference internal" href="#curses">curses</a></li>
<li><a class="reference internal" href="#dataclasses">dataclasses</a><ul>
<li><a class="reference internal" href="#slots">__slots__</a></li>
<li><a class="reference internal" href="#keyword-only-fields">Keyword-only fields</a></li>
</ul>
</li>
<li><a class="reference internal" href="#distutils">distutils</a></li>
<li><a class="reference internal" href="#doctest">doctest</a></li>
<li><a class="reference internal" href="#encodings">encodings</a></li>
<li><a class="reference internal" href="#enum">enum</a></li>
<li><a class="reference internal" href="#fileinput">fileinput</a></li>
<li><a class="reference internal" href="#faulthandler">faulthandler</a></li>
<li><a class="reference internal" href="#gc">gc</a></li>
<li><a class="reference internal" href="#glob">glob</a></li>
<li><a class="reference internal" href="#hashlib">hashlib</a></li>
<li><a class="reference internal" href="#hmac">hmac</a></li>
<li><a class="reference internal" href="#idle-and-idlelib">IDLE and idlelib</a></li>
<li><a class="reference internal" href="#importlib-metadata">importlib.metadata</a></li>
<li><a class="reference internal" href="#inspect">inspect</a></li>
<li><a class="reference internal" href="#itertools">itertools</a></li>
<li><a class="reference internal" href="#linecache">linecache</a></li>
<li><a class="reference internal" href="#os">os</a></li>
<li><a class="reference internal" href="#os-path">os.path</a></li>
<li><a class="reference internal" href="#pathlib">pathlib</a></li>
<li><a class="reference internal" href="#platform">platform</a></li>
<li><a class="reference internal" href="#pprint">pprint</a></li>
<li><a class="reference internal" href="#py-compile">py_compile</a></li>
<li><a class="reference internal" href="#pyclbr">pyclbr</a></li>
<li><a class="reference internal" href="#shelve">shelve</a></li>
<li><a class="reference internal" href="#statistics">statistics</a></li>
<li><a class="reference internal" href="#site">site</a></li>
<li><a class="reference internal" href="#socket">socket</a></li>
<li><a class="reference internal" href="#ssl">ssl</a></li>
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
<li><a class="reference internal" href="#sys">sys</a></li>
<li><a class="reference internal" href="#thread">_thread</a></li>
<li><a class="reference internal" href="#threading">threading</a></li>
<li><a class="reference internal" href="#traceback">traceback</a></li>
<li><a class="reference internal" href="#types">types</a></li>
<li><a class="reference internal" href="#typing">typing</a></li>
<li><a class="reference internal" href="#unittest">unittest</a></li>
<li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
<li><a class="reference internal" href="#xml">xml</a></li>
<li><a class="reference internal" href="#zipimport">zipimport</a></li>
</ul>
</li>
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
<li><a class="reference internal" href="#deprecated">Deprecated</a></li>
<li><a class="reference internal" href="#removed">Removed</a></li>
<li><a class="reference internal" href="#porting-to-python-3-10">Porting to Python 3.10</a><ul>
<li><a class="reference internal" href="#changes-in-the-python-syntax">Changes in the Python syntax</a></li>
<li><a class="reference internal" href="#changes-in-the-python-api">Changes in the Python API</a></li>
<li><a class="reference internal" href="#changes-in-the-c-api">Changes in the C API</a></li>
</ul>
</li>
<li><a class="reference internal" href="#cpython-bytecode-changes">CPython bytecode changes</a></li>
<li><a class="reference internal" href="#build-changes">Build Changes</a></li>
<li><a class="reference internal" href="#c-api-changes">C API Changes</a><ul>
<li><a class="reference internal" href="#pep-652-maintaining-the-stable-abi">PEP 652: Maintaining the Stable ABI</a></li>
<li><a class="reference internal" href="#id1">New Features</a></li>
<li><a class="reference internal" href="#id2">Porting to Python 3.10</a></li>
<li><a class="reference internal" href="#id3">Deprecated</a></li>
<li><a class="reference internal" href="#id4">Removed</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Önceki konu</h4>
<p class="topless"><a href="3.11.html"
title="önceki bölüm">What’s New In Python 3.11</a></p>
</div>
<div>
<h4>Sonraki konu</h4>
<p class="topless"><a href="3.9.html"
title="sonraki bölüm">Python 3.9’daki Yenilikler</a></p>
</div>
<div role="note" aria-label="source link">
<h3>Bu Sayfa</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Hata Bildir</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.11/Doc/whatsnew/3.10.rst"
rel="nofollow">Kaynağı Göster
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Gezinti</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="Genel Endeks"
accesskey="I">dizin</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Modül Dizini"
>modülleri</a> |</li>
<li class="right" >
<a href="3.9.html" title="Python 3.9’daki Yenilikler"
accesskey="N">sonraki</a> |</li>
<li class="right" >
<a href="3.11.html" title="What’s New In Python 3.11"
accesskey="P">önceki</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.11.5 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> »</li>
<li class="nav-item nav-item-this"><a href="">What’s New In Python 3.10</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Hızlı Arama" aria-label="Hızlı Arama" type="search" name="q" id="search-box" />
<input type="submit" value="Git" />
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="what-s-new-in-python-3-10">
<h1>What’s New In Python 3.10<a class="headerlink" href="#what-s-new-in-python-3-10" title="Permalink to this heading">¶</a></h1>
<dl class="field-list simple">
<dt class="field-odd">Editor<span class="colon">:</span></dt>
<dd class="field-odd"><p>Pablo Galindo Salgado</p>
</dd>
</dl>
<p>This article explains the new features in Python 3.10, compared to 3.9.
Python 3.10 was released on October 4, 2021.
For full details, see the <a class="reference internal" href="changelog.html#changelog"><span class="std std-ref">changelog</span></a>.</p>
<section id="summary-release-highlights">
<h2>Summary – Release highlights<a class="headerlink" href="#summary-release-highlights" title="Permalink to this heading">¶</a></h2>
<p>New syntax features:</p>
<ul class="simple">
<li><p><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0634/"><strong>PEP 634</strong></a>, Structural Pattern Matching: Specification</p></li>
<li><p><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0635/"><strong>PEP 635</strong></a>, Structural Pattern Matching: Motivation and Rationale</p></li>
<li><p><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0636/"><strong>PEP 636</strong></a>, Structural Pattern Matching: Tutorial</p></li>
<li><p><a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=12782">bpo-12782</a>, Parenthesized context managers are now officially allowed.</p></li>
</ul>
<p>New features in the standard library:</p>
<ul class="simple">
<li><p><span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0618/"><strong>PEP 618</strong></a>, Add Optional Length-Checking To zip.</p></li>
</ul>
<p>Interpreter improvements:</p>
<ul class="simple">
<li><p><span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0626/"><strong>PEP 626</strong></a>, Precise line numbers for debugging and other tools.</p></li>
</ul>
<p>New typing features:</p>
<ul class="simple">
<li><p><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0604/"><strong>PEP 604</strong></a>, Allow writing union types as X | Y</p></li>
<li><p><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0612/"><strong>PEP 612</strong></a>, Parameter Specification Variables</p></li>
<li><p><span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0613/"><strong>PEP 613</strong></a>, Explicit Type Aliases</p></li>
<li><p><span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0647/"><strong>PEP 647</strong></a>, User-Defined Type Guards</p></li>
</ul>
<p>Important deprecations, removals or restrictions:</p>
<ul class="simple">
<li><p><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0644/"><strong>PEP 644</strong></a>, Require OpenSSL 1.1.1 or newer</p></li>
<li><p><span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0632/"><strong>PEP 632</strong></a>, Deprecate distutils module.</p></li>
<li><p><span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0623/"><strong>PEP 623</strong></a>, Deprecate and prepare for the removal of the wstr member in PyUnicodeObject.</p></li>
<li><p><span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0624/"><strong>PEP 624</strong></a>, Remove Py_UNICODE encoder APIs</p></li>
<li><p><span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-0597/"><strong>PEP 597</strong></a>, Add optional EncodingWarning</p></li>
</ul>
</section>
<section id="new-features">
<h2>New Features<a class="headerlink" href="#new-features" title="Permalink to this heading">¶</a></h2>
<section id="parenthesized-context-managers">
<span id="whatsnew310-pep563"></span><h3>Parenthesized context managers<a class="headerlink" href="#parenthesized-context-managers" title="Permalink to this heading">¶</a></h3>
<p>Using enclosing parentheses for continuation across multiple lines
in context managers is now supported. This allows formatting a long
collection of context managers in multiple lines in a similar way
as it was previously possible with import statements. For instance,
all these examples are now valid:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="p">(</span><span class="n">CtxManager</span><span class="p">()</span> <span class="k">as</span> <span class="n">example</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">with</span> <span class="p">(</span>
<span class="n">CtxManager1</span><span class="p">(),</span>
<span class="n">CtxManager2</span><span class="p">()</span>
<span class="p">):</span>
<span class="o">...</span>
<span class="k">with</span> <span class="p">(</span><span class="n">CtxManager1</span><span class="p">()</span> <span class="k">as</span> <span class="n">example</span><span class="p">,</span>
<span class="n">CtxManager2</span><span class="p">()):</span>
<span class="o">...</span>
<span class="k">with</span> <span class="p">(</span><span class="n">CtxManager1</span><span class="p">(),</span>
<span class="n">CtxManager2</span><span class="p">()</span> <span class="k">as</span> <span class="n">example</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">with</span> <span class="p">(</span>
<span class="n">CtxManager1</span><span class="p">()</span> <span class="k">as</span> <span class="n">example1</span><span class="p">,</span>
<span class="n">CtxManager2</span><span class="p">()</span> <span class="k">as</span> <span class="n">example2</span>
<span class="p">):</span>
<span class="o">...</span>
</pre></div>
</div>
<p>it is also possible to use a trailing comma at the end of the
enclosed group:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="p">(</span>
<span class="n">CtxManager1</span><span class="p">()</span> <span class="k">as</span> <span class="n">example1</span><span class="p">,</span>
<span class="n">CtxManager2</span><span class="p">()</span> <span class="k">as</span> <span class="n">example2</span><span class="p">,</span>
<span class="n">CtxManager3</span><span class="p">()</span> <span class="k">as</span> <span class="n">example3</span><span class="p">,</span>
<span class="p">):</span>
<span class="o">...</span>
</pre></div>
</div>
<p>This new syntax uses the non LL(1) capacities of the new parser.
Check <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0617/"><strong>PEP 617</strong></a> for more details.</p>
<p>(Contributed by Guido van Rossum, Pablo Galindo and Lysandros Nikolaou
in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=12782">bpo-12782</a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40334">bpo-40334</a>.)</p>
</section>
<section id="better-error-messages">
<h3>Better error messages<a class="headerlink" href="#better-error-messages" title="Permalink to this heading">¶</a></h3>
<section id="syntaxerrors">
<h4>SyntaxErrors<a class="headerlink" href="#syntaxerrors" title="Permalink to this heading">¶</a></h4>
<p>When parsing code that contains unclosed parentheses or brackets the interpreter
now includes the location of the unclosed bracket of parentheses instead of displaying
<em>SyntaxError: unexpected EOF while parsing</em> or pointing to some incorrect location.
For instance, consider the following code (notice the unclosed ‘{‘):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">expected</span> <span class="o">=</span> <span class="p">{</span><span class="mi">9</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">18</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">19</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">27</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">28</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">29</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">36</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">37</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
<span class="mi">38</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">39</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">45</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">46</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">47</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">48</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">49</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">54</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
<span class="n">some_other_code</span> <span class="o">=</span> <span class="n">foo</span><span class="p">()</span>
</pre></div>
</div>
<p>Previous versions of the interpreter reported confusing places as the location of
the syntax error:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="s2">"example.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">3</span>
<span class="n">some_other_code</span> <span class="o">=</span> <span class="n">foo</span><span class="p">()</span>
<span class="o">^</span>
<span class="ne">SyntaxError</span><span class="p">:</span> <span class="n">invalid</span> <span class="n">syntax</span>
</pre></div>
</div>
<p>but in Python 3.10 a more informative error is emitted:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="s2">"example.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span>
<span class="n">expected</span> <span class="o">=</span> <span class="p">{</span><span class="mi">9</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">18</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">19</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">27</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">28</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">29</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">36</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">37</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
<span class="o">^</span>
<span class="ne">SyntaxError</span><span class="p">:</span> <span class="s1">'{'</span> <span class="n">was</span> <span class="n">never</span> <span class="n">closed</span>
</pre></div>
</div>
<p>In a similar way, errors involving unclosed string literals (single and triple
quoted) now point to the start of the string instead of reporting EOF/EOL.</p>
<p>These improvements are inspired by previous work in the PyPy interpreter.</p>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42864">bpo-42864</a> and Batuhan Taskaya in
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40176">bpo-40176</a>.)</p>
<p><a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> exceptions raised by the interpreter will now highlight the
full error range of the expression that constitutes the syntax error itself,
instead of just where the problem is detected. In this way, instead of displaying
(before Python 3.10):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">Generator expression must be parenthesized</span>
</pre></div>
</div>
<p>now Python 3.10 will display the exception as:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
<span class="w"> </span><span class="pm">^^^^^^^^^^^^^^^^^^^^</span>
<span class="gr">SyntaxError</span>: <span class="n">Generator expression must be parenthesized</span>
</pre></div>
</div>
<p>This improvement was contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43914">bpo-43914</a>.</p>
<p>A considerable amount of new specialized messages for <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> exceptions
have been incorporated. Some of the most notable ones are as follows:</p>
<ul>
<li><p>Missing <code class="docutils literal notranslate"><span class="pre">:</span></code> before blocks:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">></span> <span class="n">event_horizon</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">></span> <span class="n">event_horizon</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">expected ':'</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42997">bpo-42997</a>.)</p>
</div></blockquote>
</li>
<li><p>Unparenthesised tuples in comprehensions targets:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">{</span><span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="s1">'abcd'</span><span class="p">,</span> <span class="s1">'1234'</span><span class="p">)}</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="p">{</span><span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="s1">'abcd'</span><span class="p">,</span> <span class="s1">'1234'</span><span class="p">)}</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">did you forget parentheses around the comprehension target?</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43017">bpo-43017</a>.)</p>
</div></blockquote>
</li>
<li><p>Missing commas in collection literals and between expressions:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">items</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span><span class="n">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="gp">... </span><span class="n">y</span><span class="p">:</span> <span class="mi">2</span>
<span class="gp">... </span><span class="n">z</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
<span class="w"> </span><span class="n">y</span><span class="p">:</span> <span class="mi">2</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">invalid syntax. Perhaps you forgot a comma?</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43822">bpo-43822</a>.)</p>
</div></blockquote>
</li>
<li><p>Multiple Exception types without parentheses:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">build_dyson_sphere</span><span class="p">()</span>
<span class="gp">... </span><span class="k">except</span> <span class="n">NotEnoughScienceError</span><span class="p">,</span> <span class="n">NotEnoughResourcesError</span><span class="p">:</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
<span class="w"> </span><span class="k">except</span> <span class="n">NotEnoughScienceError</span><span class="p">,</span> <span class="n">NotEnoughResourcesError</span><span class="p">:</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">multiple exception types must be parenthesized</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43149">bpo-43149</a>.)</p>
</div></blockquote>
</li>
<li><p>Missing <code class="docutils literal notranslate"><span class="pre">:</span></code> and values in dictionary literals:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span><span class="n">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="gp">... </span><span class="n">y</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="gp">... </span><span class="n">z</span><span class="p">:</span>
<span class="gp">... </span><span class="p">}</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">4</span>
<span class="w"> </span><span class="n">z</span><span class="p">:</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">expression expected after dictionary key and ':'</span>
<span class="gp">>>> </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span> <span class="n">w</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span> <span class="n">w</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">':' expected after dictionary key</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43823">bpo-43823</a>.)</p>
</div></blockquote>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">try</span></code> blocks without <code class="docutils literal notranslate"><span class="pre">except</span></code> or <code class="docutils literal notranslate"><span class="pre">finally</span></code> blocks:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span><span class="n">something</span> <span class="o">=</span> <span class="mi">3</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
<span class="w"> </span><span class="n">something</span> <span class="o">=</span> <span class="mi">3</span>
<span class="w"> </span><span class="pm">^^^^^^^^^</span>
<span class="gr">SyntaxError</span>: <span class="n">expected 'except' or 'finally' block</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44305">bpo-44305</a>.)</p>
</div></blockquote>
</li>
<li><p>Usage of <code class="docutils literal notranslate"><span class="pre">=</span></code> instead of <code class="docutils literal notranslate"><span class="pre">==</span></code> in comparisons:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">event_horizon</span><span class="p">:</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">event_horizon</span><span class="p">:</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">cannot assign to attribute here. Maybe you meant '==' instead of '='?</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43797">bpo-43797</a>.)</p>
</div></blockquote>
</li>
<li><p>Usage of <code class="docutils literal notranslate"><span class="pre">*</span></code> in f-strings:</p>
<blockquote>
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">f</span><span class="s2">"Black holes </span><span class="si">{</span><span class="o">*</span><span class="n">all_black_holes</span><span class="si">}</span><span class="s2"> and revelations"</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">all_black_holes</span><span class="p">)</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">SyntaxError</span>: <span class="n">f-string: cannot use starred expression here</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41064">bpo-41064</a>.)</p>
</div></blockquote>
</li>
</ul>
</section>
<section id="indentationerrors">
<h4>IndentationErrors<a class="headerlink" href="#indentationerrors" title="Permalink to this heading">¶</a></h4>
<p>Many <a class="reference internal" href="../library/exceptions.html#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a> exceptions now have more context regarding what kind of block
was expecting an indentation, including the location of the statement:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">lel</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
<span class="w"> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="w"> </span><span class="pm">^</span>
<span class="gr">IndentationError</span>: <span class="n">expected an indented block after 'if' statement in line 2</span>
</pre></div>
</div>
</section>
<section id="attributeerrors">
<h4>AttributeErrors<a class="headerlink" href="#attributeerrors" title="Permalink to this heading">¶</a></h4>
<p>When printing <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code> will offer
suggestions of similar attribute names in the object that the exception was
raised from:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">collections</span><span class="o">.</span><span class="n">namedtoplo</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">AttributeError</span>: <span class="n">module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38530">bpo-38530</a>.)</p>
<blockquote>
<div><div class="admonition warning">
<p class="admonition-title">Uyarı</p>
<p>Notice this won’t work if <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code> is not called to display the error
which can happen if some other custom error display function is used. This is a common
scenario in some REPLs like IPython.</p>
</div>
</div></blockquote>
</section>
<section id="nameerrors">
<h4>NameErrors<a class="headerlink" href="#nameerrors" title="Permalink to this heading">¶</a></h4>
<p>When printing <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> raised by the interpreter, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code>
will offer suggestions of similar variable names in the function that the exception
was raised from:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">schwarzschild_black_hole</span> <span class="o">=</span> <span class="kc">None</span>
<span class="gp">>>> </span><span class="n">schwarschild_black_hole</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">NameError</span>: <span class="n">name 'schwarschild_black_hole' is not defined. Did you mean: schwarzschild_black_hole?</span>
</pre></div>
</div>
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38530">bpo-38530</a>.)</p>
<blockquote>
<div><div class="admonition warning">
<p class="admonition-title">Uyarı</p>
<p>Notice this won’t work if <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code> is not called to display the error,
which can happen if some other custom error display function is used. This is a common
scenario in some REPLs like IPython.</p>
</div>
</div></blockquote>
</section>
</section>
<section id="pep-626-precise-line-numbers-for-debugging-and-other-tools">
<h3>PEP 626: Precise line numbers for debugging and other tools<a class="headerlink" href="#pep-626-precise-line-numbers-for-debugging-and-other-tools" title="Permalink to this heading">¶</a></h3>
<p>PEP 626 brings more precise and reliable line numbers for debugging, profiling and coverage tools.
Tracing events, with the correct line number, are generated for all lines of code executed and only for lines of code that are executed.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">f_lineno</span></code> attribute of frame objects will always contain the expected line number.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">co_lnotab</span></code> attribute of code objects is deprecated and will be removed in 3.12.
Code that needs to convert from offset to line number should use the new <code class="docutils literal notranslate"><span class="pre">co_lines()</span></code> method instead.</p>
</section>
<section id="pep-634-structural-pattern-matching">
<h3>PEP 634: Structural Pattern Matching<a class="headerlink" href="#pep-634-structural-pattern-matching" title="Permalink to this heading">¶</a></h3>
<p>Structural pattern matching has been added in the form of a <em>match statement</em>
and <em>case statements</em> of patterns with associated actions. Patterns
consist of sequences, mappings, primitive data types as well as class instances.
Pattern matching enables programs to extract information from complex data types,
branch on the structure of data, and apply specific actions based on different
forms of data.</p>
<section id="syntax-and-operations">
<h4>Syntax and operations<a class="headerlink" href="#syntax-and-operations" title="Permalink to this heading">¶</a></h4>
<p>The generic syntax of pattern matching is:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">subject</span><span class="p">:</span>
<span class="k">case</span> <span class="o"><</span><span class="n">pattern_1</span><span class="o">></span><span class="p">:</span>
<span class="o"><</span><span class="n">action_1</span><span class="o">></span>
<span class="k">case</span> <span class="o"><</span><span class="n">pattern_2</span><span class="o">></span><span class="p">:</span>
<span class="o"><</span><span class="n">action_2</span><span class="o">></span>
<span class="k">case</span> <span class="o"><</span><span class="n">pattern_3</span><span class="o">></span><span class="p">:</span>
<span class="o"><</span><span class="n">action_3</span><span class="o">></span>
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
<span class="o"><</span><span class="n">action_wildcard</span><span class="o">></span>
</pre></div>
</div>
<p>A match statement takes an expression and compares its value to successive
patterns given as one or more case blocks. Specifically, pattern matching
operates by:</p>
<blockquote>
<div><ol class="arabic simple">
<li><p>using data with type and shape (the <code class="docutils literal notranslate"><span class="pre">subject</span></code>)</p></li>
<li><p>evaluating the <code class="docutils literal notranslate"><span class="pre">subject</span></code> in the <code class="docutils literal notranslate"><span class="pre">match</span></code> statement</p></li>
<li><p>comparing the subject with each pattern in a <code class="docutils literal notranslate"><span class="pre">case</span></code> statement
from top to bottom until a match is confirmed.</p></li>
<li><p>executing the action associated with the pattern of the confirmed
match</p></li>
<li><p>If an exact match is not confirmed, the last case, a wildcard <code class="docutils literal notranslate"><span class="pre">_</span></code>,
if provided, will be used as the matching case. If an exact match is
not confirmed and a wildcard case does not exist, the entire match
block is a no-op.</p></li>
</ol>
</div></blockquote>
</section>
<section id="declarative-approach">
<h4>Declarative approach<a class="headerlink" href="#declarative-approach" title="Permalink to this heading">¶</a></h4>
<p>Readers may be aware of pattern matching through the simple example of matching
a subject (data object) to a literal (pattern) with the switch statement found
in C, Java or JavaScript (and many other languages). Often the switch statement
is used for comparison of an object/expression with case statements containing
literals.</p>
<p>More powerful examples of pattern matching can be found in languages such as
Scala and Elixir. With structural pattern matching, the approach is “declarative” and
explicitly states the conditions (the patterns) for data to match.</p>
<p>While an “imperative” series of instructions using nested “if” statements
could be used to accomplish something similar to structural pattern matching,
it is less clear than the “declarative” approach. Instead the “declarative”
approach states the conditions to meet for a match and is more readable through
its explicit patterns. While structural pattern matching can be used in its
simplest form comparing a variable to a literal in a case statement, its
true value for Python lies in its handling of the subject’s type and shape.</p>
</section>
<section id="simple-pattern-match-to-a-literal">
<h4>Simple pattern: match to a literal<a class="headerlink" href="#simple-pattern-match-to-a-literal" title="Permalink to this heading">¶</a></h4>
<p>Let’s look at this example as pattern matching in its simplest form: a value,
the subject, being matched to several literals, the patterns. In the example
below, <code class="docutils literal notranslate"><span class="pre">status</span></code> is the subject of the match statement. The patterns are
each of the case statements, where literals represent request status codes.
The associated action to the case is executed after a match:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">http_error</span><span class="p">(</span><span class="n">status</span><span class="p">):</span>
<span class="k">match</span> <span class="n">status</span><span class="p">:</span>
<span class="k">case</span> <span class="mi">400</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"Bad request"</span>
<span class="k">case</span> <span class="mi">404</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"Not found"</span>
<span class="k">case</span> <span class="mi">418</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"I'm a teapot"</span>
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"Something's wrong with the internet"</span>
</pre></div>
</div>
<p>If the above function is passed a <code class="docutils literal notranslate"><span class="pre">status</span></code> of 418, “I’m a teapot” is returned.
If the above function is passed a <code class="docutils literal notranslate"><span class="pre">status</span></code> of 500, the case statement with
<code class="docutils literal notranslate"><span class="pre">_</span></code> will match as a wildcard, and “Something’s wrong with the internet” is
returned.
Note the last block: the variable name, <code class="docutils literal notranslate"><span class="pre">_</span></code>, acts as a <em>wildcard</em> and insures
the subject will always match. The use of <code class="docutils literal notranslate"><span class="pre">_</span></code> is optional.</p>
<p>You can combine several literals in a single pattern using <code class="docutils literal notranslate"><span class="pre">|</span></code> (“or”):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">case</span> <span class="mi">401</span> <span class="o">|</span> <span class="mi">403</span> <span class="o">|</span> <span class="mi">404</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"Not allowed"</span>
</pre></div>
</div>
<section id="behavior-without-the-wildcard">
<h5>Behavior without the wildcard<a class="headerlink" href="#behavior-without-the-wildcard" title="Permalink to this heading">¶</a></h5>
<p>If we modify the above example by removing the last case block, the example
becomes:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">http_error</span><span class="p">(</span><span class="n">status</span><span class="p">):</span>
<span class="k">match</span> <span class="n">status</span><span class="p">:</span>
<span class="k">case</span> <span class="mi">400</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"Bad request"</span>
<span class="k">case</span> <span class="mi">404</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"Not found"</span>
<span class="k">case</span> <span class="mi">418</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">"I'm a teapot"</span>
</pre></div>
</div>
<p>Without the use of <code class="docutils literal notranslate"><span class="pre">_</span></code> in a case statement, a match may not exist. If no
match exists, the behavior is a no-op. For example, if <code class="docutils literal notranslate"><span class="pre">status</span></code> of 500 is
passed, a no-op occurs.</p>
</section>
</section>
<section id="patterns-with-a-literal-and-variable">
<h4>Patterns with a literal and variable<a class="headerlink" href="#patterns-with-a-literal-and-variable" title="Permalink to this heading">¶</a></h4>
<p>Patterns can look like unpacking assignments, and a pattern may be used to bind
variables. In this example, a data point can be unpacked to its x-coordinate
and y-coordinate:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># point is an (x, y) tuple</span>
<span class="k">match</span> <span class="n">point</span><span class="p">:</span>
<span class="k">case</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Origin"</span><span class="p">)</span>
<span class="k">case</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Y=</span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">case</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"X=</span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">case</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"X=</span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">, Y=</span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"Not a point"</span><span class="p">)</span>
</pre></div>
</div>
<p>The first pattern has two literals, <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code>, and may be thought of as an
extension of the literal pattern shown above. The next two patterns combine a
literal and a variable, and the variable <em>binds</em> a value from the subject
(<code class="docutils literal notranslate"><span class="pre">point</span></code>). The fourth pattern captures two values, which makes it
conceptually similar to the unpacking assignment <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y)</span> <span class="pre">=</span> <span class="pre">point</span></code>.</p>
</section>
<section id="patterns-and-classes">
<h4>Patterns and classes<a class="headerlink" href="#patterns-and-classes" title="Permalink to this heading">¶</a></h4>
<p>If you are using classes to structure your data, you can use as a pattern
the class name followed by an argument list resembling a constructor. This
pattern has the ability to capture class attributes into variables:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
<span class="n">x</span><span class="p">:</span> <span class="nb">int</span>
<span class="n">y</span><span class="p">:</span> <span class="nb">int</span>
<span class="k">def</span> <span class="nf">location</span><span class="p">(</span><span class="n">point</span><span class="p">):</span>
<span class="k">match</span> <span class="n">point</span><span class="p">:</span>
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Origin is the point's location."</span><span class="p">)</span>
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">y</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Y=</span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2"> and the point is on the y-axis."</span><span class="p">)</span>
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"X=</span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2"> and the point is on the x-axis."</span><span class="p">)</span>
<span class="k">case</span> <span class="n">Point</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"The point is located somewhere else on the plane."</span><span class="p">)</span>
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Not a point"</span><span class="p">)</span>
</pre></div>
</div>
<section id="patterns-with-positional-parameters">
<h5>Patterns with positional parameters<a class="headerlink" href="#patterns-with-positional-parameters" title="Permalink to this heading">¶</a></h5>
<p>You can use positional parameters with some builtin classes that provide an
ordering for their attributes (e.g. dataclasses). You can also define a specific
position for attributes in patterns by setting the <code class="docutils literal notranslate"><span class="pre">__match_args__</span></code> special
attribute in your classes. If it’s set to (“x”, “y”), the following patterns
are all equivalent (and all bind the <code class="docutils literal notranslate"><span class="pre">y</span></code> attribute to the <code class="docutils literal notranslate"><span class="pre">var</span></code> variable):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Point</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">var</span><span class="p">)</span>
<span class="n">Point</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">var</span><span class="p">)</span>
<span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">var</span><span class="p">)</span>
<span class="n">Point</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">var</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</section>
</section>
<section id="nested-patterns">
<h4>Nested patterns<a class="headerlink" href="#nested-patterns" title="Permalink to this heading">¶</a></h4>
<p>Patterns can be arbitrarily nested. For example, if our data is a short
list of points, it could be matched like this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">points</span><span class="p">:</span>
<span class="k">case</span> <span class="p">[]:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"No points in the list."</span><span class="p">)</span>
<span class="k">case</span> <span class="p">[</span><span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"The origin is the only point in the list."</span><span class="p">)</span>
<span class="k">case</span> <span class="p">[</span><span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)]:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"A single point </span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2"> is in the list."</span><span class="p">)</span>
<span class="k">case</span> <span class="p">[</span><span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">y1</span><span class="p">),</span> <span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">y2</span><span class="p">)]:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Two points on the Y axis at </span><span class="si">{</span><span class="n">y1</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">y2</span><span class="si">}</span><span class="s2"> are in the list."</span><span class="p">)</span>
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Something else is found in the list."</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="complex-patterns-and-the-wildcard">
<h4>Complex patterns and the wildcard<a class="headerlink" href="#complex-patterns-and-the-wildcard" title="Permalink to this heading">¶</a></h4>
<p>To this point, the examples have used <code class="docutils literal notranslate"><span class="pre">_</span></code> alone in the last case statement.
A wildcard can be used in more complex patterns, such as <code class="docutils literal notranslate"><span class="pre">('error',</span> <span class="pre">code,</span> <span class="pre">_)</span></code>.
For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">test_variable</span><span class="p">:</span>
<span class="k">case</span> <span class="p">(</span><span class="s1">'warning'</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="mi">40</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"A warning has been received."</span><span class="p">)</span>
<span class="k">case</span><span class="w"> </span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="k">_</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"An error </span><span class="si">{</span><span class="n">code</span><span class="si">}</span><span class="s2"> occurred."</span><span class="p">)</span>
</pre></div>
</div>
<p>In the above case, <code class="docutils literal notranslate"><span class="pre">test_variable</span></code> will match for (‘error’, code, 100) and
(‘error’, code, 800).</p>
</section>
<section id="guard">
<h4>Guard<a class="headerlink" href="#guard" title="Permalink to this heading">¶</a></h4>
<p>We can add an <code class="docutils literal notranslate"><span class="pre">if</span></code> clause to a pattern, known as a “guard”. If the
guard is false, <code class="docutils literal notranslate"><span class="pre">match</span></code> goes on to try the next case block. Note
that value capture happens before the guard is evaluated:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">point</span><span class="p">:</span>
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="n">y</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The point is located on the diagonal Y=X at </span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">."</span><span class="p">)</span>
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Point is not on the diagonal."</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="other-key-features">
<h4>Other Key Features<a class="headerlink" href="#other-key-features" title="Permalink to this heading">¶</a></h4>
<p>Several other key features:</p>
<ul>
<li><p>Like unpacking assignments, tuple and list patterns have exactly the
same meaning and actually match arbitrary sequences. Technically,
the subject must be a sequence.
Therefore, an important exception is that patterns don’t match iterators.
Also, to prevent a common mistake, sequence patterns don’t match strings.</p></li>
<li><p>Sequence patterns support wildcards: <code class="docutils literal notranslate"><span class="pre">[x,</span> <span class="pre">y,</span> <span class="pre">*rest]</span></code> and <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y,</span>
<span class="pre">*rest)</span></code> work similar to wildcards in unpacking assignments. The
name after <code class="docutils literal notranslate"><span class="pre">*</span></code> may also be <code class="docutils literal notranslate"><span class="pre">_</span></code>, so <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y,</span> <span class="pre">*_)</span></code> matches a sequence
of at least two items without binding the remaining items.</p></li>
<li><p>Mapping patterns: <code class="docutils literal notranslate"><span class="pre">{"bandwidth":</span> <span class="pre">b,</span> <span class="pre">"latency":</span> <span class="pre">l}</span></code> captures the
<code class="docutils literal notranslate"><span class="pre">"bandwidth"</span></code> and <code class="docutils literal notranslate"><span class="pre">"latency"</span></code> values from a dict. Unlike sequence
patterns, extra keys are ignored. A wildcard <code class="docutils literal notranslate"><span class="pre">**rest</span></code> is also
supported. (But <code class="docutils literal notranslate"><span class="pre">**_</span></code> would be redundant, so is not allowed.)</p></li>
<li><p>Subpatterns may be captured using the <code class="docutils literal notranslate"><span class="pre">as</span></code> keyword:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">case</span> <span class="p">(</span><span class="n">Point</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">y1</span><span class="p">),</span> <span class="n">Point</span><span class="p">(</span><span class="n">x2</span><span class="p">,</span> <span class="n">y2</span><span class="p">)</span> <span class="k">as</span> <span class="n">p2</span><span class="p">):</span> <span class="o">...</span>
</pre></div>
</div>
<p>This binds x1, y1, x2, y2 like you would expect without the <code class="docutils literal notranslate"><span class="pre">as</span></code> clause,
and p2 to the entire second item of the subject.</p>
</li>
<li><p>Most literals are compared by equality. However, the singletons <code class="docutils literal notranslate"><span class="pre">True</span></code>,
<code class="docutils literal notranslate"><span class="pre">False</span></code> and <code class="docutils literal notranslate"><span class="pre">None</span></code> are compared by identity.</p></li>
<li><p>Named constants may be used in patterns. These named constants must be
dotted names to prevent the constant from being interpreted as a capture
variable:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">enum</span> <span class="kn">import</span> <span class="n">Enum</span>
<span class="k">class</span> <span class="nc">Color</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
<span class="n">RED</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">GREEN</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">BLUE</span> <span class="o">=</span> <span class="mi">2</span>
<span class="k">match</span> <span class="n">color</span><span class="p">:</span>
<span class="k">case</span> <span class="n">Color</span><span class="o">.</span><span class="n">RED</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"I see red!"</span><span class="p">)</span>
<span class="k">case</span> <span class="n">Color</span><span class="o">.</span><span class="n">GREEN</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Grass is green"</span><span class="p">)</span>
<span class="k">case</span> <span class="n">Color</span><span class="o">.</span><span class="n">BLUE</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"I'm feeling the blues :("</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<p>For the full specification see <span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0634/"><strong>PEP 634</strong></a>. Motivation and rationale
are in <span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-0635/"><strong>PEP 635</strong></a>, and a longer tutorial is in <span class="target" id="index-17"></span><a class="pep reference external" href="https://peps.python.org/pep-0636/"><strong>PEP 636</strong></a>.</p>
</section>
</section>
<section id="optional-encodingwarning-and-encoding-locale-option">
<span id="whatsnew310-pep597"></span><h3>Optional <code class="docutils literal notranslate"><span class="pre">EncodingWarning</span></code> and <code class="docutils literal notranslate"><span class="pre">encoding="locale"</span></code> option<a class="headerlink" href="#optional-encodingwarning-and-encoding-locale-option" title="Permalink to this heading">¶</a></h3>
<p>The default encoding of <code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code> and <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> is
platform and locale dependent. Since UTF-8 is used on most Unix
platforms, omitting <code class="docutils literal notranslate"><span class="pre">encoding</span></code> option when opening UTF-8 files
(e.g. JSON, YAML, TOML, Markdown) is a very common bug. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># BUG: "rb" mode or encoding="utf-8" should be used.</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"data.json"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p>To find this type of bug, an optional <code class="docutils literal notranslate"><span class="pre">EncodingWarning</span></code> is added.
It is emitted when <a class="reference internal" href="../library/sys.html#sys.flags" title="sys.flags"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.flags.warn_default_encoding</span></code></a>
is true and locale-specific default encoding is used.</p>
<p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">warn_default_encoding</span></code> option and <span class="target" id="index-18"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONWARNDEFAULTENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNDEFAULTENCODING</span></code></a>
are added to enable the warning.</p>
<p>See <a class="reference internal" href="../library/io.html#io-text-encoding"><span class="std std-ref">Text Encoding</span></a> for more information.</p>
</section>
</section>
<section id="new-features-related-to-type-hints">
<span id="new-feat-related-type-hints"></span><h2>New Features Related to Type Hints<a class="headerlink" href="#new-features-related-to-type-hints" title="Permalink to this heading">¶</a></h2>
<p>This section covers major changes affecting <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0484/"><strong>PEP 484</strong></a> type hints and
the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module.</p>
<section id="pep-604-new-type-union-operator">
<h3>PEP 604: New Type Union Operator<a class="headerlink" href="#pep-604-new-type-union-operator" title="Permalink to this heading">¶</a></h3>
<p>A new type union operator was introduced which enables the syntax <code class="docutils literal notranslate"><span class="pre">X</span> <span class="pre">|</span> <span class="pre">Y</span></code>.
This provides a cleaner way of expressing ‘either type X or type Y’ instead of
using <a class="reference internal" href="../library/typing.html#typing.Union" title="typing.Union"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.Union</span></code></a>, especially in type hints.</p>
<p>In previous versions of Python, to apply a type hint for functions accepting
arguments of multiple types, <a class="reference internal" href="../library/typing.html#typing.Union" title="typing.Union"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.Union</span></code></a> was used:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">square</span><span class="p">(</span><span class="n">number</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">])</span> <span class="o">-></span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">]:</span>
<span class="k">return</span> <span class="n">number</span> <span class="o">**</span> <span class="mi">2</span>
</pre></div>
</div>
<p>Type hints can now be written in a more succinct manner:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">square</span><span class="p">(</span><span class="n">number</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">float</span><span class="p">:</span>
<span class="k">return</span> <span class="n">number</span> <span class="o">**</span> <span class="mi">2</span>
</pre></div>
</div>
<p>This new syntax is also accepted as the second argument to <a class="reference internal" href="../library/functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a>
and <a class="reference internal" href="../library/functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">str</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="../library/stdtypes.html#types-union"><span class="std std-ref">Sendika Türü</span></a> and <span class="target" id="index-20"></span><a class="pep reference external" href="https://peps.python.org/pep-0604/"><strong>PEP 604</strong></a> for more details.</p>
<p>(Contributed by Maggie Moss and Philippe Prados in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41428">bpo-41428</a>,
with additions by Yurii Karabas and Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44490">bpo-44490</a>.)</p>
</section>
<section id="pep-612-parameter-specification-variables">
<h3>PEP 612: Parameter Specification Variables<a class="headerlink" href="#pep-612-parameter-specification-variables" title="Permalink to this heading">¶</a></h3>
<p>Two new options to improve the information provided to static type checkers for
<span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-0484/"><strong>PEP 484</strong></a>‘s <code class="docutils literal notranslate"><span class="pre">Callable</span></code> have been added to the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module.</p>
<p>The first is the parameter specification variable. They are used to forward the
parameter types of one callable to another callable – a pattern commonly
found in higher order functions and decorators. Examples of usage can be found
in <a class="reference internal" href="../library/typing.html#typing.ParamSpec" title="typing.ParamSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpec</span></code></a>. Previously, there was no easy way to type annotate
dependency of parameter types in such a precise manner.</p>
<p>The second option is the new <code class="docutils literal notranslate"><span class="pre">Concatenate</span></code> operator. It’s used in conjunction
with parameter specification variables to type annotate a higher order callable
which adds or removes parameters of another callable. Examples of usage can
be found in <a class="reference internal" href="../library/typing.html#typing.Concatenate" title="typing.Concatenate"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Concatenate</span></code></a>.</p>
<p>See <a class="reference internal" href="../library/typing.html#typing.Callable" title="typing.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Callable</span></code></a>, <a class="reference internal" href="../library/typing.html#typing.ParamSpec" title="typing.ParamSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpec</span></code></a>,
<a class="reference internal" href="../library/typing.html#typing.Concatenate" title="typing.Concatenate"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Concatenate</span></code></a>, <a class="reference internal" href="../library/typing.html#typing.ParamSpecArgs" title="typing.ParamSpecArgs"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpecArgs</span></code></a>,
<a class="reference internal" href="../library/typing.html#typing.ParamSpecKwargs" title="typing.ParamSpecKwargs"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpecKwargs</span></code></a>, and <span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0612/"><strong>PEP 612</strong></a> for more details.</p>
<p>(Contributed by Ken Jin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41559">bpo-41559</a>, with minor enhancements by Jelle
Zijlstra in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43783">bpo-43783</a>. PEP written by Mark Mendoza.)</p>
</section>
<section id="pep-613-typealias">
<h3>PEP 613: TypeAlias<a class="headerlink" href="#pep-613-typealias" title="Permalink to this heading">¶</a></h3>
<p><span class="target" id="index-23"></span><a class="pep reference external" href="https://peps.python.org/pep-0484/"><strong>PEP 484</strong></a> introduced the concept of type aliases, only requiring them to be
top-level unannotated assignments. This simplicity sometimes made it difficult
for type checkers to distinguish between type aliases and ordinary assignments,
especially when forward references or invalid types were involved. Compare:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">StrCache</span> <span class="o">=</span> <span class="s1">'Cache[str]'</span> <span class="c1"># a type alias</span>
<span class="n">LOG_PREFIX</span> <span class="o">=</span> <span class="s1">'LOG[DEBUG]'</span> <span class="c1"># a module constant</span>
</pre></div>
</div>
<p>Now the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module has a special value <code class="xref py py-data docutils literal notranslate"><span class="pre">TypeAlias</span></code>
which lets you declare type aliases more explicitly:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">StrCache</span><span class="p">:</span> <span class="n">TypeAlias</span> <span class="o">=</span> <span class="s1">'Cache[str]'</span> <span class="c1"># a type alias</span>
<span class="n">LOG_PREFIX</span> <span class="o">=</span> <span class="s1">'LOG[DEBUG]'</span> <span class="c1"># a module constant</span>
</pre></div>
</div>
<p>See <span class="target" id="index-24"></span><a class="pep reference external" href="https://peps.python.org/pep-0613/"><strong>PEP 613</strong></a> for more details.</p>
<p>(Contributed by Mikhail Golubev in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41923">bpo-41923</a>.)</p>
</section>
<section id="pep-647-user-defined-type-guards">
<h3>PEP 647: User-Defined Type Guards<a class="headerlink" href="#pep-647-user-defined-type-guards" title="Permalink to this heading">¶</a></h3>
<p><code class="xref py py-data docutils literal notranslate"><span class="pre">TypeGuard</span></code> has been added to the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module to annotate
type guard functions and improve information provided to static type checkers
during type narrowing. For more information, please see <code class="xref py py-data docutils literal notranslate"><span class="pre">TypeGuard</span></code>‘s
documentation, and <span class="target" id="index-25"></span><a class="pep reference external" href="https://peps.python.org/pep-0647/"><strong>PEP 647</strong></a>.</p>
<p>(Contributed by Ken Jin and Guido van Rossum in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43766">bpo-43766</a>.
PEP written by Eric Traut.)</p>
</section>
</section>
<section id="other-language-changes">