-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathsimple_stmts.html
More file actions
854 lines (802 loc) · 110 KB
/
simple_stmts.html
File metadata and controls
854 lines (802 loc) · 110 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
<!DOCTYPE html>
<html lang="zh-TW" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>7. 簡單陳述式 — Python 3.14.5 說明文件</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=4365c8fe" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
<script src="../_static/documentation_options.js?v=8c54008b"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/translations.js?v=cbf116e0"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.14.5 說明文件 中搜尋"
href="../_static/opensearch.xml"/>
<link rel="author" title="關於這些文件" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜尋" href="../search.html" />
<link rel="copyright" title="版權所有" href="../copyright.html" />
<link rel="next" title="8. 複合陳述式" href="compound_stmts.html" />
<link rel="prev" title="6. 運算式" href="expressions.html" />
<link rel="canonical" href="https://docs.python.org/3/reference/simple_stmts.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>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</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="選單">
<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="Python 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="快速搜索" aria-label="快速搜索" type="search" name="q">
<input type="submit" value="前往">
</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">
主題
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>自動</option>
<option value="light">淺色模式</option>
<option value="dark">深色模式</option>
</select>
</label>
<div>
<h3><a href="../contents.html">目錄</a></h3>
<ul>
<li><a class="reference internal" href="#">7. 簡單陳述式</a><ul>
<li><a class="reference internal" href="#expression-statements">7.1. 運算式陳述式</a></li>
<li><a class="reference internal" href="#assignment-statements">7.2. 賦值陳述式</a><ul>
<li><a class="reference internal" href="#augmented-assignment-statements">7.2.1. 擴增賦值陳述式</a></li>
<li><a class="reference internal" href="#annotated-assignment-statements">7.2.2. 註釋賦值陳述式</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-assert-statement">7.3. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">assert</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-pass-statement">7.4. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">pass</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-del-statement">7.5. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-return-statement">7.6. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-yield-statement">7.7. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-raise-statement">7.8. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-break-statement">7.9. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-continue-statement">7.10. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">continue</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-import-statement">7.11. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> 陳述式</a><ul>
<li><a class="reference internal" href="#future-statements">7.11.1. Future 陳述式</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-global-statement">7.12. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-nonlocal-statement">7.13. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-type-statement">7.14. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code> 陳述式</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>上個主題</h4>
<p class="topless"><a href="expressions.html"
title="上一章"><span class="section-number">6. </span>運算式</a></p>
</div>
<div>
<h4>下個主題</h4>
<p class="topless"><a href="compound_stmts.html"
title="下一章"><span class="section-number">8. </span>複合陳述式</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "reference/simple_stmts.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>此頁面</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">回報錯誤</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">改進此頁面</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/reference/simple_stmts.rst?plain=1"
rel="nofollow">顯示原始碼
</a>
</li>
<li>
<a href="https://github.com/python/python-docs-zh-TW/blob/3.14/reference/simple_stmts.po?plain=1"
rel="nofollow">顯示翻譯原始碼</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>導航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="總索引"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="compound_stmts.html" title="8. 複合陳述式"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="expressions.html" title="6. 運算式"
accesskey="P">上一頁</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.14.5 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 語言參考手冊</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">7. </span>簡單陳述式</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="search" name="q" id="search-box">
<input type="submit" value="前往">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
主題
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>自動</option>
<option value="light">淺色模式</option>
<option value="dark">深色模式</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="simple-statements">
<span id="simple"></span><h1><span class="section-number">7. </span>簡單陳述式<a class="headerlink" href="#simple-statements" title="連結到這個標頭">¶</a></h1>
<p id="index-0">一個簡單陳述式會被包含在一個單獨的邏輯列中。多個簡單陳述式用分號分隔就可以出現在同一列中。簡單陳述式的語法如下:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-simple_stmt">simple_stmt</strong>: <a class="reference internal" href="#grammar-token-python-grammar-expression_stmt"><code class="xref docutils literal notranslate"><span class="pre">expression_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-assert_stmt"><code class="xref docutils literal notranslate"><span class="pre">assert_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-assignment_stmt"><code class="xref docutils literal notranslate"><span class="pre">assignment_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-augmented_assignment_stmt"><code class="xref docutils literal notranslate"><span class="pre">augmented_assignment_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-annotated_assignment_stmt"><code class="xref docutils literal notranslate"><span class="pre">annotated_assignment_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-pass_stmt"><code class="xref docutils literal notranslate"><span class="pre">pass_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-del_stmt"><code class="xref docutils literal notranslate"><span class="pre">del_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-return_stmt"><code class="xref docutils literal notranslate"><span class="pre">return_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-yield_stmt"><code class="xref docutils literal notranslate"><span class="pre">yield_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-raise_stmt"><code class="xref docutils literal notranslate"><span class="pre">raise_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-break_stmt"><code class="xref docutils literal notranslate"><span class="pre">break_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-continue_stmt"><code class="xref docutils literal notranslate"><span class="pre">continue_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-import_stmt"><code class="xref docutils literal notranslate"><span class="pre">import_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-future_stmt"><code class="xref docutils literal notranslate"><span class="pre">future_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-global_stmt"><code class="xref docutils literal notranslate"><span class="pre">global_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-nonlocal_stmt"><code class="xref docutils literal notranslate"><span class="pre">nonlocal_stmt</span></code></a>
| <a class="reference internal" href="#grammar-token-python-grammar-type_stmt"><code class="xref docutils literal notranslate"><span class="pre">type_stmt</span></code></a>
</pre>
<section id="expression-statements">
<span id="exprstmts"></span><h2><span class="section-number">7.1. </span>運算式陳述式<a class="headerlink" href="#expression-statements" title="連結到這個標頭">¶</a></h2>
<p id="index-2"><span id="index-1"></span>運算式陳述式(主要在互動模式下)用於計算並寫入一個值,或者(通常)用於呼叫一個程序 (procedure)(一個不回傳有意義結果的函式;在 Python 中,程序會回傳 <code class="docutils literal notranslate"><span class="pre">None</span></code> 值)。運算式陳述式的其他用途也是被允許的,並且偶爾會很有用。運算式陳述式的語法如下:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-expression_stmt">expression_stmt</strong>: <a class="reference internal" href="expressions.html#grammar-token-python-grammar-starred_expression"><code class="xref docutils literal notranslate"><span class="pre">starred_expression</span></code></a>
</pre>
<p>運算式陳述式會對運算式串列(可能是單個運算式)進行求值 (evaluate)。</p>
<p id="index-3">在互動模式下,如果值不是 <code class="docutils literal notranslate"><span class="pre">None</span></code>,就會以內建的 <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 函式轉換成字串,並將結果字串單獨寫入標準輸出的一列中(除非結果是 <code class="docutils literal notranslate"><span class="pre">None</span></code>,則程序呼叫就不會產生任何輸出)。</p>
</section>
<section id="assignment-statements">
<span id="assignment"></span><h2><span class="section-number">7.2. </span>賦值陳述式<a class="headerlink" href="#assignment-statements" title="連結到這個標頭">¶</a></h2>
<p id="index-4">賦值陳述式用於將名稱(重新)繫結到值,以及修改可變物件的屬性或項目:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-assignment_stmt">assignment_stmt</strong>: (<a class="reference internal" href="#grammar-token-python-grammar-target_list"><code class="xref docutils literal notranslate"><span class="pre">target_list</span></code></a> <span class="sx">"="</span>)+ (<a class="reference internal" href="expressions.html#grammar-token-python-grammar-starred_expression"><code class="xref docutils literal notranslate"><span class="pre">starred_expression</span></code></a> | <a class="reference internal" href="expressions.html#grammar-token-python-grammar-yield_expression"><code class="xref docutils literal notranslate"><span class="pre">yield_expression</span></code></a>)
<strong id="grammar-token-python-grammar-target_list">target_list</strong>: <a class="reference internal" href="#grammar-token-python-grammar-target"><code class="xref docutils literal notranslate"><span class="pre">target</span></code></a> (<span class="sx">","</span> <a class="reference internal" href="#grammar-token-python-grammar-target"><code class="xref docutils literal notranslate"><span class="pre">target</span></code></a>)* [<span class="sx">","</span>]
<strong id="grammar-token-python-grammar-target">target</strong>: <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>
| <span class="sx">"("</span> [<a class="reference internal" href="#grammar-token-python-grammar-target_list"><code class="xref docutils literal notranslate"><span class="pre">target_list</span></code></a>] <span class="sx">")"</span>
| <span class="sx">"["</span> [<a class="reference internal" href="#grammar-token-python-grammar-target_list"><code class="xref docutils literal notranslate"><span class="pre">target_list</span></code></a>] <span class="sx">"]"</span>
| <a class="reference internal" href="expressions.html#grammar-token-python-grammar-attributeref"><code class="xref docutils literal notranslate"><span class="pre">attributeref</span></code></a>
| <a class="reference internal" href="expressions.html#grammar-token-python-grammar-subscription"><code class="xref docutils literal notranslate"><span class="pre">subscription</span></code></a>
| <span class="sx">"*"</span> <a class="reference internal" href="#grammar-token-python-grammar-target"><code class="xref docutils literal notranslate"><span class="pre">target</span></code></a>
</pre>
<p>(關於 <em>attributeref</em> 和 <em>subscription</em> 的語法定義,請參閱 <a class="reference internal" href="expressions.html#primaries"><span class="std std-ref">Primaries</span></a> 章節。)</p>
<p>賦值陳述式會對運算式串列進行求值(請記住,這可以是單個運算式或以逗號分隔的串列,後者會產生一個元組),並將單個結果物件從左到右賦值給每個目標串列。</p>
<p id="index-5">賦值是根據目標(串列)的形式遞迴定義的。當目標是可變物件的一部分(屬性參照或下標)時,該可變物件最終必須執行賦值並決定其有效性,如果賦值不被接受則可能會引發例外。各種型別遵循的規則以及引發的例外在物件型別的定義中給定(請參閱<a class="reference internal" href="datamodel.html#types"><span class="std std-ref">標準型別階層</span></a>章節)。</p>
<p id="index-6">將物件賦值給目標串列(可選擇性地用圓括號或方括號括起來)是以下列方式遞迴定義的。</p>
<ul class="simple">
<li><p>如果目標串列是單個目標且沒有尾隨逗號(可選擇性地用圓括號括起來),則物件會被賦值給該目標。</p></li>
<li><p>否則:</p>
<ul>
<li><p>如果目標串列包含一個以星號為前綴的目標,稱為 "starred" 目標:該物件必須是一個可疊代物件,其項目數量至少與目標串列中的目標數量減一一樣多。可疊代物件的前幾個項目從左到右賦值給 starred 目標之前的目標。可疊代物件的最後幾個項目賦值給 starred 目標之後的目標。然後可疊代物件中剩餘項目的串列會被賦值給 starred 目標(該串列可以為空)。</p></li>
<li><p>否則:該物件必須是一個可疊代物件,其項目數量與目標串列中的目標數量相同,並且這些項目從左到右賦值給對應的目標。</p></li>
</ul>
</li>
</ul>
<p>將物件賦值給單個目標是以下列方式遞迴定義的。</p>
<ul>
<li><p>如果目標是一個識別字(名稱):</p>
<ul class="simple">
<li><p>如果該名稱沒有出現在當前程式碼區塊的 <a class="reference internal" href="#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 或 <a class="reference internal" href="#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 陳述式中:該名稱會被繫結到當前區域命名空間中的物件。</p></li>
<li><p>否則:該名稱分別被繫結到全域命名空間或由 <a class="reference internal" href="#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 決定的外層命名空間中的物件。</p></li>
</ul>
<p id="index-7">如果名稱已經被繫結,則會重新繫結。這可能導致先前繫結到該名稱的物件的參照計數變為零,從而導致該物件被釋放並呼叫其解構函式(如果有的話)。</p>
</li>
<li id="index-8"><p>如果目標是屬性參照:參照中的主要運算式會被求值。它應該產生一個具有可賦值屬性的物件;如果不是這種情況,則會引發 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。然後要求該物件將被賦值的物件賦值給指定的屬性;如果它無法執行賦值,則會引發例外(通常是 <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> 但並非一定)。</p>
<p id="attr-target-note">注意:如果物件是類別實例,且屬性參照出現在賦值運算子的兩側,右側運算式 <code class="docutils literal notranslate"><span class="pre">a.x</span></code> 可以存取實例屬性或(如果不存在實例屬性)類別屬性。左側目標 <code class="docutils literal notranslate"><span class="pre">a.x</span></code> 總是被設定為實例屬性,如有必要會建立它。因此,<code class="docutils literal notranslate"><span class="pre">a.x</span></code> 的兩次出現不一定指向同一個屬性:如果右側運算式參照的是類別屬性,左側會建立一個新的實例屬性作為賦值的目標:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">Cls</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># 類別變數</span>
<span class="n">inst</span> <span class="o">=</span> <span class="n">Cls</span><span class="p">()</span>
<span class="n">inst</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">inst</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># 將 inst.x 寫為 4,Cls.x 保持為 3</span>
</pre></div>
</div>
<p>此描述不一定適用於描述器屬性,例如使用 <a class="reference internal" href="../library/functions.html#property" title="property"><code class="xref py py-func docutils literal notranslate"><span class="pre">property()</span></code></a> 建立的特性 (property)。</p>
</li>
<li id="index-9"><p>如果目標是下標:參照中的主要運算式會被求值。接著,下標運算式會被求值。然後會以兩個引數(下標和被賦值的物件)呼叫主要物件的 <a class="reference internal" href="datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a> 方法。</p>
<p>通常 <a class="reference internal" href="datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a> 被定義在可變序列物件(如串列)和對映物件(如字典)上,其行為如下。</p>
<p id="index-10">如果主要物件是可變序列物件(如串列),下標必須產生一個整數。如果它是負數,會加上序列的長度。結果值必須是一個非負整數且小於序列的長度,然後要求該序列將被賦值的物件賦值給具有該索引的項目。如果索引超出範圍,則會引發 <a class="reference internal" href="../library/exceptions.html#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a>(對下標序列的賦值無法向串列新增項目)。</p>
<p id="index-11">如果主要物件是對映物件(如字典),下標的型別必須與對映的鍵型別相容,然後要求該對映建立一個將下標對映到被賦值物件的鍵/值對。這可以替換具有相同鍵值的現有鍵/值對,或插入新的鍵/值對(如果不存在相同值的鍵)。</p>
<p id="index-12">如果目標是切片:主要運算式應該求值為一個可變序列物件(如串列)。被賦值的物件應該是<a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">可疊代的</span></a>。切片的下界和上界應該是整數;如果它們是 <code class="docutils literal notranslate"><span class="pre">None</span></code>(或不存在),預設值為零和序列的長度。如果任一邊界為負數,則會加上序列的長度。結果邊界會被裁剪到零和序列長度之間(包含邊界)。最後,要求該序列物件用被賦值序列的項目替換切片。切片的長度可能與被賦值序列的長度不同,因此會改變目標序列的長度(如果目標序列允許的話)。</p>
</li>
</ul>
<p>雖然賦值的定義意味著左側和右側之間的重疊是「同時的」(例如 <code class="docutils literal notranslate"><span class="pre">a,</span> <span class="pre">b</span> <span class="pre">=</span> <span class="pre">b,</span> <span class="pre">a</span></code> 會交換兩個變數),但被賦值變數集合<em>內部</em>的重疊是從左到右發生的,有時會導致混淆。例如,以下程式會印出 <code class="docutils literal notranslate"><span class="pre">[0,</span> <span class="pre">2]</span></code>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">i</span><span class="p">,</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span> <span class="c1"># i 先被更新,然後 x[i] 再被更新</span>
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">也參考</p>
<dl class="simple">
<dt><span class="target" id="index-56"></span><a class="pep reference external" href="https://peps.python.org/pep-3132/"><strong>PEP 3132</strong></a> - 擴充可疊代物件拆解</dt><dd><p><code class="docutils literal notranslate"><span class="pre">*target</span></code> 功能的規格說明。</p>
</dd>
</dl>
</div>
<section id="augmented-assignment-statements">
<span id="augassign"></span><h3><span class="section-number">7.2.1. </span>擴增賦值陳述式<a class="headerlink" href="#augmented-assignment-statements" title="連結到這個標頭">¶</a></h3>
<p id="index-14">擴增賦值是將二元運算與賦值陳述式結合在單一陳述式中:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-augmented_assignment_stmt">augmented_assignment_stmt</strong>: <a class="reference internal" href="#grammar-token-python-grammar-augtarget"><code class="xref docutils literal notranslate"><span class="pre">augtarget</span></code></a> <a class="reference internal" href="#grammar-token-python-grammar-augop"><code class="xref docutils literal notranslate"><span class="pre">augop</span></code></a> (<a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression_list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a> | <a class="reference internal" href="expressions.html#grammar-token-python-grammar-yield_expression"><code class="xref docutils literal notranslate"><span class="pre">yield_expression</span></code></a>)
<strong id="grammar-token-python-grammar-augtarget">augtarget</strong>: <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> | <a class="reference internal" href="expressions.html#grammar-token-python-grammar-attributeref"><code class="xref docutils literal notranslate"><span class="pre">attributeref</span></code></a> | <a class="reference internal" href="expressions.html#grammar-token-python-grammar-subscription"><code class="xref docutils literal notranslate"><span class="pre">subscription</span></code></a>
<strong id="grammar-token-python-grammar-augop">augop</strong>: <span class="sx">"+="</span> | <span class="sx">"-="</span> | <span class="sx">"*="</span> | <span class="sx">"@="</span> | <span class="sx">"/="</span> | <span class="sx">"//="</span> | <span class="sx">"%="</span> | <span class="sx">"**="</span>
| <span class="sx">">>="</span> | <span class="sx">"<<="</span> | <span class="sx">"&="</span> | <span class="sx">"^="</span> | <span class="sx">"|="</span>
</pre>
<p>(關於最後三個符號的語法定義,請參閱 <a class="reference internal" href="expressions.html#primaries"><span class="std std-ref">Primaries</span></a> 章節。)</p>
<p>擴增賦值會對目標(與一般賦值陳述式不同,它不能是拆解運算)和運算式串列進行求值,對這兩個運算元執行該賦值型別特定的二元運算,並將結果賦值給原始目標。目標只會被求值一次。</p>
<p>像 <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">+=</span> <span class="pre">1</span></code> 這樣的擴增賦值陳述式可以改寫為 <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">+</span> <span class="pre">1</span></code> 來達到類似但不完全相同的效果。在擴增版本中,<code class="docutils literal notranslate"><span class="pre">x</span></code> 只會被求值一次。此外,在可能的情況下,實際的運算會<em>原地 (in-place)</em>執行,意即不是建立一個新物件並將其賦值給目標,而是直接修改舊物件。</p>
<p>與一般賦值不同,擴增賦值會在求值右側<em>之前</em>先求值左側。例如,<code class="docutils literal notranslate"><span class="pre">a[i]</span> <span class="pre">+=</span> <span class="pre">f(x)</span></code> 會先查找 <code class="docutils literal notranslate"><span class="pre">a[i]</span></code>,然後求值 <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> 並執行加法,最後將結果寫回 <code class="docutils literal notranslate"><span class="pre">a[i]</span></code>。</p>
<p>除了在單一陳述式中賦值給元組和多個目標的情況外,擴增賦值陳述式執行的賦值與一般賦值的處理方式相同。同樣地,除了可能的<em>原地</em>行為外,擴增賦值執行的二元運算與一般二元運算相同。</p>
<p>對於屬性參照的目標,與一般賦值相同的<a class="reference internal" href="#attr-target-note"><span class="std std-ref">關於類別和實例屬性的注意事項</span></a>也適用。</p>
</section>
<section id="annotated-assignment-statements">
<span id="annassign"></span><h3><span class="section-number">7.2.2. </span>註釋賦值陳述式<a class="headerlink" href="#annotated-assignment-statements" title="連結到這個標頭">¶</a></h3>
<p id="index-15"><a class="reference internal" href="../glossary.html#term-variable-annotation"><span class="xref std std-term">註釋</span></a> 賦值是將變數或屬性註釋與可選的賦值陳述式結合在一個陳述式中:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-annotated_assignment_stmt">annotated_assignment_stmt</strong>: <a class="reference internal" href="#grammar-token-python-grammar-augtarget"><code class="xref docutils literal notranslate"><span class="pre">augtarget</span></code></a> <span class="sx">":"</span> <a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
[<span class="sx">"="</span> (<a class="reference internal" href="expressions.html#grammar-token-python-grammar-starred_expression"><code class="xref docutils literal notranslate"><span class="pre">starred_expression</span></code></a> | <a class="reference internal" href="expressions.html#grammar-token-python-grammar-yield_expression"><code class="xref docutils literal notranslate"><span class="pre">yield_expression</span></code></a>)]
</pre>
<p>與一般<a class="reference internal" href="#assignment"><span class="std std-ref">賦值</span></a>的區別在於只允許單個目標。</p>
<p>如果賦值目標由一個未被圓括號括起來的單一名稱組成,則被視為「簡單的」。對於簡單的賦值目標,如果在類別或模組作用域中,註釋會被收集在一個惰性求值的<a class="reference internal" href="executionmodel.html#annotation-scopes"><span class="std std-ref">註釋作用域</span></a>中。註釋可以使用類別或模組的 <a class="reference internal" href="datamodel.html#object.__annotations__" title="object.__annotations__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__annotations__</span></code></a> 屬性來求值,或者使用 <a class="reference internal" href="../library/annotationlib.html#module-annotationlib" title="annotationlib: Functionality for introspecting annotations"><code class="xref py py-mod docutils literal notranslate"><span class="pre">annotationlib</span></code></a> 模組中的工具。</p>
<p>如果賦值目標不是簡單的(屬性、下標節點或被圓括號括起來的名稱),則註釋永遠不會被求值。</p>
<p>如果一個名稱在函式作用域中被註釋,則該名稱對該作用域而言是區域的。註釋永遠不會在函式作用域中被求值和儲存。</p>
<p>如果右側存在,註釋賦值會執行實際的賦值,就像沒有註釋一樣。如果運算式目標的右側不存在,則直譯器會對目標進行求值,但不包括最後的 <a class="reference internal" href="datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a> 或 <a class="reference internal" href="datamodel.html#object.__setattr__" title="object.__setattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code></a> 呼叫。</p>
<div class="admonition seealso">
<p class="admonition-title">也參考</p>
<dl class="simple">
<dt><span class="target" id="index-57"></span><a class="pep reference external" href="https://peps.python.org/pep-0526/"><strong>PEP 526</strong></a> - 變數註釋的語法</dt><dd><p>此提案新增了用於註釋變數型別(包括類別變數和實例變數)的語法,取代透過註解來表達的方式。</p>
</dd>
<dt><span class="target" id="index-58"></span><a class="pep reference external" href="https://peps.python.org/pep-0484/"><strong>PEP 484</strong></a> - 型別提示</dt><dd><p>新增了 <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> 模組的提案,提供了可用於靜態分析工具和 IDE 的標準型別註釋語法。</p>
</dd>
</dl>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.8 版的變更: </span>現在註釋賦值允許右側與一般賦值相同的運算式。以前,某些運算式(如未加圓括號的元組運算式)會導致語法錯誤。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.14 版的變更: </span>註釋現在會在獨立的<a class="reference internal" href="executionmodel.html#annotation-scopes"><span class="std std-ref">註釋作用域</span></a>中惰性求值。如果賦值目標不是簡單的,則註釋永遠不會被求值。</p>
</div>
</section>
</section>
<section id="the-assert-statement">
<span id="assert"></span><h2><span class="section-number">7.3. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">assert</span></code> 陳述式<a class="headerlink" href="#the-assert-statement" title="連結到這個標頭">¶</a></h2>
<p id="index-18">Assert 陳述式是將除錯斷言插入程式中的便捷方式:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-assert_stmt">assert_stmt</strong>: <span class="sx">"assert"</span> <a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> [<span class="sx">","</span> <a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>]
</pre>
<p>簡單形式 <code class="docutils literal notranslate"><span class="pre">assert</span> <span class="pre">expression</span></code> 等價於:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">__debug__</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">expression</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">AssertionError</span>
</pre></div>
</div>
<p>擴展形式 <code class="docutils literal notranslate"><span class="pre">assert</span> <span class="pre">expression1,</span> <span class="pre">expression2</span></code> 等價於:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">__debug__</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">expression1</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span><span class="n">expression2</span><span class="p">)</span>
</pre></div>
</div>
<p id="index-19">這些等價關係假設 <a class="reference internal" href="../library/constants.html#debug__" title="__debug__"><code class="xref py py-const docutils literal notranslate"><span class="pre">__debug__</span></code></a> 和 <a class="reference internal" href="../library/exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a> 參照的是具有這些名稱的內建變數。在目前的實作中,內建變數 <code class="docutils literal notranslate"><span class="pre">__debug__</span></code> 在正常情況下為 <code class="docutils literal notranslate"><span class="pre">True</span></code>,在請求最佳化時(命令列選項 <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a>)為 <code class="docutils literal notranslate"><span class="pre">False</span></code>。當在編譯時請求最佳化時,目前的程式碼產生器不會為 <a class="reference internal" href="#assert"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">assert</span></code></a> 陳述式產生任何程式碼。請注意,沒有必要在錯誤訊息中包含失敗運算式的原始碼;它會作為堆疊追蹤的一部分顯示。</p>
<p>對 <a class="reference internal" href="../library/constants.html#debug__" title="__debug__"><code class="xref py py-const docutils literal notranslate"><span class="pre">__debug__</span></code></a> 進行賦值是非法的。內建變數的值在直譯器啟動時決定。</p>
</section>
<section id="the-pass-statement">
<span id="pass"></span><h2><span class="section-number">7.4. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">pass</span></code> 陳述式<a class="headerlink" href="#the-pass-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-20">
<strong id="grammar-token-python-grammar-pass_stmt">pass_stmt</strong>: <span class="sx">"pass"</span>
</pre>
<p><a class="reference internal" href="#pass"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">pass</span></code></a> 是一個空操作——當它被執行時,什麼事都不會發生。當語法上需要一個陳述式但不需要執行任何程式碼時,它可以作為佔位符使用,例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span> <span class="k">pass</span> <span class="c1"># 一個(還)沒有實作的函式</span>
<span class="k">class</span><span class="w"> </span><span class="nc">C</span><span class="p">:</span> <span class="k">pass</span> <span class="c1"># 一個(還)沒有方法的類別</span>
</pre></div>
</div>
</section>
<section id="the-del-statement">
<span id="del"></span><h2><span class="section-number">7.5. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code> 陳述式<a class="headerlink" href="#the-del-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-21">
<strong id="grammar-token-python-grammar-del_stmt">del_stmt</strong>: <span class="sx">"del"</span> <a class="reference internal" href="#grammar-token-python-grammar-target_list"><code class="xref docutils literal notranslate"><span class="pre">target_list</span></code></a>
</pre>
<p>刪除的遞迴定義與賦值的定義方式非常相似。這裡提供一些提示,而不是詳細說明全部細節。</p>
<p>刪除目標串列會從左到右遞迴刪除每個目標。</p>
<p id="index-22">刪除名稱會從區域或全域命名空間中移除該名稱的繫結,取決於該名稱是否出現在同一程式碼區塊的 <a class="reference internal" href="#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 陳述式中。嘗試刪除未繫結的名稱會引發 <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> 例外。</p>
<p id="index-23">屬性參照和下標的刪除會傳遞給涉及的主要物件;切片的刪除通常等價於賦值一個正確型別的空切片(但這一點也是由被切片的物件決定的)。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版的變更: </span>以前,如果一個名稱作為自由變數出現在巢狀區塊中,則從區域命名空間刪除該名稱是非法的。</p>
</div>
</section>
<section id="the-return-statement">
<span id="return"></span><h2><span class="section-number">7.6. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code> 陳述式<a class="headerlink" href="#the-return-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-24">
<strong id="grammar-token-python-grammar-return_stmt">return_stmt</strong>: <span class="sx">"return"</span> [<a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression_list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a>]
</pre>
<p><a class="reference internal" href="#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> 只能在語法上出現在函式定義的巢狀結構中,不能出現在巢狀類別定義中。</p>
<p>如果運算式串列存在,則會對其求值,否則會以 <code class="docutils literal notranslate"><span class="pre">None</span></code> 替代。</p>
<p><a class="reference internal" href="#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> 會離開當前的函式呼叫,並以運算式串列(或 <code class="docutils literal notranslate"><span class="pre">None</span></code>)作為回傳值。</p>
<p id="index-25">當 <a class="reference internal" href="#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> 將控制權從帶有 <a class="reference internal" href="compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 子句的 <a class="reference internal" href="compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 陳述式中傳出時,該 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code> 子句會在真正離開函式之前被執行。</p>
<p>在產生器函式中,<a class="reference internal" href="#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> 陳述式表示產生器已完成,並會導致引發 <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a>。回傳值(如果有的話)會被用作建構 <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> 的引數,並成為 <a class="reference internal" href="../library/exceptions.html#StopIteration.value" title="StopIteration.value"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StopIteration.value</span></code></a> 屬性。</p>
<p>在非同步產生器函式中,空的 <a class="reference internal" href="#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> 陳述式表示非同步產生器已完成,並會導致引發 <a class="reference internal" href="../library/exceptions.html#StopAsyncIteration" title="StopAsyncIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopAsyncIteration</span></code></a>。在非同步產生器函式中,非空的 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code> 陳述式是語法錯誤。</p>
</section>
<section id="the-yield-statement">
<span id="yield"></span><h2><span class="section-number">7.7. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> 陳述式<a class="headerlink" href="#the-yield-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-26">
<strong id="grammar-token-python-grammar-yield_stmt">yield_stmt</strong>: <a class="reference internal" href="expressions.html#grammar-token-python-grammar-yield_expression"><code class="xref docutils literal notranslate"><span class="pre">yield_expression</span></code></a>
</pre>
<p><a class="reference internal" href="#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> 陳述式在語義上等價於<a class="reference internal" href="expressions.html#yieldexpr"><span class="std std-ref">yield 運算式</span></a>。<code class="docutils literal notranslate"><span class="pre">yield</span></code> 陳述式可用於省略在等價的 yield 運算式陳述式中原本需要的圓括號。例如,以下 yield 陳述式:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">yield</span> <span class="o"><</span><span class="n">expr</span><span class="o">></span>
<span class="k">yield from</span> <span class="o"><</span><span class="n">expr</span><span class="o">></span>
</pre></div>
</div>
<p>等價於以下 yield 運算式陳述式:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="k">yield</span> <span class="o"><</span><span class="n">expr</span><span class="o">></span><span class="p">)</span>
<span class="p">(</span><span class="k">yield from</span> <span class="o"><</span><span class="n">expr</span><span class="o">></span><span class="p">)</span>
</pre></div>
</div>
<p>Yield 運算式和陳述式只在定義<a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">產生器</span></a>函式時使用,且只在產生器函式的主體中使用。在函式定義中使用 <a class="reference internal" href="#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> 就足以使該定義建立一個產生器函式而非一般函式。</p>
<p>關於 <a class="reference internal" href="#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> 語義的完整詳情,請參閱<a class="reference internal" href="expressions.html#yieldexpr"><span class="std std-ref">Yield expressions</span></a>章節。</p>
</section>
<section id="the-raise-statement">
<span id="raise"></span><h2><span class="section-number">7.8. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code> 陳述式<a class="headerlink" href="#the-raise-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-27">
<strong id="grammar-token-python-grammar-raise_stmt">raise_stmt</strong>: <span class="sx">"raise"</span> [<a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> [<span class="sx">"from"</span> <a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>]]
</pre>
<p>如果沒有運算式,<a class="reference internal" href="#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> 會重新引發當前正在處理的例外,這也稱為<em>活動例外</em>。如果當前沒有活動例外,則會引發 <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> 例外,表示這是一個錯誤。</p>
<p>否則,<a class="reference internal" href="#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> 會將第一個運算式求值為例外物件。它必須是 <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseException</span></code></a> 的子類別或實例。如果它是一個類別,則在需要時會透過不帶引數地實例化該類別來獲得例外實例。</p>
<p>例外的<em class="dfn">型別 (type)</em>是例外實例的類別,<em class="dfn">值 (value)</em>是實例本身。</p>
<p id="index-28">通常在引發例外時會自動建立一個追蹤物件,並將其作為 <a class="reference internal" href="../library/exceptions.html#BaseException.__traceback__" title="BaseException.__traceback__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code></a> 屬性附加到例外上。你可以使用 <a class="reference internal" href="../library/exceptions.html#BaseException.with_traceback" title="BaseException.with_traceback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">with_traceback()</span></code></a> 例外方法(它會回傳同一個例外實例,並將其追蹤設定為其引數)在一個步驟中建立例外並設定你自己的追蹤,如下所示:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">"foo occurred"</span><span class="p">)</span><span class="o">.</span><span class="n">with_traceback</span><span class="p">(</span><span class="n">tracebackobj</span><span class="p">)</span>
</pre></div>
</div>
<p id="index-29"><code class="docutils literal notranslate"><span class="pre">from</span></code> 子句用於例外鏈結:如果給定,第二個<em>運算式</em>必須是另一個例外類別或實例。如果第二個運算式是例外實例,它將作為 <a class="reference internal" href="../library/exceptions.html#BaseException.__cause__" title="BaseException.__cause__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code></a> 屬性(可寫入)附加到被引發的例外上。如果運算式是例外類別,則會實例化該類別,並將產生的例外實例作為 <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> 屬性附加到被引發的例外上。如果被引發的例外未被處理,則兩個例外都會被印出:</p>
<div class="highlight-pycon 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="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"Something bad happened"</span><span class="p">)</span> <span class="kn">from</span><span class="w"> </span><span class="nn">exc</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">2</span>, in <span class="n"><module></span>
<span class="w"> </span><span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="pm">~~^~~</span>
<span class="gr">ZeroDivisionError</span>: <span class="n">division by zero</span>
<span class="gt">The above exception was the direct cause of the following exception:</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">4</span>, in <span class="n"><module></span>
<span class="w"> </span><span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"Something bad happened"</span><span class="p">)</span> <span class="kn">from</span><span class="w"> </span><span class="nn">exc</span>
<span class="gr">RuntimeError</span>: <span class="n">Something bad happened</span>
</pre></div>
</div>
<p>如果在已經處理一個例外時引發了新的例外,類似的機制會隱式地運作。使用 <a class="reference internal" href="compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> 或 <a class="reference internal" href="compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 子句,或 <a class="reference internal" href="compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 陳述式時,例外可能正在被處理。然後先前的例外會作為新例外的 <a class="reference internal" href="../library/exceptions.html#BaseException.__context__" title="BaseException.__context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code></a> 屬性附加:</p>
<div class="highlight-pycon 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="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"Something bad happened"</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">2</span>, in <span class="n"><module></span>
<span class="w"> </span><span class="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="pm">~~^~~</span>
<span class="gr">ZeroDivisionError</span>: <span class="n">division by zero</span>
<span class="gt">During handling of the above exception, another exception occurred:</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">4</span>, in <span class="n"><module></span>
<span class="w"> </span><span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"Something bad happened"</span><span class="p">)</span>
<span class="gr">RuntimeError</span>: <span class="n">Something bad happened</span>
</pre></div>
</div>
<p>可以透過在 <code class="docutils literal notranslate"><span class="pre">from</span></code> 子句中指定 <a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> 來明確抑制例外鏈結:</p>
<div class="highlight-pycon 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="nb">print</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"Something bad happened"</span><span class="p">)</span> <span class="kn">from</span><span class="w"> </span><span class="kc">None</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">4</span>, in <span class="n"><module></span>
<span class="gr">RuntimeError</span>: <span class="n">Something bad happened</span>
</pre></div>
</div>
<p>關於例外的更多資訊可以在<a class="reference internal" href="executionmodel.html#exceptions"><span class="std std-ref">例外</span></a>章節找到,關於處理例外的資訊可以在<a class="reference internal" href="compound_stmts.html#try"><span class="std std-ref">try 陳述式</span></a>章節找到。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版的變更: </span><a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> 現在允許作為 <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">X</span> <span class="pre">from</span> <span class="pre">Y</span></code> 中的 <code class="docutils literal notranslate"><span class="pre">Y</span></code>。</p>
<p>新增了 <a class="reference internal" href="../library/exceptions.html#BaseException.__suppress_context__" title="BaseException.__suppress_context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__suppress_context__</span></code></a> 屬性以抑制例外情境的自動顯示。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.11 版的變更: </span>如果活動例外的追蹤在 <a class="reference internal" href="compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> 子句中被修改,則後續的 <code class="docutils literal notranslate"><span class="pre">raise</span></code> 陳述式會使用修改後的追蹤重新引發例外。以前,例外會使用它被捕獲時的追蹤重新引發。</p>
</div>
</section>
<section id="the-break-statement">
<span id="break"></span><h2><span class="section-number">7.9. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code> 陳述式<a class="headerlink" href="#the-break-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-30">
<strong id="grammar-token-python-grammar-break_stmt">break_stmt</strong>: <span class="sx">"break"</span>
</pre>
<p><a class="reference internal" href="#break"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code></a> 只能在語法上出現在 <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> 或 <a class="reference internal" href="compound_stmts.html#while"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">while</span></code></a> 迴圈的巢狀結構中,但不能巢狀在該迴圈內的函式或類別定義中。</p>
<p id="index-31">它會終止最近的外層迴圈,如果迴圈有可選的 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">else</span></code> 子句,則會跳過它。</p>
<p>如果 <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> 迴圈被 <a class="reference internal" href="#break"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code></a> 終止,迴圈控制目標會保持其當前值。</p>
<p id="index-32">當 <a class="reference internal" href="#break"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code></a> 將控制權從帶有 <a class="reference internal" href="compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 子句的 <a class="reference internal" href="compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 陳述式中傳出時,該 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code> 子句會在真正離開迴圈之前被執行。</p>
</section>
<section id="the-continue-statement">
<span id="continue"></span><h2><span class="section-number">7.10. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">continue</span></code> 陳述式<a class="headerlink" href="#the-continue-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-33">
<strong id="grammar-token-python-grammar-continue_stmt">continue_stmt</strong>: <span class="sx">"continue"</span>
</pre>
<p><a class="reference internal" href="#continue"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">continue</span></code></a> 在語法上只能出現於 <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> 或 <a class="reference internal" href="compound_stmts.html#while"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">while</span></code></a> 迴圈的巢狀結構中,但不能在該迴圈內的函式或類別定義中。它會繼續執行最近外層迴圈的下一個循環。</p>
<p>當 <a class="reference internal" href="#continue"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">continue</span></code></a> 將控制權從帶有 <a class="reference internal" href="compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 子句的 <a class="reference internal" href="compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 陳述式中傳出時,該 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code> 子句會在真正開始下一個迴圈循環之前被執行。</p>
</section>
<section id="the-import-statement">
<span id="from"></span><span id="import"></span><h2><span class="section-number">7.11. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> 陳述式<a class="headerlink" href="#the-import-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-34">
<strong id="grammar-token-python-grammar-import_stmt">import_stmt</strong>: <span class="sx">"import"</span> <a class="reference internal" href="#grammar-token-python-grammar-module"><code class="xref docutils literal notranslate"><span class="pre">module</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>] (<span class="sx">","</span> <a class="reference internal" href="#grammar-token-python-grammar-module"><code class="xref docutils literal notranslate"><span class="pre">module</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>])*
| <span class="sx">"from"</span> <a class="reference internal" href="#grammar-token-python-grammar-relative_module"><code class="xref docutils literal notranslate"><span class="pre">relative_module</span></code></a> <span class="sx">"import"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>]
(<span class="sx">","</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>])*
| <span class="sx">"from"</span> <a class="reference internal" href="#grammar-token-python-grammar-relative_module"><code class="xref docutils literal notranslate"><span class="pre">relative_module</span></code></a> <span class="sx">"import"</span> <span class="sx">"("</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>]
(<span class="sx">","</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>])* [<span class="sx">","</span>] <span class="sx">")"</span>
| <span class="sx">"from"</span> <a class="reference internal" href="#grammar-token-python-grammar-relative_module"><code class="xref docutils literal notranslate"><span class="pre">relative_module</span></code></a> <span class="sx">"import"</span> <span class="sx">"*"</span>
<strong id="grammar-token-python-grammar-module">module</strong>: (<a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> <span class="sx">"."</span>)* <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>
<strong id="grammar-token-python-grammar-relative_module">relative_module</strong>: <span class="sx">"."</span>* <a class="reference internal" href="#grammar-token-python-grammar-module"><code class="xref docutils literal notranslate"><span class="pre">module</span></code></a> | <span class="sx">"."</span>+
</pre>
<p>基本的 import 陳述式(不含 <a class="reference internal" href="#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> 子句)分兩個步驟執行:</p>
<ol class="arabic simple">
<li><p>找到一個模組,如有必要則載入並初始化它</p></li>
<li><p>define a name or names in the current namespace for the scope where
the <a class="reference internal" href="#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement occurs, just as an assignment statement
would (including <a class="reference internal" href="#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> and <a class="reference internal" href="#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> semantics).</p></li>
</ol>
<p>當陳述式包含多個子句(以逗號分隔)時,這兩個步驟會分別對每個子句執行,就像這些子句被分離成單獨的 import 陳述式一樣。</p>
<p>第一步(尋找和載入模組)的詳細資訊在<a class="reference internal" href="import.html#importsystem"><span class="std std-ref">引入系統</span></a>章節中有更詳細的描述,該章節還描述了可以引入的各種套件和模組型別,以及所有可用於自訂引入系統的掛鉤 (hook)。請注意,此步驟的失敗可能表示找不到模組,<em>或者</em>在初始化模組時發生錯誤,這包括執行模組的程式碼。</p>
<p>如果請求的模組被成功取得,它將透過以下三種方式之一在區域命名空間中可用:</p>
<ul class="simple" id="index-35">
<li><p>如果模組名稱後面跟著 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code>,則 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> 後面的名稱會直接繫結到被引入的模組。</p></li>
<li><p>如果沒有指定其他名稱,且被引入的模組是頂層模組,則模組的名稱會在區域命名空間中繫結為被引入模組的參照</p></li>
<li><p>如果被引入的模組<em>不是</em>頂層模組,則包含該模組的頂層套件的名稱會在區域命名空間中繫結為頂層套件的參照。被引入的模組必須使用其完整的限定名稱來存取,而不是直接存取</p></li>
</ul>
<p id="index-36"><a class="reference internal" href="#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> 形式使用稍微複雜一點的過程:</p>
<ol class="arabic simple">
<li><p>找到 <a class="reference internal" href="#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> 子句中指定的模組,如有必要則載入並初始化它;</p></li>
<li><p>對於 <a class="reference internal" href="#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> 子句中指定的每個識別字:</p>
<ol class="arabic simple">
<li><p>檢查被引入的模組是否有該名稱的屬性</p></li>
<li><p>如果沒有,嘗試引入具有該名稱的子模組,然後再次檢查被引入的模組是否有該屬性</p></li>
<li><p>如果找不到該屬性,則會引發 <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>。</p></li>
<li><p>otherwise, a reference to that value is stored in the current namespace,
using the name in the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> clause if it is present,
otherwise using the attribute name</p></li>
</ol>
</li>
</ol>
<p>範例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">foo</span> <span class="c1"># foo 被引入並在本地繫結</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">foo.bar.baz</span> <span class="c1"># foo、foo.bar 和 foo.bar.baz 被引入,foo 在本地繫結</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">foo.bar.baz</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">fbb</span> <span class="c1"># foo、foo.bar 和 foo.bar.baz 被引入,foo.bar.baz 繫結為 fbb</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">foo.bar</span><span class="w"> </span><span class="kn">import</span> <span class="n">baz</span> <span class="c1"># foo、foo.bar 和 foo.bar.baz 被引入,foo.bar.baz 繫結為 baz</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">foo</span><span class="w"> </span><span class="kn">import</span> <span class="n">attr</span> <span class="c1"># foo 被引入,foo.attr 繫結為 attr</span>
</pre></div>
</div>
<p id="index-37">如果識別字串列被星號(<code class="docutils literal notranslate"><span class="pre">'*'</span></code>)取代,則模組中定義的所有公開名稱都會在 <a class="reference internal" href="#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> 陳述式出現的作用域的區域命名空間中繫結。</p>
<p id="module.__all__"><span id="index-38"></span>模組定義的<em>公開名稱</em>是透過檢查模組命名空間中名為 <code class="docutils literal notranslate"><span class="pre">__all__</span></code> 的變數來決定的;如果有定義,它必須是一個字串序列,這些字串是該模組定義或引入的名稱。包含非 ASCII 字元的名稱必須使用 <a class="reference external" href="https://www.unicode.org/reports/tr15/#Norm_Forms">normalization form</a> NFKC;詳情請參閱 <a class="reference internal" href="lexical_analysis.html#lexical-names-nonascii"><span class="std std-ref">名稱中的非 ASCII 字元</span></a>。<code class="docutils literal notranslate"><span class="pre">__all__</span></code> 中給出的名稱都被視為公開的,且必須存在。如果 <code class="docutils literal notranslate"><span class="pre">__all__</span></code> 沒有定義,公開名稱的集合包括模組命名空間中所有不以底線字元(<code class="docutils literal notranslate"><span class="pre">'_'</span></code>)開頭的名稱。<code class="docutils literal notranslate"><span class="pre">__all__</span></code> 應該包含整個公開 API。這是為了避免意外匯出不屬於 API 的項目(例如在模組內被引入和使用的函式庫模組)。</p>
<p>萬用字元形式的引入——<code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code>——只允許在模組層級使用。嘗試在類別或函式定義中使用它會引發 <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>。</p>
<p id="index-39">在指定要引入的模組時,你不必指定模組的絕對名稱。當一個模組或套件包含在另一個套件中時,可以在同一個頂層套件內進行相對引入,而不必提及套件名稱。透過在 <a class="reference internal" href="#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> 後面的指定模組或套件中使用前導點,你可以指定要向上遍歷當前套件階層結構多高,而不必指定確切的名稱。一個前導點表示進行引入的模組所在的當前套件。兩個點表示向上一個套件層級。三個點表示向上兩個層級,依此類推。因此,如果你從 <code class="docutils literal notranslate"><span class="pre">pkg</span></code> 套件中的模組執行 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">.</span> <span class="pre">import</span> <span class="pre">mod</span></code>,你最終會引入 <code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code>。如果你從 <code class="docutils literal notranslate"><span class="pre">pkg.subpkg1</span></code> 內部執行 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">..subpkg2</span> <span class="pre">import</span> <span class="pre">mod</span></code>,你會引入 <code class="docutils literal notranslate"><span class="pre">pkg.subpkg2.mod</span></code>。相對引入的規格說明包含在<a class="reference internal" href="import.html#relativeimports"><span class="std std-ref">套件相對引入</span></a>章節中。</p>
<p><a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a> 用於支援需要動態決定載入模組的應用程式。</p>
<p class="audit-hook">引發一個附帶引數 <code class="docutils literal notranslate"><span class="pre">module</span></code>、<code class="docutils literal notranslate"><span class="pre">filename</span></code>、<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>、<code class="docutils literal notranslate"><span class="pre">sys.meta_path</span></code>、<code class="docutils literal notranslate"><span class="pre">sys.path_hooks</span></code> 的<a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">稽核事件</span></a> <code class="docutils literal notranslate"><span class="pre">import</span></code>。</p>
<section id="future-statements">
<span id="future"></span><h3><span class="section-number">7.11.1. </span>Future 陳述式<a class="headerlink" href="#future-statements" title="連結到這個標頭">¶</a></h3>
<p id="index-40"><em class="dfn">future 陳述式 (future statement)</em>是給編譯器的指令,表示特定模組應該使用將在 Python 的指定未來版本(該功能成為標準的版本)中可用的語法或語義來編譯。</p>
<p>future 陳述式旨在簡化向 Python 未來版本的遷移,這些版本引入了語言的不相容變更。它允許在該功能成為標準的版本發佈之前,以每個模組為基礎使用新功能。</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-future_stmt">future_stmt</strong>: <span class="sx">"from"</span> <span class="sx">"__future__"</span> <span class="sx">"import"</span> <a class="reference internal" href="#grammar-token-python-grammar-feature"><code class="xref docutils literal notranslate"><span class="pre">feature</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>]
(<span class="sx">","</span> <a class="reference internal" href="#grammar-token-python-grammar-feature"><code class="xref docutils literal notranslate"><span class="pre">feature</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>])*
| <span class="sx">"from"</span> <span class="sx">"__future__"</span> <span class="sx">"import"</span> <span class="sx">"("</span> <a class="reference internal" href="#grammar-token-python-grammar-feature"><code class="xref docutils literal notranslate"><span class="pre">feature</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>]
(<span class="sx">","</span> <a class="reference internal" href="#grammar-token-python-grammar-feature"><code class="xref docutils literal notranslate"><span class="pre">feature</span></code></a> [<span class="sx">"as"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>])* [<span class="sx">","</span>] <span class="sx">")"</span>
<strong id="grammar-token-python-grammar-feature">feature</strong>: <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>
</pre>
<p>future 陳述式必須出現在模組的頂部附近。可以出現在 future 陳述式之前的行只有:</p>
<ul class="simple">
<li><p>模組的文件字串(如果有的話),</p></li>
<li><p>註解,</p></li>
<li><p>空白行,以及</p></li>
<li><p>其他 future 陳述式。</p></li>
</ul>
<p>唯一需要使用 future 陳述式的功能是 <code class="docutils literal notranslate"><span class="pre">annotations</span></code>(請參閱 <span class="target" id="index-59"></span><a class="pep reference external" href="https://peps.python.org/pep-0563/"><strong>PEP 563</strong></a>)。</p>
<p>所有透過 future 陳述式啟用的歷史功能仍被 Python 3 識別。這些功能包括 <code class="docutils literal notranslate"><span class="pre">absolute_import</span></code>、<code class="docutils literal notranslate"><span class="pre">division</span></code>、<code class="docutils literal notranslate"><span class="pre">generators</span></code>、<code class="docutils literal notranslate"><span class="pre">generator_stop</span></code>、<code class="docutils literal notranslate"><span class="pre">unicode_literals</span></code>、<code class="docutils literal notranslate"><span class="pre">print_function</span></code>、<code class="docutils literal notranslate"><span class="pre">nested_scopes</span></code> 和 <code class="docutils literal notranslate"><span class="pre">with_statement</span></code>。它們都是多餘的,因為它們總是被啟用的,只是為了向後相容性而保留。</p>
<p>future 陳述式在編譯時被識別並特別處理:對核心構造語義的變更通常透過產生不同的程式碼來實作。甚至可能出現新功能引入不相容語法(例如新的保留字)的情況,在這種情況下,編譯器可能需要以不同的方式剖析模組。這些決定無法延遲到 runtime 才做出。</p>
<p>對於任何給定的版本,編譯器知道哪些功能名稱已被定義,如果 future 陳述式包含它不認識的功能,則會引發編譯時錯誤。</p>
<p>直接的 runtime 語義與任何 import 陳述式相同:有一個標準模組 <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a>(稍後描述),它會在 future 陳述式執行時以通常的方式被引入。</p>
<p>有趣的 runtime 語義取決於 future 陳述式啟用的特定功能。</p>
<p>請注意,以下陳述式沒有什麼特別的:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">__future__</span> <span class="p">[</span><span class="k">as</span> <span class="n">name</span><span class="p">]</span>
</pre></div>
</div>
<p>這不是 future 陳述式;它是一個普通的 import 陳述式,沒有特殊的語義或語法限制。</p>
<p>在包含 future 陳述式的模組 <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code> 中,透過呼叫內建函式 <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> 和 <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 編譯的程式碼預設會使用與 future 陳述式相關的新語法或語義。這可以透過 <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 的可選引數來控制——詳情請參閱該函式的文件。</p>
<p>在互動式直譯器提示字元下輸入的 future 陳述式會在直譯器工作階段的其餘部分生效。如果直譯器使用 <a class="reference internal" href="../using/cmdline.html#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a> 選項啟動,傳入一個要執行的腳本名稱,且該腳本包含 future 陳述式,則它會在腳本執行後啟動的互動式工作階段中生效。</p>
<div class="admonition seealso">
<p class="admonition-title">也參考</p>
<dl class="simple">
<dt><span class="target" id="index-60"></span><a class="pep reference external" href="https://peps.python.org/pep-0236/"><strong>PEP 236</strong></a> - 回到 __future__</dt><dd><p>__future__ 機制的原始提案。</p>
</dd>
</dl>
</div>
</section>
</section>
<section id="the-global-statement">
<span id="global"></span><h2><span class="section-number">7.12. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式<a class="headerlink" href="#the-global-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-43">
<strong id="grammar-token-python-grammar-global_stmt">global_stmt</strong>: <span class="sx">"global"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> (<span class="sx">","</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>)*
</pre>
<p><a class="reference internal" href="#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 陳述式會使列出的識別字被解釋為全域變數。如果沒有 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code>,就不可能對全域變數進行賦值,儘管自由變數可以在未被宣告為 global 的情況下參照全域變數。</p>
<p><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式適用於整個當前作用域(模組、函式主體或類別定義)。如果在該作用域中,變數在其 global 宣告之前被使用或賦值,則會引發 <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>。</p>
<p>在模組層級,所有變數都是全域的,因此 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式沒有效果。然而,變數仍然不能在其 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 宣告之前被使用或賦值。這個要求在互動式提示符(<a class="reference internal" href="../glossary.html#term-REPL"><span class="xref std std-term">REPL</span></a>)中被放寬。</p>
<p id="index-44"><strong>程式設計師注意事項:</strong><a class="reference internal" href="#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 是給剖析器的指令。它只適用於與 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式同時被剖析的程式碼。特別是,包含在提供給內建 <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> 函式的字串或程式碼物件中的 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式不會影響<em>包含</em>該函式呼叫的程式碼區塊,而且這種字串中包含的程式碼不受包含函式呼叫的程式碼中的 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式影響。同樣的規則也適用於 <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 和 <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 函式。</p>
</section>
<section id="the-nonlocal-statement">
<span id="nonlocal"></span><h2><span class="section-number">7.13. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code> 陳述式<a class="headerlink" href="#the-nonlocal-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-45">
<strong id="grammar-token-python-grammar-nonlocal_stmt">nonlocal_stmt</strong>: <span class="sx">"nonlocal"</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> (<span class="sx">","</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>)*
</pre>
<p>當函式或類別的定義巢狀(封閉)在其他函式的定義內時,它的非區域作用域就是外層函式的區域作用域。<a class="reference internal" href="#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 陳述式會使列出的識別字參照先前在非區域作用域中繫結的名稱。它允許被封裝的程式碼重新繫結這些非區域識別字。如果一個名稱在多個非區域作用域中被繫結,則使用最近的繫結。如果一個名稱在任何非區域作用域中都沒有被繫結,或者沒有非區域作用域,則會引發 <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>。</p>
<p><a class="reference internal" href="#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 陳述式適用於函式或類別主體的整個作用域。如果在該作用域中,變數在其 nonlocal 宣告之前被使用或賦值,則會引發 <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>。</p>
<div class="admonition seealso">
<p class="admonition-title">也參考</p>
<dl class="simple">
<dt><span class="target" id="index-61"></span><a class="pep reference external" href="https://peps.python.org/pep-3104/"><strong>PEP 3104</strong></a> - 存取外層作用域中的名稱</dt><dd><p><a class="reference internal" href="#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 陳述式的規格說明。</p>
</dd>
</dl>
</div>
<p><strong>程式設計師注意事項:</strong><a class="reference internal" href="#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 是給剖析器的指令,只適用於與它一起被剖析的程式碼。請參閱 <a class="reference internal" href="#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 陳述式的注意事項。</p>
</section>
<section id="the-type-statement">
<span id="type"></span><h2><span class="section-number">7.14. </span><code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code> 陳述式<a class="headerlink" href="#the-type-statement" title="連結到這個標頭">¶</a></h2>
<pre class="highlight" id="index-47">
<strong id="grammar-token-python-grammar-type_stmt">type_stmt</strong>: <span class="sx">'type'</span> <a class="reference internal" href="lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> [<a class="reference internal" href="compound_stmts.html#grammar-token-python-grammar-type_params"><code class="xref docutils literal notranslate"><span class="pre">type_params</span></code></a>] <span class="sx">"="</span> <a class="reference internal" href="expressions.html#grammar-token-python-grammar-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
</pre>
<p><code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code> 陳述式宣告一個型別別名,它是 <a class="reference internal" href="../library/typing.html#typing.TypeAliasType" title="typing.TypeAliasType"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.TypeAliasType</span></code></a> 的實例。</p>
<p>例如以下陳述式建立了一個型別別名:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">type</span> <span class="n">Point</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]</span>
</pre></div>
</div>
<p>這段程式碼大致等價於:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">annotation</span><span class="o">-</span><span class="k">def</span><span class="w"> </span><span class="nf">VALUE_OF_Point</span><span class="p">():</span>
<span class="k">return</span> <span class="nb">tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]</span>
<span class="n">Point</span> <span class="o">=</span> <span class="n">typing</span><span class="o">.</span><span class="n">TypeAliasType</span><span class="p">(</span><span class="s2">"Point"</span><span class="p">,</span> <span class="n">VALUE_OF_Point</span><span class="p">())</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">annotation-def</span></code> 表示一個<a class="reference internal" href="executionmodel.html#annotation-scopes"><span class="std std-ref">註釋作用域</span></a>,它的行為大部分像函式,但有一些小差異。</p>
<p>型別別名的值在註釋作用域中被求值。它不是在型別別名建立時求值,而是只有在透過型別別名的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">__value__</span></code> 屬性存取該值時才求值(請參閱<a class="reference internal" href="executionmodel.html#lazy-evaluation"><span class="std std-ref">Lazy evaluation</span></a>)。這允許型別別名參照尚未定義的名稱。</p>
<p>型別別名可以透過在名稱後面新增<a class="reference internal" href="compound_stmts.html#type-params"><span class="std std-ref">型別參數串列</span></a>來使其成為泛型。更多資訊請參閱<a class="reference internal" href="compound_stmts.html#generic-type-aliases"><span class="std std-ref">Generic type aliases</span></a>。</p>
<p><code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code> 是一個<a class="reference internal" href="lexical_analysis.html#soft-keywords"><span class="std std-ref">軟關鍵字</span></a>。</p>
<div class="versionadded">
<p><span class="versionmodified added">在 3.12 版被加入.</span></p>
</div>
<div class="admonition seealso">
<p class="admonition-title">也參考</p>
<dl class="simple">
<dt><span class="target" id="index-62"></span><a class="pep reference external" href="https://peps.python.org/pep-0695/"><strong>PEP 695</strong></a> - 型別參數語法</dt><dd><p>引入了 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code> 陳述式以及泛型類別和函式的語法。</p>
</dd>
</dl>
</div>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="../contents.html">目錄</a></h3>
<ul>
<li><a class="reference internal" href="#">7. 簡單陳述式</a><ul>
<li><a class="reference internal" href="#expression-statements">7.1. 運算式陳述式</a></li>
<li><a class="reference internal" href="#assignment-statements">7.2. 賦值陳述式</a><ul>
<li><a class="reference internal" href="#augmented-assignment-statements">7.2.1. 擴增賦值陳述式</a></li>
<li><a class="reference internal" href="#annotated-assignment-statements">7.2.2. 註釋賦值陳述式</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-assert-statement">7.3. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">assert</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-pass-statement">7.4. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">pass</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-del-statement">7.5. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-return-statement">7.6. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-yield-statement">7.7. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-raise-statement">7.8. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-break-statement">7.9. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-continue-statement">7.10. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">continue</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-import-statement">7.11. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> 陳述式</a><ul>
<li><a class="reference internal" href="#future-statements">7.11.1. Future 陳述式</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-global-statement">7.12. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-nonlocal-statement">7.13. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code> 陳述式</a></li>
<li><a class="reference internal" href="#the-type-statement">7.14. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code> 陳述式</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>上個主題</h4>
<p class="topless"><a href="expressions.html"
title="上一章"><span class="section-number">6. </span>運算式</a></p>
</div>
<div>
<h4>下個主題</h4>
<p class="topless"><a href="compound_stmts.html"
title="下一章"><span class="section-number">8. </span>複合陳述式</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "reference/simple_stmts.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>此頁面</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">回報錯誤</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">改進此頁面</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/reference/simple_stmts.rst?plain=1"
rel="nofollow">顯示原始碼
</a>
</li>
<li>
<a href="https://github.com/python/python-docs-zh-TW/blob/3.14/reference/simple_stmts.po?plain=1"
rel="nofollow">顯示翻譯原始碼</a>
</li>
</ul>
</div>
</div>
<div id="sidebarbutton" title="收合側邊欄">
<span>«</span>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>導航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="總索引"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="compound_stmts.html" title="8. 複合陳述式"
>下一頁</a> |</li>
<li class="right" >
<a href="expressions.html" title="6. 運算式"
>上一頁</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.14.5 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 語言參考手冊</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">7. </span>簡單陳述式</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="search" name="q" id="search-box">
<input type="submit" value="前往">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
主題
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>自動</option>
<option value="light">淺色模式</option>
<option value="dark">深色模式</option>
</select>
</label> |</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">版權所有</a> 2001 Python Software Foundation.
<br>
此頁面採用 Python 軟體基金會授權條款第 2 版。
<br>
文件中的範例、應用技巧與其他程式碼額外採用了 Zero Clause BSD 授權條款。
<br>
更多訊息請見<a href="/license.html">歷史與授權條款</a>。<br>
<br>
Python 軟體基金會是一家非營利法人。
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
<br>
<br>
最後更新於 5月 14, 2026 (19:12 UTC)。
<a href="/bugs.html">發現 bug</a>?
<br>
以 <a href="https://www.sphinx-doc.org/">Sphinx</a>8.2.3建立。
</div>
</body>
</html>