-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathprogramming.po
4529 lines (4021 loc) · 164 KB
/
programming.po
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2001-2025, Python Software Foundation
# This file is distributed under the same license as the Python package.
#
# Translators:
# jerrychen <[email protected]>, 2016
# Ching-Lung Chuang, 2015
# hsiao yi <[email protected]>, 2016
msgid ""
msgstr ""
"Project-Id-Version: Python 3.13\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-19 00:15+0000\n"
"PO-Revision-Date: 2024-04-25 14:17+0800\n"
"Last-Translator: KNChiu <[email protected]>\n"
"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-"
"tw)\n"
"Language: zh_TW\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"X-Generator: Poedit 3.4.2\n"
#: ../../faq/programming.rst:5
msgid "Programming FAQ"
msgstr "程式開發常見問答集"
#: ../../faq/programming.rst:8
msgid "Contents"
msgstr "目錄"
#: ../../faq/programming.rst:12
msgid "General Questions"
msgstr "常見問題"
#: ../../faq/programming.rst:15
msgid ""
"Is there a source code level debugger with breakpoints, single-stepping, "
"etc.?"
msgstr "是否有可以使用在程式碼階段,具有中斷點,步驟執行等功能的除錯器?"
#: ../../faq/programming.rst:17 ../../faq/programming.rst:58
msgid "Yes."
msgstr "有的。"
#: ../../faq/programming.rst:19
msgid ""
"Several debuggers for Python are described below, and the built-in "
"function :func:`breakpoint` allows you to drop into any of them."
msgstr ""
"下面描述了幾個 Python 除錯器,內建函式 :func:`breakpoint` 允許你進入其中任何"
"一個。"
#: ../../faq/programming.rst:22
msgid ""
"The pdb module is a simple but adequate console-mode debugger for Python. It "
"is part of the standard Python library, and is :mod:`documented in the "
"Library Reference Manual <pdb>`. You can also write your own debugger by "
"using the code for pdb as an example."
msgstr ""
"pdb 模組是一個簡單但足夠的 Python 控制台模式除錯器。它是標準 Python 函式庫的"
"一部分,並\\ :mod:`記錄在函式庫參考手冊 <pdb>`\\ 中。你也可以參考 pdb 的程式"
"碼作為範例來編寫自己的除錯器。"
#: ../../faq/programming.rst:27
msgid ""
"The IDLE interactive development environment, which is part of the standard "
"Python distribution (normally available as `Tools/scripts/idle3 <https://"
"github.com/python/cpython/blob/main/Tools/scripts/idle3>`_), includes a "
"graphical debugger."
msgstr ""
"IDLE 交互式開發環境,它是標準 Python 發行版的一部分(通常作為 `Tools/scripts/"
"idle3 <https://github.com/python/cpython/blob/main/Tools/scripts/idle3>`_ 提"
"供),包括一個圖形除錯器。"
#: ../../faq/programming.rst:32
msgid ""
"PythonWin is a Python IDE that includes a GUI debugger based on pdb. The "
"PythonWin debugger colors breakpoints and has quite a few cool features such "
"as debugging non-PythonWin programs. PythonWin is available as part of "
"`pywin32 <https://github.com/mhammond/pywin32>`_ project and as a part of "
"the `ActivePython <https://www.activestate.com/products/python/>`_ "
"distribution."
msgstr ""
"PythonWin 是一個 Python IDE,它包含一個基於 pdb 的 GUI 除錯器。 PythonWin 除"
"錯器為斷點著色並具有許多很酷的功能,例如除錯非 PythonWin 程式。 PythonWin 作"
"為 `pywin32 <https://github.com/mhammond/pywin32>`_ 專案的一部分和作為 "
"`ActivePython <https://www.activestate.com/products/python/>`_ 的一部分發佈。"
#: ../../faq/programming.rst:39
msgid ""
"`Eric <https://eric-ide.python-projects.org/>`_ is an IDE built on PyQt and "
"the Scintilla editing component."
msgstr ""
"`Eric <https://eric-ide.python-projects.org/>`_ 是一個基於 PyQt 和 Scintilla "
"編輯元件所建構的 IDE。"
#: ../../faq/programming.rst:42
msgid ""
"`trepan3k <https://github.com/rocky/python3-trepan/>`_ is a gdb-like "
"debugger."
msgstr ""
"`trepan3k <https://github.com/rocky/python3-trepan/>`_ 是一個類似 gdb 的除錯"
"器。"
#: ../../faq/programming.rst:44
msgid ""
"`Visual Studio Code <https://code.visualstudio.com/>`_ is an IDE with "
"debugging tools that integrates with version-control software."
msgstr ""
"`Visual Studio Code <https://code.visualstudio.com/>`_ 是一個整合了版本控制軟"
"體與除錯工具的 IDE。"
#: ../../faq/programming.rst:47
msgid ""
"There are a number of commercial Python IDEs that include graphical "
"debuggers. They include:"
msgstr "有數個商業化Python整合化開發工具包含圖形除錯功能。這些包含:"
#: ../../faq/programming.rst:50
msgid "`Wing IDE <https://wingware.com/>`_"
msgstr "`Wing IDE <https://wingware.com/>`_"
#: ../../faq/programming.rst:51
msgid "`Komodo IDE <https://www.activestate.com/products/komodo-ide/>`_"
msgstr "`Komodo IDE <https://www.activestate.com/products/komodo-ide/>`_"
#: ../../faq/programming.rst:52
msgid "`PyCharm <https://www.jetbrains.com/pycharm/>`_"
msgstr "`PyCharm <https://www.jetbrains.com/pycharm/>`_"
#: ../../faq/programming.rst:56
msgid "Are there tools to help find bugs or perform static analysis?"
msgstr "有沒有工具能夠幫忙找 bug 或執行靜態分析?"
#: ../../faq/programming.rst:60
msgid ""
"`Pylint <https://pylint.pycqa.org/en/latest/index.html>`_ and `Pyflakes "
"<https://github.com/PyCQA/pyflakes>`_ do basic checking that will help you "
"catch bugs sooner."
msgstr ""
"`Pylint <https://pylint.pycqa.org/en/latest/index.html>`_ 和 `Pyflakes "
"<https://github.com/PyCQA/pyflakes>`_ 進行基本檢查以幫助你儘早抓出錯誤。"
#: ../../faq/programming.rst:64
msgid ""
"Static type checkers such as `Mypy <https://mypy-lang.org/>`_, `Pyre "
"<https://pyre-check.org/>`_, and `Pytype <https://github.com/google/"
"pytype>`_ can check type hints in Python source code."
msgstr ""
"靜態型別檢查器,例如 `Mypy <https://mypy-lang.org/>`_、`Pyre <https://pyre-"
"check.org/>`_ 和 `Pytype <https://github.com/google/pytype>`_ 可以檢查 "
"Python 原始碼中的型別提示。"
#: ../../faq/programming.rst:73
msgid "How can I create a stand-alone binary from a Python script?"
msgstr "如何從 Python 腳本建立獨立的二進位檔案?"
#: ../../faq/programming.rst:75
#, fuzzy
msgid ""
"You don't need the ability to compile Python to C code if all you want is a "
"stand-alone program that users can download and run without having to "
"install the Python distribution first. There are a number of tools that "
"determine the set of modules required by a program and bind these modules "
"together with a Python binary to produce a single executable."
msgstr ""
"如果你想要的只是一個使用者可以下載並運行而無需先安裝 Python 發行版的獨立程"
"式,則不需要將 Python 編譯為 C 程式碼的能力。有許多工具可以確定程式所需的模組"
"集,並將這些模組與 Python 二進製檔案綁定在一起以生成單個可執行檔案。"
#: ../../faq/programming.rst:81
#, fuzzy
msgid ""
"One is to use the freeze tool, which is included in the Python source tree "
"as `Tools/freeze <https://github.com/python/cpython/tree/main/Tools/"
"freeze>`_. It converts Python byte code to C arrays; with a C compiler you "
"can embed all your modules into a new program, which is then linked with the "
"standard Python modules."
msgstr ""
"一種是使用凍結工具,它作為 `Tools/freeze <https://github.com/python/cpython/"
"tree/main/Tools/freeze>`_ 包含在 Python 源程式碼樹中。它將 Python 位元組碼轉"
"換為 C 數組;使用 C 編譯器,你可以將所有模組嵌入到一個新程式中,然後將其與標"
"準 Python 模組鏈接。"
#: ../../faq/programming.rst:87
#, fuzzy
msgid ""
"It works by scanning your source recursively for import statements (in both "
"forms) and looking for the modules in the standard Python path as well as in "
"the source directory (for built-in modules). It then turns the bytecode for "
"modules written in Python into C code (array initializers that can be turned "
"into code objects using the marshal module) and creates a custom-made config "
"file that only contains those built-in modules which are actually used in "
"the program. It then compiles the generated C code and links it with the "
"rest of the Python interpreter to form a self-contained binary which acts "
"exactly like your script."
msgstr ""
"它的工作原理是遞迴地掃描你的源程式碼以查詢引入陳述式(兩種形式)並在標準 "
"Python 路徑和源目錄(對於內建模組)中查詢模組。然後它將用 Python 編寫的模組的"
"位元組碼轉換為 C 程式碼(數組初始化器可以使用 marshal 模組轉換為程式碼物件)"
"並建立一個定制的配置檔案,該檔案僅包含那些實際使用的內建模組程式。然後它編譯"
"生成的 C 程式碼並將其與 Python 直譯器的其餘部分鏈接以形成一個獨立的二進製檔"
"案,其行為與你的腳本完全一樣。"
#: ../../faq/programming.rst:96
msgid ""
"The following packages can help with the creation of console and GUI "
"executables:"
msgstr "以下套件可以幫助建立 console 和 GUI 可執行檔案:"
#: ../../faq/programming.rst:99
msgid "`Nuitka <https://nuitka.net/>`_ (Cross-platform)"
msgstr "`Nuitka <https://nuitka.net/>`_\\ (跨平台)"
#: ../../faq/programming.rst:100
msgid "`PyInstaller <https://pyinstaller.org/>`_ (Cross-platform)"
msgstr "`PyInstaller <https://pyinstaller.org/>`_\\ (跨平台)"
#: ../../faq/programming.rst:101
msgid ""
"`PyOxidizer <https://pyoxidizer.readthedocs.io/en/stable/>`_ (Cross-platform)"
msgstr ""
"`PyOxidizer <https://pyoxidizer.readthedocs.io/en/stable/>`_\\ (跨平台)"
#: ../../faq/programming.rst:102
msgid ""
"`cx_Freeze <https://marcelotduarte.github.io/cx_Freeze/>`_ (Cross-platform)"
msgstr ""
"`cx_Freeze <https://marcelotduarte.github.io/cx_Freeze/>`_\\ (跨平台)"
#: ../../faq/programming.rst:103
msgid "`py2app <https://github.com/ronaldoussoren/py2app>`_ (macOS only)"
msgstr "`py2app <https://github.com/ronaldoussoren/py2app>`_\\ (僅限 macOS)"
#: ../../faq/programming.rst:104
msgid "`py2exe <https://www.py2exe.org/>`_ (Windows only)"
msgstr "`py2exe <https://www.py2exe.org/>`_\\ (僅限 Windows)"
#: ../../faq/programming.rst:107
msgid "Are there coding standards or a style guide for Python programs?"
msgstr "Python 程式碼是否有編碼標準或風格指南?"
#: ../../faq/programming.rst:109
msgid ""
"Yes. The coding style required for standard library modules is documented "
"as :pep:`8`."
msgstr "是的。標準函式庫模組所需的編碼風格稱為 :pep:`8`。"
#: ../../faq/programming.rst:114
msgid "Core Language"
msgstr "核心語言"
#: ../../faq/programming.rst:119
msgid "Why am I getting an UnboundLocalError when the variable has a value?"
msgstr "為什麼當變數有值時,我仍得到錯誤訊息 UnboundLocalError?"
#: ../../faq/programming.rst:121
#, fuzzy
msgid ""
"It can be a surprise to get the :exc:`UnboundLocalError` in previously "
"working code when it is modified by adding an assignment statement somewhere "
"in the body of a function."
msgstr ""
"當透過在函式主體的某處新增賦值陳述式修改以前的工作程式碼時,在以前的工作程式"
"碼中得到 :exc:`UnboundLocalError` 可能會令人驚訝。"
#: ../../faq/programming.rst:125
msgid "This code:"
msgstr "這段程式碼:"
#: ../../faq/programming.rst:134
msgid "works, but this code:"
msgstr "可以執行,但是這段程式:"
#: ../../faq/programming.rst:141
msgid "results in an :exc:`!UnboundLocalError`:"
msgstr "導致 :exc:`!UnboundLocalError`:"
#: ../../faq/programming.rst:148
#, fuzzy
msgid ""
"This is because when you make an assignment to a variable in a scope, that "
"variable becomes local to that scope and shadows any similarly named "
"variable in the outer scope. Since the last statement in foo assigns a new "
"value to ``x``, the compiler recognizes it as a local variable. "
"Consequently when the earlier ``print(x)`` attempts to print the "
"uninitialized local variable and an error results."
msgstr ""
"這是因為當你對作用域中的變數進行賦值時,該變數將成為該作用域的局部變數,並隱"
"藏外部作用域中任何類似命名的變數。由於 foo 中的最後一條陳述式為 ``x`` 分配了"
"一個新值,因此編譯器將其識別為局部變數。因此,當較早的 ``print(x)`` 嘗試印出"
"未初始化的局部變數並產生錯誤時。"
#: ../../faq/programming.rst:155
msgid ""
"In the example above you can access the outer scope variable by declaring it "
"global:"
msgstr "在上面的範例中,你可以透過將其聲明為全域變數來存取外部範圍變數:"
#: ../../faq/programming.rst:167
#, fuzzy
msgid ""
"This explicit declaration is required in order to remind you that (unlike "
"the superficially analogous situation with class and instance variables) you "
"are actually modifying the value of the variable in the outer scope:"
msgstr ""
"需要此顯式聲明是為了提醒你(與類別和實例變數表面上類似的情況不同)你實際上是"
"在外部範圍內修改變數的值:"
#: ../../faq/programming.rst:174
msgid ""
"You can do a similar thing in a nested scope using the :keyword:`nonlocal` "
"keyword:"
msgstr "你可以使用 :keyword:`nonlocal` 關鍵字在巢狀作用域內做類似的事情:"
#: ../../faq/programming.rst:192
msgid "What are the rules for local and global variables in Python?"
msgstr "Python 的區域變數和全域變數有什麼規則?"
#: ../../faq/programming.rst:194
#, fuzzy
msgid ""
"In Python, variables that are only referenced inside a function are "
"implicitly global. If a variable is assigned a value anywhere within the "
"function's body, it's assumed to be a local unless explicitly declared as "
"global."
msgstr ""
"在 Python 中,僅在函式內部參照的變數是隱式全域變數。如果一個變數在函式體內的"
"任何地方被賦值,除非明確聲明為全域變數,否則它被假定為局部變數。"
#: ../../faq/programming.rst:198
#, fuzzy
msgid ""
"Though a bit surprising at first, a moment's consideration explains this. "
"On one hand, requiring :keyword:`global` for assigned variables provides a "
"bar against unintended side-effects. On the other hand, if ``global`` was "
"required for all global references, you'd be using ``global`` all the time. "
"You'd have to declare as global every reference to a built-in function or to "
"a component of an imported module. This clutter would defeat the usefulness "
"of the ``global`` declaration for identifying side-effects."
msgstr ""
"雖然起初有點令人驚訝,但稍加考慮就可以解釋這一點。一方面,要"
"求 :keyword:`global` 分配的變數可以防止意外的副作用。另一方面,如果所有全域參"
"照都需要 ``global``,那麼你將一直使用 ``global``。你必須將對內建函式或引入模"
"組的組件的每個參照聲明為全域。這種混亂會破壞用於識別副作用的 ``global`` 聲明"
"的有用性。"
#: ../../faq/programming.rst:208
msgid ""
"Why do lambdas defined in a loop with different values all return the same "
"result?"
msgstr "為什麼以不同的值在迴圈中定義的 lambda 都回傳相同的結果?"
#: ../../faq/programming.rst:210
msgid ""
"Assume you use a for loop to define a few different lambdas (or even plain "
"functions), e.g.::"
msgstr ""
"假設你使用 for 迴圈來定義幾個不同的 lambda(甚至是普通函式),例如: ::"
#: ../../faq/programming.rst:213
msgid ""
">>> squares = []\n"
">>> for x in range(5):\n"
"... squares.append(lambda: x**2)"
msgstr ""
">>> squares = []\n"
">>> for x in range(5):\n"
"... squares.append(lambda: x**2)"
#: ../../faq/programming.rst:217
msgid ""
"This gives you a list that contains 5 lambdas that calculate ``x**2``. You "
"might expect that, when called, they would return, respectively, ``0``, "
"``1``, ``4``, ``9``, and ``16``. However, when you actually try you will "
"see that they all return ``16``::"
msgstr ""
"這會提供一個包含五個計算 ``x**2`` 的 lambda 串列。你可能會預期在呼叫它時,它"
"們會分別回傳 ``0``、``1``、``4``、``9`` 和 ``16``,然而當你實際嘗試你會發現它"
"們都回傳 ``16``: ::"
#: ../../faq/programming.rst:222
msgid ""
">>> squares[2]()\n"
"16\n"
">>> squares[4]()\n"
"16"
msgstr ""
">>> squares[2]()\n"
"16\n"
">>> squares[4]()\n"
"16"
#: ../../faq/programming.rst:227
msgid ""
"This happens because ``x`` is not local to the lambdas, but is defined in "
"the outer scope, and it is accessed when the lambda is called --- not when "
"it is defined. At the end of the loop, the value of ``x`` is ``4``, so all "
"the functions now return ``4**2``, i.e. ``16``. You can also verify this by "
"changing the value of ``x`` and see how the results of the lambdas change::"
msgstr ""
"發生這種情況是因為 ``x`` 不是 lambda 的局部變數,而是在外部作用域中定義的,且"
"是在呼叫 lambda 時才會存取它,並非於定義時就會存取。在迴圈結束時,``x`` 的值"
"為 ``4``,因此所有函式都回傳 ``4**2``,即為 ``16``。你還可以透過更改 ``x`` 的"
"值來驗證這一點,並查看 lambda 運算式的結果如何變化: ::"
#: ../../faq/programming.rst:233
msgid ""
">>> x = 8\n"
">>> squares[2]()\n"
"64"
msgstr ""
">>> x = 8\n"
">>> squares[2]()\n"
"64"
#: ../../faq/programming.rst:237
msgid ""
"In order to avoid this, you need to save the values in variables local to "
"the lambdas, so that they don't rely on the value of the global ``x``::"
msgstr ""
"為了避免這種情況,你需要將值保存在 lambda 的局部變數中,這樣它們就不會依賴於"
"全域 ``x`` 的值: ::"
#: ../../faq/programming.rst:240
msgid ""
">>> squares = []\n"
">>> for x in range(5):\n"
"... squares.append(lambda n=x: n**2)"
msgstr ""
">>> squares = []\n"
">>> for x in range(5):\n"
"... squares.append(lambda n=x: n**2)"
#: ../../faq/programming.rst:244
#, fuzzy
msgid ""
"Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed "
"when the lambda is defined so that it has the same value that ``x`` had at "
"that point in the loop. This means that the value of ``n`` will be ``0`` in "
"the first lambda, ``1`` in the second, ``2`` in the third, and so on. "
"Therefore each lambda will now return the correct result::"
msgstr ""
"在這裡,``n=x`` 建立了一個新變數 ``n`` 局部於 lambda 並在定義 lambda 時計算,"
"因此它具有與 ``x`` 在迴圈中的那個點相同的值。這意味著 ``n`` 的值在第一個 "
"lambda 中為 ``0`` ,在第二個中為 ``1`` ,在第三個中為 ``2`` ,依此類推。因此"
"每個 lambda 現在將回傳正確的結果: ::"
#: ../../faq/programming.rst:250
msgid ""
">>> squares[2]()\n"
"4\n"
">>> squares[4]()\n"
"16"
msgstr ""
">>> squares[2]()\n"
"4\n"
">>> squares[4]()\n"
"16"
#: ../../faq/programming.rst:255
msgid ""
"Note that this behaviour is not peculiar to lambdas, but applies to regular "
"functions too."
msgstr "請注意,此行為並非 lambda 所特有,也適用於常規函式。"
#: ../../faq/programming.rst:260
msgid "How do I share global variables across modules?"
msgstr "如何跨模組共享全域變數?"
#: ../../faq/programming.rst:262
#, fuzzy
msgid ""
"The canonical way to share information across modules within a single "
"program is to create a special module (often called config or cfg). Just "
"import the config module in all modules of your application; the module then "
"becomes available as a global name. Because there is only one instance of "
"each module, any changes made to the module object get reflected "
"everywhere. For example:"
msgstr ""
"在單個程式中跨模組共享資訊的規範方法是建立一個特殊模組(通常稱為 config 或 "
"cfg)。只需在應用程式的所有模組中引入配置模組;然後該模組可作為全域名稱使用。"
"因為每個模組只有一個實例,所以對模組物件所做的任何更改都會在各處反映出來。例"
"如:"
#: ../../faq/programming.rst:268
msgid "config.py::"
msgstr "config.py: ::"
#: ../../faq/programming.rst:270
msgid "x = 0 # Default value of the 'x' configuration setting"
msgstr "x = 0 # 'x' 配置設定的預設值"
#: ../../faq/programming.rst:272
msgid "mod.py::"
msgstr "mod.py: ::"
#: ../../faq/programming.rst:274
msgid ""
"import config\n"
"config.x = 1"
msgstr ""
"import config\n"
"config.x = 1"
#: ../../faq/programming.rst:277
msgid "main.py::"
msgstr "main.py: ::"
#: ../../faq/programming.rst:279
msgid ""
"import config\n"
"import mod\n"
"print(config.x)"
msgstr ""
"import config\n"
"import mod\n"
"print(config.x)"
#: ../../faq/programming.rst:283
msgid ""
"Note that using a module is also the basis for implementing the singleton "
"design pattern, for the same reason."
msgstr "請注意,出於同樣的原因,使用模組也是實作單例設計模式的基礎。"
#: ../../faq/programming.rst:288
msgid "What are the \"best practices\" for using import in a module?"
msgstr "在模組中使用 import 的「最佳實踐」有哪些?"
#: ../../faq/programming.rst:290
#, fuzzy
msgid ""
"In general, don't use ``from modulename import *``. Doing so clutters the "
"importer's namespace, and makes it much harder for linters to detect "
"undefined names."
msgstr ""
"一般來說,不要使用``from modulename import *``。這樣做會使引入器的命名空間混"
"亂,並使 linters 更難檢測未定義的名稱。"
#: ../../faq/programming.rst:294
#, fuzzy
msgid ""
"Import modules at the top of a file. Doing so makes it clear what other "
"modules your code requires and avoids questions of whether the module name "
"is in scope. Using one import per line makes it easy to add and delete "
"module imports, but using multiple imports per line uses less screen space."
msgstr ""
"在檔案頂部引入模組。這樣做可以明確你的程式碼需要哪些其他模組,並避免模組名稱"
"是否在範圍內的問題。每行使用一個引入可以輕鬆新增和刪除模組引入,但每行使用多"
"個引入會佔用更少的屏幕空間。"
#: ../../faq/programming.rst:299
#, fuzzy
msgid "It's good practice if you import modules in the following order:"
msgstr "如果你按以下順序引入模組,這是一個很好的做法:"
#: ../../faq/programming.rst:301
msgid ""
"standard library modules -- "
"e.g. :mod:`sys`, :mod:`os`, :mod:`argparse`, :mod:`re`"
msgstr ""
"標準函式庫模組 —— 例如 :mod:`sys`、:mod:`os`、:mod:`argparse`、:mod:`re`"
#: ../../faq/programming.rst:302
#, fuzzy
msgid ""
"third-party library modules (anything installed in Python's site-packages "
"directory) -- e.g. :mod:`!dateutil`, :mod:`!requests`, :mod:`!PIL.Image`"
msgstr ""
"第三方函式庫模組(任何安裝在 Python 的站點包目錄中的模組)——例如:mod:`!"
"dateutil`, :mod:`!requests`, :mod:`!PIL.Image`"
#: ../../faq/programming.rst:304
#, fuzzy
msgid "locally developed modules"
msgstr "本地開發的模組"
#: ../../faq/programming.rst:306
#, fuzzy
msgid ""
"It is sometimes necessary to move imports to a function or class to avoid "
"problems with circular imports. Gordon McMillan says:"
msgstr "有時需要將引入移動到函式或類別以避免循環引入的問題。戈登麥克米蘭 說:"
#: ../../faq/programming.rst:309
#, fuzzy
msgid ""
"Circular imports are fine where both modules use the \"import <module>\" "
"form of import. They fail when the 2nd module wants to grab a name out of "
"the first (\"from module import name\") and the import is at the top level. "
"That's because names in the 1st are not yet available, because the first "
"module is busy importing the 2nd."
msgstr ""
"在兩個模組都使用 \"import <module>\" 引入形式的情況下,循環引入很好。當第二個"
"模組想要從第一個模組中獲取一個名稱( \"from module import name\" )並且引入位"
"於頂層時,它們會失敗。那是因為 1st 中的名稱尚不可用,因為第一個模組正忙於導"
"入 2nd。"
#: ../../faq/programming.rst:315
#, fuzzy
msgid ""
"In this case, if the second module is only used in one function, then the "
"import can easily be moved into that function. By the time the import is "
"called, the first module will have finished initializing, and the second "
"module can do its import."
msgstr ""
"在這種情況下,如果第二個模組只在一個函式中使用,那麼引入可以很容易地移到那個"
"函式中。在呼叫引入時,第一個模組將完成初始化,第二個模組可以進行引入。"
#: ../../faq/programming.rst:320
#, fuzzy
msgid ""
"It may also be necessary to move imports out of the top level of code if "
"some of the modules are platform-specific. In that case, it may not even be "
"possible to import all of the modules at the top of the file. In this case, "
"importing the correct modules in the corresponding platform-specific code is "
"a good option."
msgstr ""
"如果某些模組是特定於平台的,則可能還需要將引入移出程式碼的頂層。在這種情況"
"下,甚至可能無法引入檔案頂部的所有模組。在這種情況下,在相應的特定於平台的程"
"式碼中引入正確的模組是一個不錯的選擇。"
#: ../../faq/programming.rst:325
#, fuzzy
msgid ""
"Only move imports into a local scope, such as inside a function definition, "
"if it's necessary to solve a problem such as avoiding a circular import or "
"are trying to reduce the initialization time of a module. This technique is "
"especially helpful if many of the imports are unnecessary depending on how "
"the program executes. You may also want to move imports into a function if "
"the modules are only ever used in that function. Note that loading a module "
"the first time may be expensive because of the one time initialization of "
"the module, but loading a module multiple times is virtually free, costing "
"only a couple of dictionary lookups. Even if the module name has gone out "
"of scope, the module is probably available in :data:`sys.modules`."
msgstr ""
"如果有必要解決諸如避免循環引入之類的問題或試圖減少模組的初始化時間,則僅將導"
"入移動到局部範圍內,例如在函式定義內。如果根據程式的執行方式,許多引入是不必"
"要的,則此技術特別有用。如果模組僅在該函式中使用,你可能還想將引入移動到該函"
"式中。請注意,由於模組的一次性初始化,第一次載入模組可能很昂貴,但多次載入模"
"組實際上是免費的,只需幾次字典查詢。即使模組名稱超出範圍,該模組也可能"
"在 :data:`sys.modules` 中可用。"
#: ../../faq/programming.rst:338
#, fuzzy
msgid "Why are default values shared between objects?"
msgstr "為什麼物件之間共享預設值?"
#: ../../faq/programming.rst:340
#, fuzzy
msgid ""
"This type of bug commonly bites neophyte programmers. Consider this "
"function::"
msgstr "這種型別的錯誤通常會困擾新手程式員。考慮這個功能: ::"
#: ../../faq/programming.rst:342
msgid ""
"def foo(mydict={}): # Danger: shared reference to one dict for all calls\n"
" ... compute something ...\n"
" mydict[key] = value\n"
" return mydict"
msgstr ""
#: ../../faq/programming.rst:347
#, fuzzy
msgid ""
"The first time you call this function, ``mydict`` contains a single item. "
"The second time, ``mydict`` contains two items because when ``foo()`` begins "
"executing, ``mydict`` starts out with an item already in it."
msgstr ""
"第一次呼叫此函式時, ``mydict`` 包含一個項目。第二次,``mydict`` 包含兩個項"
"目,因為當 ``foo()`` 開始執行時,``mydict`` 以其中已有的項目開始。"
#: ../../faq/programming.rst:351
#, fuzzy
msgid ""
"It is often expected that a function call creates new objects for default "
"values. This is not what happens. Default values are created exactly once, "
"when the function is defined. If that object is changed, like the "
"dictionary in this example, subsequent calls to the function will refer to "
"this changed object."
msgstr ""
"通常期望函式呼叫為預設值建立新物件。這不是發生的事情。當定義函式時,預設值只"
"建立一次。如果該物件發生更改,如本例中的字典,則對該函式的後續呼叫將參照該已"
"更改的物件。"
#: ../../faq/programming.rst:356
#, fuzzy
msgid ""
"By definition, immutable objects such as numbers, strings, tuples, and "
"``None``, are safe from change. Changes to mutable objects such as "
"dictionaries, lists, and class instances can lead to confusion."
msgstr ""
"根據定義,數字、字串、元組和 ``None`` 等不可變物件是安全的,不會發生變化。對"
"字典、list和類別實例等可變物件的更改可能會導致混淆。"
#: ../../faq/programming.rst:360
#, fuzzy
msgid ""
"Because of this feature, it is good programming practice to not use mutable "
"objects as default values. Instead, use ``None`` as the default value and "
"inside the function, check if the parameter is ``None`` and create a new "
"list/dictionary/whatever if it is. For example, don't write::"
msgstr ""
"由於這個特性,不使用可變物件作為預設值是一個很好的編程習慣。相反,使用 "
"``None`` 作為預設值並在函式內部檢查參數是否為 ``None`` 並建立一個新的list/字"
"典/無論是否是。例如,不要寫: ::"
#: ../../faq/programming.rst:365
msgid ""
"def foo(mydict={}):\n"
" ..."
msgstr ""
"def foo(mydict={}):\n"
" ..."
#: ../../faq/programming.rst:368
msgid "but::"
msgstr "但是: ::"
#: ../../faq/programming.rst:370
msgid ""
"def foo(mydict=None):\n"
" if mydict is None:\n"
" mydict = {} # create a new dict for local namespace"
msgstr ""
"def foo(mydict=None):\n"
" if mydict is None:\n"
" mydict = {} # 為區域命名空間建立一個新字典"
#: ../../faq/programming.rst:374
#, fuzzy
msgid ""
"This feature can be useful. When you have a function that's time-consuming "
"to compute, a common technique is to cache the parameters and the resulting "
"value of each call to the function, and return the cached value if the same "
"value is requested again. This is called \"memoizing\", and can be "
"implemented like this::"
msgstr ""
"此功能可能很有用。當你有一個計算起來很耗時的函式時,一種常用的技術是快取參數"
"和每次呼叫該函式的結果值,並在再次請求相同的值時回傳快取的值。這稱為「記憶"
"化」,可以像這樣實作: ::"
#: ../../faq/programming.rst:379
msgid ""
"# Callers can only provide two parameters and optionally pass _cache by "
"keyword\n"
"def expensive(arg1, arg2, *, _cache={}):\n"
" if (arg1, arg2) in _cache:\n"
" return _cache[(arg1, arg2)]\n"
"\n"
" # Calculate the value\n"
" result = ... expensive computation ...\n"
" _cache[(arg1, arg2)] = result # Store result in the cache\n"
" return result"
msgstr ""
#: ../../faq/programming.rst:389
msgid ""
"You could use a global variable containing a dictionary instead of the "
"default value; it's a matter of taste."
msgstr "你可以使用包含字典的全域變數而不是預設值;這取決於喜好。"
#: ../../faq/programming.rst:394
#, fuzzy
msgid ""
"How can I pass optional or keyword parameters from one function to another?"
msgstr "如何將可選參數或關鍵字參數從一個函式傳遞到另一個函式?"
#: ../../faq/programming.rst:396
#, fuzzy
msgid ""
"Collect the arguments using the ``*`` and ``**`` specifiers in the "
"function's parameter list; this gives you the positional arguments as a "
"tuple and the keyword arguments as a dictionary. You can then pass these "
"arguments when calling another function by using ``*`` and ``**``::"
msgstr ""
"在函式的引數list中使用``*`` 和``**`` 說明符收集參數;這為你提供了作為元組的位"
"置引數和作為字典的關鍵字引數。然後,你可以在使用 ``*`` 和 ``**`` 呼叫另一個函"
"式時傳遞這些引數: ::"
#: ../../faq/programming.rst:401
msgid ""
"def f(x, *args, **kwargs):\n"
" ...\n"
" kwargs['width'] = '14.3c'\n"
" ...\n"
" g(x, *args, **kwargs)"
msgstr ""
"def f(x, *args, **kwargs):\n"
" ...\n"
" kwargs['width'] = '14.3c'\n"
" ...\n"
" g(x, *args, **kwargs)"
#: ../../faq/programming.rst:415
msgid "What is the difference between arguments and parameters?"
msgstr "引數 (arguments) 和參數 (parameters) 有什麼區別?"
#: ../../faq/programming.rst:417
msgid ""
":term:`Parameters <parameter>` are defined by the names that appear in a "
"function definition, whereas :term:`arguments <argument>` are the values "
"actually passed to a function when calling it. Parameters define "
"what :term:`kind of arguments <parameter>` a function can accept. For "
"example, given the function definition::"
msgstr ""
":term:`參數 <parameter>`\\ 由出現在函式定義中的名稱定義,而\\ :term:`引數 "
"<argument>`\\ 是呼叫函式時實際傳遞給函式的值。參數定義函式可以接受的"
"\\ :term:`引數種類 <parameter>`。例如,給定以下函式定義: ::"
#: ../../faq/programming.rst:423
msgid ""
"def func(foo, bar=None, **kwargs):\n"
" pass"
msgstr ""
"def func(foo, bar=None, **kwargs):\n"
" pass"
#: ../../faq/programming.rst:426
msgid ""
"*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling "
"``func``, for example::"
msgstr ""
"*foo*、*bar* 和 *kwargs* 是 ``func`` 的參數。然而當呼叫 ``func`` 時,例"
"如: ::"
#: ../../faq/programming.rst:429
msgid "func(42, bar=314, extra=somevar)"
msgstr "func(42, bar=314, extra=somevar)"
#: ../../faq/programming.rst:431
msgid "the values ``42``, ``314``, and ``somevar`` are arguments."
msgstr "``42``、``314`` 和 ``somevar`` 是引數。"
#: ../../faq/programming.rst:435
msgid "Why did changing list 'y' also change list 'x'?"
msgstr "為什麼更改 list 'y' 也會更改 list 'x'?"
#: ../../faq/programming.rst:437
msgid "If you wrote code like::"
msgstr "如果你寫了像這樣的程式碼: ::"
#: ../../faq/programming.rst:439
msgid ""
">>> x = []\n"
">>> y = x\n"
">>> y.append(10)\n"
">>> y\n"
"[10]\n"
">>> x\n"
"[10]"
msgstr ""
">>> x = []\n"
">>> y = x\n"
">>> y.append(10)\n"
">>> y\n"
"[10]\n"
">>> x\n"
"[10]"
#: ../../faq/programming.rst:447
msgid ""
"you might be wondering why appending an element to ``y`` changed ``x`` too."
msgstr "你可能想知道為什麼將一個元素附加到 ``y`` 時也會改變 ``x``。"
#: ../../faq/programming.rst:449
msgid "There are two factors that produce this result:"
msgstr "產生這個結果的原因有兩個:"
#: ../../faq/programming.rst:451
#, fuzzy
msgid ""
"Variables are simply names that refer to objects. Doing ``y = x`` doesn't "
"create a copy of the list -- it creates a new variable ``y`` that refers to "
"the same object ``x`` refers to. This means that there is only one object "
"(the list), and both ``x`` and ``y`` refer to it."
msgstr ""
"變數只是參照物件的名稱。執行 ``y = x`` 不會建立list的副本——它會建立一個新變"
"數 ``y``,它指向 ``x`` 指向的同一物件。這意味著只有一個物件(list),並且 "
"``x`` 和 ``y`` 都參照它。"
#: ../../faq/programming.rst:455
msgid ""
"Lists are :term:`mutable`, which means that you can change their content."
msgstr "list 是 :term:`mutable`,這意味著你可以變更它們的內容。"
#: ../../faq/programming.rst:457
msgid ""
"After the call to :meth:`!append`, the content of the mutable object has "
"changed from ``[]`` to ``[10]``. Since both the variables refer to the same "
"object, using either name accesses the modified value ``[10]``."
msgstr ""
"在呼叫 :meth:`!append` 之後,可變物件的內容從 ``[]`` 變成了 ``[10]``。由於這"
"兩個變數都參照同一個物件,因此使用任一名稱都可以存取修改後的值 ``[10]``。"
#: ../../faq/programming.rst:461
msgid "If we instead assign an immutable object to ``x``::"
msgstr "如果我們改為賦予一個不可變物件給 ``x``: ::"
#: ../../faq/programming.rst:463
msgid ""
">>> x = 5 # ints are immutable\n"
">>> y = x\n"
">>> x = x + 1 # 5 can't be mutated, we are creating a new object here\n"
">>> x\n"
"6\n"
">>> y\n"
"5"
msgstr ""
">>> x = 5 # 整數為不可變的\n"
">>> y = x\n"
">>> x = x + 1 # 5 不可變,在這邊會建立一個新物件\n"
">>> x\n"
"6\n"
">>> y\n"
"5"
#: ../../faq/programming.rst:471
#, fuzzy
msgid ""
"we can see that in this case ``x`` and ``y`` are not equal anymore. This is "
"because integers are :term:`immutable`, and when we do ``x = x + 1`` we are "
"not mutating the int ``5`` by incrementing its value; instead, we are "
"creating a new object (the int ``6``) and assigning it to ``x`` (that is, "
"changing which object ``x`` refers to). After this assignment we have two "
"objects (the ints ``6`` and ``5``) and two variables that refer to them "
"(``x`` now refers to ``6`` but ``y`` still refers to ``5``)."
msgstr ""
"我們可以看到,在這種情況下,``x`` 和 ``y`` 不再相等。這是因為整數是不可變的,"
"當我們做 x = x + 1 時,我們並沒有透過增加它的值來改變 int 5 ;相反,我們正在"
"建立一個新物件(int ``6``)並將其分配給``x``(也就是說,更改``x``指向的物"
"件)。在這個賦值之後,我們有兩個物件(整數 ``6`` 和 ``5``)和兩個參照它們的變"
"數(``x`` 現在指的是 ``6`` 但 ``y`` 仍然指的是``5``)。"
#: ../../faq/programming.rst:479
#, fuzzy
msgid ""
"Some operations (for example ``y.append(10)`` and ``y.sort()``) mutate the "
"object, whereas superficially similar operations (for example ``y = y + "
"[10]`` and :func:`sorted(y) <sorted>`) create a new object. In general in "
"Python (and in all cases in the standard library) a method that mutates an "
"object will return ``None`` to help avoid getting the two types of "
"operations confused. So if you mistakenly write ``y.sort()`` thinking it "
"will give you a sorted copy of ``y``, you'll instead end up with ``None``, "
"which will likely cause your program to generate an easily diagnosed error."
msgstr ""
"一些操作(例如 ``y.append(10)`` 和 ``y.sort()``)會改變物件,而表面上相似的操"
"作(例如 ``y = y + [10]`` 和: func:`sorted(y) <sorted>`) 建立一個新物件。通"
"常在 Python 中(以及在標準函式庫中的所有情況下)改變物件的方法將回傳 "
"``None`` 以幫助避免混淆這兩種型別的操作。因此,如果你錯誤地編寫了 "
"``y.sort()``,認為它會為你提供 ``y`` 的排序副本,那麼你最終會得到 ``None``,"
"這可能會導致你的程式生成一個容易診斷的錯誤。"
#: ../../faq/programming.rst:488
#, fuzzy
msgid ""
"However, there is one class of operations where the same operation sometimes "
"has different behaviors with different types: the augmented assignment "
"operators. For example, ``+=`` mutates lists but not tuples or ints "
"(``a_list += [1, 2, 3]`` is equivalent to ``a_list.extend([1, 2, 3])`` and "
"mutates ``a_list``, whereas ``some_tuple += (1, 2, 3)`` and ``some_int += "
"1`` create new objects)."
msgstr ""
"但是,有一種操作,其中相同的操作有時具有不同型別的不同行為:擴充賦值運算子。"
"例如,``+=`` 改變list而不是元組或整數(``a_list += [1, 2, 3]`` 等同於"
"``a_list.extend([1, 2, 3])``並改變 ``a_list``,而 ``some_tuple += (1, 2, "
"3)`` 和 ``some_int += 1`` 建立新物件)。"
#: ../../faq/programming.rst:495
msgid "In other words:"
msgstr "換句話說:"
#: ../../faq/programming.rst:497
#, fuzzy
msgid ""
"If we have a mutable object (:class:`list`, :class:`dict`, :class:`set`, "
"etc.), we can use some specific operations to mutate it and all the "
"variables that refer to it will see the change."
msgstr ""
"如果我們有一個可變物件(:class:`list`、:class:`dict`、:class:`set` 等),我們"
"可以使用一些特定的操作來改變它,所有參照它的變數都會看到變化。"
#: ../../faq/programming.rst:500
#, fuzzy
msgid ""
"If we have an immutable object (:class:`str`, :class:`int`, :class:`tuple`, "
"etc.), all the variables that refer to it will always see the same value, "
"but operations that transform that value into a new value always return a "
"new object."
msgstr ""
"如果我們有一個不可變物件(:class:`str`、:class:`int`、:class:`tuple` 等),所"
"有參照它的變數將始終看到相同的值,但是轉換的操作將該值轉化為新值總是回傳一個"
"新物件。"
#: ../../faq/programming.rst:505
#, fuzzy
msgid ""
"If you want to know if two variables refer to the same object or not, you "