From 9bb6498e055964e95fa23b0289358ecbcf72b1f3 Mon Sep 17 00:00:00 2001 From: dylanpong1109 Date: Sat, 3 Oct 2026 01:57:52 +0800 Subject: [PATCH 1/2] Translate faq/design.po --- faq/design.po | 288 +++++++++++++++++++++++++++++--------------------- 1 file changed, 167 insertions(+), 121 deletions(-) diff --git a/faq/design.po b/faq/design.po index f5363b34de5..4b54903a2e5 100644 --- a/faq/design.po +++ b/faq/design.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: Python 3.14\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2025-09-08 15:25+0800\n" +"POT-Creation-Date: 2026-09-13 20:11+0800\n" "PO-Revision-Date: 2023-08-31 11:34+0800\n" "Last-Translator: Steven Hsu \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" @@ -450,7 +450,8 @@ msgid "" msgstr "" ":meth:`~str.join` 是一個字串方法,因為在用他的時候,你是告訴分隔字串去走遍整" "個字串序列,並將自己插入到相鄰的兩項之間。這個方法的參數可以是任何符合序列規" -"則的物件,包括自己定義的新類別。在 bytes 和 bytearray 物件也有類似的方法可用。" +"則的物件,包括自己定義的新類別。在 bytes 和 bytearray 物件也有類似的方法可" +"用。" #: ../../faq/design.rst:233 msgid "How fast are exceptions?" @@ -519,18 +520,25 @@ msgid "" "In general, structured switch statements execute one block of code when an " "expression has a particular value or set of values. Since Python 3.10 one " "can easily match literal values, or constants within a namespace, with a " -"``match ... case`` statement. An older alternative is a sequence of ``if... " -"elif... elif... else``." +"``match ... case`` statement. See :ref:`the specification ` " +"and :ref:`the tutorial ` for more information " +"about :keyword:`match` statements. An older alternative is a sequence of " +"``if... elif... elif... else``." msgstr "" +"一般來說,結構化的 switch 陳述式會在運算式具有特定值或一組值時,執行一個程式" +"碼區塊。從 Python 3.10 開始,可以使用 ``match ... case`` 陳述式來匹配字面值," +"或命名空間中的常數。更多關於 :keyword:`match` 陳述式的資訊,請參閱\\ :ref:`規" +"格說明 `\\ 和\\ :ref:`教學 `。較舊的替代方案是一連串的 " +"``if... elif... elif... else``。" -#: ../../faq/design.rst:268 +#: ../../faq/design.rst:270 msgid "" "For cases where you need to choose from a very large number of " "possibilities, you can create a dictionary mapping case values to functions " "to call. For example::" msgstr "如果可能性很多,你可以用字典去對映要呼叫的函式。舉例來說: ::" -#: ../../faq/design.rst:272 +#: ../../faq/design.rst:274 msgid "" "functions = {'a': function_1,\n" " 'b': function_2,\n" @@ -539,12 +547,14 @@ msgid "" "func = functions[value]\n" "func()" msgstr "" -"if key in mydict:\n" -" value = mydict[key]\n" -"else:\n" -" value = mydict[key] = getvalue(key)" +"functions = {'a': function_1,\n" +" 'b': function_2,\n" +" 'c': self.method_1}\n" +"\n" +"func = functions[value]\n" +"func()" -#: ../../faq/design.rst:279 +#: ../../faq/design.rst:281 msgid "" "For calling methods on objects, you can simplify yet further by using " "the :func:`getattr` built-in to retrieve methods with a particular name::" @@ -552,7 +562,7 @@ msgstr "" "對於呼叫物件裡的方法,你可以利用內建用來找尋特定方法的函式 :func:`getattr` 來" "做進一步的簡化: ::" -#: ../../faq/design.rst:282 +#: ../../faq/design.rst:284 msgid "" "class MyVisitor:\n" " def visit_a(self):\n" @@ -572,7 +582,7 @@ msgstr "" " method = getattr(self, method_name)\n" " method()" -#: ../../faq/design.rst:291 +#: ../../faq/design.rst:293 msgid "" "It's suggested that you use a prefix for the method names, such as " "``visit_`` in this example. Without such a prefix, if values are coming " @@ -582,19 +592,21 @@ msgstr "" "我們建議在方法名稱加上前綴,以這個例子來說是 像是 ``visit_``。沒有前綴的話," "一旦收到從不信任來源的值,攻擊者便可以隨意呼叫在你的專案內的方法。" -#: ../../faq/design.rst:295 +#: ../../faq/design.rst:297 msgid "" "Imitating switch with fallthrough, as with C's switch-case-default, is " "possible, much harder, and less needed." msgstr "" +"模仿具有穿透 (fallthrough) 特性的 switch 雖然可行(就像 C 的 switch-case-" +"default 一樣),但實作起來較為困難,實用性也不高。" -#: ../../faq/design.rst:300 +#: ../../faq/design.rst:302 msgid "" "Can't you emulate threads in the interpreter instead of relying on an OS-" "specific thread implementation?" msgstr "為何不能在直譯器上模擬執行緒,而要使用作業系統的特定實作方式?" -#: ../../faq/design.rst:302 +#: ../../faq/design.rst:304 msgid "" "Answer 1: Unfortunately, the interpreter pushes at least one C stack frame " "for each Python stack frame. Also, extensions can call back into Python at " @@ -605,7 +617,7 @@ msgstr "" "的堆疊框。同時,擴充套件可以隨時呼叫 Python,因此完整的實作必須要支援 C 的執" "行緒。" -#: ../../faq/design.rst:307 +#: ../../faq/design.rst:309 msgid "" "Answer 2: Fortunately, there is `Stackless Python `_, which has a completely redesigned " @@ -614,11 +626,11 @@ msgstr "" "答案二:幸運地,`無堆疊 (Stackless) Python `_ 完全重新設計了直譯器迴圈,並避免了 C 堆疊。" -#: ../../faq/design.rst:312 +#: ../../faq/design.rst:314 msgid "Why can't lambda expressions contain statements?" msgstr "為何 lambda 運算式不能包含陳述式?" -#: ../../faq/design.rst:314 +#: ../../faq/design.rst:316 msgid "" "Python lambda expressions cannot contain statements because Python's " "syntactic framework can't handle statements nested inside expressions. " @@ -631,7 +643,7 @@ msgstr "" "立功能的 lambda,Python 的 lambda 只是一個在你懶得定義函式時可用的一個簡寫表" "達法。" -#: ../../faq/design.rst:320 +#: ../../faq/design.rst:322 msgid "" "Functions are already first class objects in Python, and can be declared in " "a local scope. Therefore the only advantage of using a lambda instead of a " @@ -644,11 +656,11 @@ msgstr "" "宣告。因此唯一用 lambda 而非區域性的函式的優點就是你不需要多想一個函式名稱 — " "但這樣就會是一個區域變數被指定成函式物件(和 lambda 運算式的結果同類)!" -#: ../../faq/design.rst:328 +#: ../../faq/design.rst:330 msgid "Can Python be compiled to machine code, C or some other language?" msgstr "Python 可以被編譯成機器語言、C 語言或其他種語言嗎?" -#: ../../faq/design.rst:330 +#: ../../faq/design.rst:332 msgid "" "`Cython `_ compiles a modified version of Python with " "optional annotations into C extensions. `Nuitka `_ is " @@ -659,11 +671,11 @@ msgstr "" "本。`Nuitka `_ 是一個有潛力編譯器,可以把 Python 編譯成 " "C++,他的目標是支援完整的 Python 語言。" -#: ../../faq/design.rst:337 +#: ../../faq/design.rst:339 msgid "How does Python manage memory?" msgstr "Python 如何管理記憶體?" -#: ../../faq/design.rst:339 +#: ../../faq/design.rst:341 msgid "" "The details of Python memory management depend on the implementation. The " "standard implementation of Python, :term:`CPython`, uses reference counting " @@ -679,7 +691,7 @@ msgstr "" "件。 :mod:`gc` 模組提供了可以執行垃圾收集、抓取除錯統計數據和調整收集器參數的" "函式。" -#: ../../faq/design.rst:347 +#: ../../faq/design.rst:349 msgid "" "Other implementations (such as `Jython `_ or `PyPy " "`_), however, can rely on a different mechanism such as a " @@ -691,7 +703,7 @@ msgstr "" "pypy.org>`_)中,會使用像是成熟的垃圾收集器等不同機制。如果你的 Python 程式碼" "的表現取決於參照計次的實作,這個相異處會導致一些微小的移植問題。" -#: ../../faq/design.rst:353 +#: ../../faq/design.rst:355 msgid "" "In some Python implementations, the following code (which is fine in " "CPython) will probably run out of file descriptors::" @@ -699,7 +711,7 @@ msgstr "" "在一些 Python 實作中,下面這段程式碼(在 CPython 可以正常運作)可能會把檔案描" "述子 (file descriptor) 用盡: ::" -#: ../../faq/design.rst:356 +#: ../../faq/design.rst:358 msgid "" "for file in very_long_list_of_files:\n" " f = open(file)\n" @@ -709,7 +721,7 @@ msgstr "" " f = open(file)\n" " c = f.read(1)" -#: ../../faq/design.rst:360 +#: ../../faq/design.rst:362 msgid "" "Indeed, using CPython's reference counting and destructor scheme, each new " "assignment to ``f`` closes the previous file. With a traditional GC, " @@ -720,7 +732,7 @@ msgstr "" "的新指派都會關閉前面打開的檔案。然而用傳統的垃圾回收 (GC) 的話,這些檔案物件" "只會在不固定且有可能很長的時間後被收集(並關閉)。" -#: ../../faq/design.rst:365 +#: ../../faq/design.rst:367 msgid "" "If you want to write code that will work with any Python implementation, you " "should explicitly close the file or use the :keyword:`with` statement; this " @@ -730,7 +742,7 @@ msgstr "" "案或是使用 :keyword:`with` 陳述式,如此一來,不用管記憶體管理的方法,他也會正" "常運作: ::" -#: ../../faq/design.rst:369 +#: ../../faq/design.rst:371 msgid "" "for file in very_long_list_of_files:\n" " with open(file) as f:\n" @@ -740,11 +752,11 @@ msgstr "" " with open(file) as f:\n" " c = f.read(1)" -#: ../../faq/design.rst:375 +#: ../../faq/design.rst:377 msgid "Why doesn't CPython use a more traditional garbage collection scheme?" msgstr "為何 CPython 不使用更多傳統的垃圾回收機制?" -#: ../../faq/design.rst:377 +#: ../../faq/design.rst:379 msgid "" "For one thing, this is not a C standard feature and hence it's not portable. " "(Yes, we know about the Boehm GC library. It has bits of assembler code for " @@ -756,7 +768,7 @@ msgstr "" "庫。他有可相容於\\ *大多數*\\ 平台的組合語言程式碼,但依然不是全部,而即便它" "大多數是通透的,也並不完全,要讓它跟 Python 相容還是需要做一些修補。)" -#: ../../faq/design.rst:383 +#: ../../faq/design.rst:385 msgid "" "Traditional GC also becomes a problem when Python is embedded into other " "applications. While in a standalone Python it's fine to replace the " @@ -772,11 +784,11 @@ msgstr "" "free() 替代品,而不是用 Python 的。以現在來說,CPython 和實作 malloc() 和 " "free() 的程式相處融洽。" -#: ../../faq/design.rst:392 +#: ../../faq/design.rst:394 msgid "Why isn't all memory freed when CPython exits?" msgstr "當 CPython 結束時,為何所有的記憶體不會被釋放?" -#: ../../faq/design.rst:394 +#: ../../faq/design.rst:396 msgid "" "Objects referenced from the global namespaces of Python modules are not " "always deallocated when Python exits. This may happen if there are circular " @@ -790,7 +802,7 @@ msgstr "" "(例如:像是 Purify 之類的工具會抱怨)。然而,Python 在關閉的時候會積極清理記" "憶體並嘗試刪除每個物件。" -#: ../../faq/design.rst:401 +#: ../../faq/design.rst:403 msgid "" "If you want to force Python to delete certain things on deallocation use " "the :mod:`atexit` module to run a function that will force those deletions." @@ -798,11 +810,11 @@ msgstr "" "如果你想要強迫 Python 在釋放記憶體時刪除特定的東西,你可以用 :mod:`atexit` 模" "組來執行會強制刪除的函式。" -#: ../../faq/design.rst:406 +#: ../../faq/design.rst:408 msgid "Why are there separate tuple and list data types?" msgstr "為何要把元組 (tuple) 和串列 (list) 分成兩個資料型別?" -#: ../../faq/design.rst:408 +#: ../../faq/design.rst:410 msgid "" "Lists and tuples, while similar in many respects, are generally used in " "fundamentally different ways. Tuples can be thought of as being similar to " @@ -816,7 +828,7 @@ msgstr "" "合,以一組為單位進行操作。舉例來說,一個笛卡兒坐標系可以適當地表示成一個有二" "或三個值的元組。" -#: ../../faq/design.rst:415 +#: ../../faq/design.rst:417 msgid "" "Lists, on the other hand, are more like arrays in other languages. They " "tend to hold a varying number of objects all of which have the same type and " @@ -830,7 +842,7 @@ msgstr "" "案,以包含字串的串列表示。如果你新增了幾個檔案到這個目錄,一般來說操作結果的" "函式也會正常運作。" -#: ../../faq/design.rst:423 +#: ../../faq/design.rst:425 msgid "" "Tuples are immutable, meaning that once a tuple has been created, you can't " "replace any of its elements with a new value. Lists are mutable, meaning " @@ -842,11 +854,11 @@ msgstr "" "可變,所以你可以改變裡面的元素。只有不可變的元素可以成為字典的鍵,所以只能把" "元組當成鍵,而串列則不行。" -#: ../../faq/design.rst:430 +#: ../../faq/design.rst:434 msgid "How are lists implemented in CPython?" msgstr "串列 (list) 在 CPython 中是怎麼實作的?" -#: ../../faq/design.rst:432 +#: ../../faq/design.rst:436 msgid "" "CPython's lists are really variable-length arrays, not Lisp-style linked " "lists. The implementation uses a contiguous array of references to other " @@ -857,13 +869,13 @@ msgstr "" "接串列 (linked list)。實作上,他是一個連續的物件參照 (reference) 陣列,並把指" "向此陣列的指標 (pointer) 和陣列長度存在串列的標頭結構內。" -#: ../../faq/design.rst:436 +#: ../../faq/design.rst:440 msgid "" "This makes indexing a list ``a[i]`` an operation whose cost is independent " "of the size of the list or the value of the index." msgstr "因此,用索引來找串列特定項 ``a[i]`` 的代價和串列大小或是索引值無關。" -#: ../../faq/design.rst:439 +#: ../../faq/design.rst:443 msgid "" "When items are appended or inserted, the array of references is resized. " "Some cleverness is applied to improve the performance of appending items " @@ -874,11 +886,16 @@ msgstr "" "用一些巧妙的方法,當陣列必須變大時,會多收集一些額外的空間,接下來幾次新增時" "就不需要再調整大小了。" -#: ../../faq/design.rst:446 +#: ../../faq/design.rst:448 +msgid "" +"See :ref:`time-complexity` for the costs of the various list operations." +msgstr "各種串列操作的時間複雜度請參閱 :ref:`time-complexity`。" + +#: ../../faq/design.rst:454 msgid "How are dictionaries implemented in CPython?" msgstr "字典 (dictionaries) 在 CPython 中是怎麼實作的?" -#: ../../faq/design.rst:448 +#: ../../faq/design.rst:456 msgid "" "CPython's dictionaries are implemented as resizable hash tables. Compared " "to B-trees, this gives better performance for lookup (the most common " @@ -888,7 +905,7 @@ msgstr "" "CPython 的字典是用可調整大小的雜湊表 (hash table) 實作的。比起 B 樹 (B-" "tree),在搜尋(目前為止最常見的操作)方面有更好的表現,實作上也較為簡單。" -#: ../../faq/design.rst:452 +#: ../../faq/design.rst:460 msgid "" "Dictionaries work by computing a hash code for each key stored in the " "dictionary using the :func:`hash` built-in function. The hash code varies " @@ -906,11 +923,17 @@ msgstr "" "被用來計算值在內部陣列儲存的位置。假設你存的鍵都有不同的雜湊值,那字典只需要" "常數時間 — 用大 O 表示法 (Big-O notation) 就是 *O*\\ (1) — 來找任意一個鍵。" -#: ../../faq/design.rst:463 +#: ../../faq/design.rst:469 +msgid "" +"See :ref:`time-complexity` for the costs of the various dictionary " +"operations." +msgstr "各種字典操作的時間複雜度請參閱 :ref:`time-complexity`。" + +#: ../../faq/design.rst:473 msgid "Why must dictionary keys be immutable?" msgstr "為何字典的鍵一定是不可變的?" -#: ../../faq/design.rst:465 +#: ../../faq/design.rst:475 msgid "" "The hash table implementation of dictionaries uses a hash value calculated " "from the key value to find the key. If the key were a mutable object, its " @@ -928,7 +951,7 @@ msgstr "" "值不同的緣故,你找不到他。而如果你嘗試用舊的值去尋找,也一樣找不到,因為他的" "雜湊結果和原先物件不同。" -#: ../../faq/design.rst:474 +#: ../../faq/design.rst:484 msgid "" "If you want a dictionary indexed with a list, simply convert the list to a " "tuple first; the function ``tuple(L)`` creates a tuple with the same entries " @@ -938,11 +961,11 @@ msgstr "" "如果你想要用串列作為字典的索引,把他轉換成元組即可。``tuple(L)`` 函式會建立一" "個和串列 ``L`` 一樣內容的元組。而元組是不可變的,所以可以用來當成字典的鍵。" -#: ../../faq/design.rst:478 +#: ../../faq/design.rst:488 msgid "Some unacceptable solutions that have been proposed:" msgstr "也有人提出一些不能接受的方法:" -#: ../../faq/design.rst:480 +#: ../../faq/design.rst:490 msgid "" "Hash lists by their address (object ID). This doesn't work because if you " "construct a new list with the same value it won't be found; e.g.::" @@ -950,7 +973,7 @@ msgstr "" "用串列的記憶體位址(物件 id)來雜湊。這不會成功,因為你如果用同樣的值建立一個" "新的串列,是找不到的。舉例來說: ::" -#: ../../faq/design.rst:483 +#: ../../faq/design.rst:493 msgid "" "mydict = {[1, 2]: '12'}\n" "print(mydict[[1, 2]])" @@ -958,7 +981,7 @@ msgstr "" "mydict = {[1, 2]: '12'}\n" "print(mydict[[1, 2]])" -#: ../../faq/design.rst:486 +#: ../../faq/design.rst:496 msgid "" "would raise a :exc:`KeyError` exception because the id of the ``[1, 2]`` " "used in the second line differs from that in the first line. In other " @@ -968,7 +991,7 @@ msgstr "" "這將會導致 :exc:`KeyError` 例外,因為 ``[1, 2]`` 的 id 在第一行和第二行是不同" "的。換句話說,字典的鍵應該要用 ``==`` 來做比較,而不是用 :keyword:`is`。" -#: ../../faq/design.rst:490 +#: ../../faq/design.rst:500 msgid "" "Make a copy when using a list as a key. This doesn't work because the list, " "being a mutable object, could contain a reference to itself, and then the " @@ -977,7 +1000,7 @@ msgstr "" "複製一個串列作為鍵。這一樣不會成功,因為串列是可變的,他可以包含自己的參照," "所以複製會形成一個無窮迴圈。" -#: ../../faq/design.rst:494 +#: ../../faq/design.rst:504 msgid "" "Allow lists as keys but tell the user not to modify them. This would allow " "a class of hard-to-track bugs in programs when you forgot or modified a list " @@ -988,7 +1011,7 @@ msgstr "" "產生一種難以追蹤的 bug。他同時也違背了一項字典的重要定則:在 ``d.keys()`` 的" "每個值都可以當成字典的鍵。" -#: ../../faq/design.rst:499 +#: ../../faq/design.rst:509 msgid "" "Mark lists as read-only once they are used as a dictionary key. The problem " "is that it's not just the top-level object that could change its value; you " @@ -1001,7 +1024,7 @@ msgstr "" "值,就像用元組包含串列來做為鍵。把一個物件當成鍵,需要將從他開始可以接觸到的" "所有物件都標記成只能讀取 — 所以再一次,自己參照自己的物件會導致無窮迴圈。" -#: ../../faq/design.rst:505 +#: ../../faq/design.rst:515 msgid "" "There is a trick to get around this if you need to, but use it at your own " "risk: You can wrap a mutable structure inside a class instance which has " @@ -1015,7 +1038,7 @@ msgstr "" "實例。只要這種包裝物件還存在於字典(或其他類似結構)中,你就必須確定在字典" "(或其他用雜湊為基底的結構)中他們的雜湊值會保持恆定。 ::" -#: ../../faq/design.rst:513 +#: ../../faq/design.rst:523 msgid "" "class ListWrapper:\n" " def __init__(self, the_list):\n" @@ -1051,7 +1074,7 @@ msgstr "" " result = (result % 7777777) + i * 333\n" " return result" -#: ../../faq/design.rst:530 +#: ../../faq/design.rst:540 msgid "" "Note that the hash computation is complicated by the possibility that some " "members of the list may be unhashable and also by the possibility of " @@ -1060,7 +1083,7 @@ msgstr "" "請注意,雜湊的計算可能變得複雜,因為有串列成員不可雜湊 (unhashable) 和算術溢" "位的可能性。" -#: ../../faq/design.rst:534 +#: ../../faq/design.rst:544 msgid "" "Furthermore it must always be the case that if ``o1 == o2`` (ie " "``o1.__eq__(o2) is True``) then ``hash(o1) == hash(o2)`` (ie, " @@ -1073,7 +1096,7 @@ msgstr "" "o2.__hash__()``),這個事實必須要成立。如果無法滿足這項限制,那字典和其他用雜" "湊為基底的結構會出現不正常的行為。" -#: ../../faq/design.rst:539 +#: ../../faq/design.rst:549 msgid "" "In the case of :class:`!ListWrapper`, whenever the wrapper object is in a " "dictionary the wrapped list must not change to avoid anomalies. Don't do " @@ -1084,11 +1107,11 @@ msgstr "" "變以避免不正常的事情發生。除非你已經謹慎思考過你的需求和無法滿足條件的後果," "不然請不要這麼做。請自行注意。" -#: ../../faq/design.rst:546 +#: ../../faq/design.rst:556 msgid "Why doesn't list.sort() return the sorted list?" msgstr "為何 list.sort() 不是回傳排序過的串列?" -#: ../../faq/design.rst:548 +#: ../../faq/design.rst:558 msgid "" "In situations where performance matters, making a copy of the list just to " "sort it would be wasteful. Therefore, :meth:`list.sort` sorts the list in " @@ -1101,7 +1124,7 @@ msgstr "" "列裡做排序。為了提醒你這件事,他不會回傳排序過的串列。這樣一來,當你需要排序" "過和未排序過的串列時,你就不會被誤導而不小心覆蓋掉串列。" -#: ../../faq/design.rst:554 +#: ../../faq/design.rst:564 msgid "" "If you want to return a new list, use the built-in :func:`sorted` function " "instead. This function creates a new list from a provided iterable, sorts " @@ -1112,17 +1135,19 @@ msgstr "" "物件 (iterable) 來排序建立新串列,並回傳之。例如,以下這個範例會說明如何有序" "地疊代字典的鍵: ::" -#: ../../faq/design.rst:559 +#: ../../faq/design.rst:569 msgid "" "for key in sorted(mydict):\n" " ... # do whatever with mydict[key]..." msgstr "" +"for key in sorted(mydict):\n" +" ... # do whatever with mydict[key]..." -#: ../../faq/design.rst:564 +#: ../../faq/design.rst:574 msgid "How do you specify and enforce an interface spec in Python?" msgstr "如何在 Python 中指定和強制使用一個介面規範 (interface spec)?" -#: ../../faq/design.rst:566 +#: ../../faq/design.rst:576 msgid "" "An interface specification for a module as provided by languages such as C++ " "and Java describes the prototypes for the methods and functions of the " @@ -1132,7 +1157,7 @@ msgstr "" "像是 C++ 和 Java 等語言提供了模組的介面規範,他描述了該模組的方法和函式的原" "型。很多人認為這種在編譯時強制執行的介面規範在建構大型程式時十分有幫助。" -#: ../../faq/design.rst:571 +#: ../../faq/design.rst:581 msgid "" "Python 2.6 adds an :mod:`abc` module that lets you define Abstract Base " "Classes (ABCs). You can then use :func:`isinstance` and :func:`issubclass` " @@ -1148,13 +1173,13 @@ msgstr "" "是 :class:`~collections.abc.Iterable`、:class:`~collections.abc.Container` " "和 :class:`~collections.abc.MutableMapping`。" -#: ../../faq/design.rst:578 +#: ../../faq/design.rst:588 msgid "" "For Python, many of the advantages of interface specifications can be " "obtained by an appropriate test discipline for components." msgstr "對 Python 來說,很多介面規範的優點可以用對元件適當的測試規則來達到。" -#: ../../faq/design.rst:581 +#: ../../faq/design.rst:591 msgid "" "A good test suite for a module can both provide a regression test and serve " "as a module interface specification and a set of examples. Many Python " @@ -1171,7 +1196,7 @@ msgstr "" "模擬來獨立測試。:mod:`doctest` 和 :mod:`unittest` 模組或第三方的測試框架可以" "用來建構詳盡徹底的測試套件來測試模組裡的每一行程式碼。" -#: ../../faq/design.rst:589 +#: ../../faq/design.rst:599 msgid "" "An appropriate testing discipline can help build large complex applications " "in Python as well as having interface specifications would. In fact, it can " @@ -1185,10 +1210,10 @@ msgstr "" "就像介面規範一樣,一個適當的測試規則在建造大型又複雜的 Python 應用程式時可以" "幫上忙。事實上,他可能可以有更好的表現,因為介面規範無法測試程式的特定屬性。" "舉例來說,:meth:`list.append` 方法應該要在某個內部的串列最後面加上新的元素," -"而介面規範沒辦法測試你的 :meth:`list.append` 是不是真的有正確的實作,但這在" -"測試套件裡是件很簡單的事。" +"而介面規範沒辦法測試你的 :meth:`list.append` 是不是真的有正確的實作,但這在測" +"試套件裡是件很簡單的事。" -#: ../../faq/design.rst:597 +#: ../../faq/design.rst:607 msgid "" "Writing test suites is very helpful, and you might want to design your code " "to make it easily tested. One increasingly popular technique, test-driven " @@ -1200,11 +1225,11 @@ msgstr "" "(test-driven development) 是一個越來越受歡迎的設計方法,他要求先完成部分的測" "試套件,再去撰寫真的要用的程式碼。當然 Python 也允許你草率地不寫任何測試。" -#: ../../faq/design.rst:605 +#: ../../faq/design.rst:615 msgid "Why is there no goto?" msgstr "為何沒有 goto 語法?" -#: ../../faq/design.rst:607 +#: ../../faq/design.rst:617 msgid "" "In the 1970s people realized that unrestricted goto could lead to messy " "\"spaghetti\" code that was hard to understand and revise. In a high-level " @@ -1221,7 +1246,7 @@ msgstr "" "迴圈(用 :keyword:`while` 和 :keyword:`for` 陳述式,可能會" "有 :keyword:`continue` 和 :keyword:`break`)。" -#: ../../faq/design.rst:614 +#: ../../faq/design.rst:624 msgid "" "One can also use exceptions to provide a \"structured goto\" that works even " "across function calls. Many feel that exceptions can conveniently emulate " @@ -1232,7 +1257,7 @@ msgstr "" "以方便地模擬在 C、Fortran 和其他語言裡各種合理使用的 ``go`` 和 ``goto``。例" "如: ::" -#: ../../faq/design.rst:620 +#: ../../faq/design.rst:630 msgid "" "class label(Exception): pass # declare a label\n" "\n" @@ -1244,19 +1269,28 @@ msgid "" " pass\n" "..." msgstr "" +"class label(Exception): pass # declare a label\n" +"\n" +"try:\n" +" ...\n" +" if condition: raise label() # goto label\n" +" ...\n" +"except label: # where to goto\n" +" pass\n" +"..." -#: ../../faq/design.rst:630 +#: ../../faq/design.rst:640 msgid "" "This doesn't allow you to jump into the middle of a loop, but that's usually " "considered an abuse of ``goto`` anyway. Use sparingly." msgstr "" "這依然不能讓你跳進迴圈內,這通常被認為是對 ``goto`` 的濫用。請小心使用。" -#: ../../faq/design.rst:635 +#: ../../faq/design.rst:645 msgid "Why can't raw strings (r-strings) end with a backslash?" msgstr "為何純字串 (r-string) 不能以反斜線結尾?" -#: ../../faq/design.rst:637 +#: ../../faq/design.rst:647 msgid "" "More precisely, they can't end with an odd number of backslashes: the " "unpaired backslash at the end escapes the closing quote character, leaving " @@ -1265,7 +1299,7 @@ msgstr "" "更精確地來說,他不能以奇數個反斜線結尾:尾端未配對的反斜線會使結尾的引號被轉" "義 (escapes),變成一個未結束的字串。" -#: ../../faq/design.rst:641 +#: ../../faq/design.rst:651 msgid "" "Raw strings were designed to ease creating input for processors (chiefly " "regular expression engines) that want to do their own backslash escape " @@ -1279,34 +1313,37 @@ msgstr "" "此。相對地,他讓你用一個反斜線跳脫引號。這些規則在他們預想的目的上正常地運" "作。" -#: ../../faq/design.rst:648 +#: ../../faq/design.rst:658 msgid "" "If you're trying to build Windows pathnames, note that all Windows system " "calls accept forward slashes too::" msgstr "" "如果你嘗試建立 Windows 的路徑名稱,請注意 Windows 系統指令也接受一般斜線: ::" -#: ../../faq/design.rst:651 +#: ../../faq/design.rst:661 msgid "f = open(\"/mydir/file.txt\") # works fine!" -msgstr "" +msgstr "f = open(\"/mydir/file.txt\") # works fine!" -#: ../../faq/design.rst:653 +#: ../../faq/design.rst:663 msgid "" "If you're trying to build a pathname for a DOS command, try e.g. one of ::" msgstr "如果你嘗試建立 DOS 指令的路徑名稱,試試看使用以下的範例: ::" -#: ../../faq/design.rst:655 +#: ../../faq/design.rst:665 msgid "" "dir = r\"\\this\\is\\my\\dos\\dir\" \"\\\\\"\n" "dir = r\"\\this\\is\\my\\dos\\dir\\ \"[:-1]\n" "dir = \"\\\\this\\\\is\\\\my\\\\dos\\\\dir\\\\\"" msgstr "" +"dir = r\"\\this\\is\\my\\dos\\dir\" \"\\\\\"\n" +"dir = r\"\\this\\is\\my\\dos\\dir\\ \"[:-1]\n" +"dir = \"\\\\this\\\\is\\\\my\\\\dos\\\\dir\\\\\"" -#: ../../faq/design.rst:661 +#: ../../faq/design.rst:671 msgid "Why doesn't Python have a \"with\" statement for attribute assignments?" msgstr "為何 Python 沒有屬性賦值的 with 陳述式?" -#: ../../faq/design.rst:663 +#: ../../faq/design.rst:673 msgid "" "Python has a :keyword:`with` statement that wraps the execution of a block, " "calling code on the entrance and exit from the block. Some languages have a " @@ -1315,18 +1352,21 @@ msgstr "" "Python 的 :keyword:`with` 陳述式包裝了一區塊程式的執行,在進入和離開該區塊時" "執行程式碼。一些語言會有像如下的結構: ::" -#: ../../faq/design.rst:667 +#: ../../faq/design.rst:677 msgid "" "with obj:\n" " a = 1 # equivalent to obj.a = 1\n" " total = total + 1 # obj.total = obj.total + 1" msgstr "" +"with obj:\n" +" a = 1 # equivalent to obj.a = 1\n" +" total = total + 1 # obj.total = obj.total + 1" -#: ../../faq/design.rst:671 +#: ../../faq/design.rst:681 msgid "In Python, such a construct would be ambiguous." msgstr "但在 Python,這種結構是模糊的。" -#: ../../faq/design.rst:673 +#: ../../faq/design.rst:683 msgid "" "Other languages, such as Object Pascal, Delphi, and C++, use static types, " "so it's possible to know, in an unambiguous way, what member is being " @@ -1337,7 +1377,7 @@ msgstr "" "以清楚地知道是哪一個成員被指派值。這是靜態型別的重點 — 在編譯的時候,編譯器" "\\ *永遠*\\ 都知道每個變數的作用域 (scope)。" -#: ../../faq/design.rst:678 +#: ../../faq/design.rst:688 msgid "" "Python uses dynamic types. It is impossible to know in advance which " "attribute will be referenced at runtime. Member attributes may be added or " @@ -1349,11 +1389,11 @@ msgstr "" "員屬性可能在執行時從物件中被新增或移除。這使得如果簡單來看的話,我們無法得知" "以下哪個屬性會被使用:區域的、全域的、或是成員屬性?" -#: ../../faq/design.rst:684 +#: ../../faq/design.rst:694 msgid "For instance, take the following incomplete snippet::" msgstr "以下列不完整的程式碼為例: ::" -#: ../../faq/design.rst:686 +#: ../../faq/design.rst:696 msgid "" "def foo(a):\n" " with a:\n" @@ -1363,7 +1403,7 @@ msgstr "" " with a:\n" " print(x)" -#: ../../faq/design.rst:690 +#: ../../faq/design.rst:700 msgid "" "The snippet assumes that ``a`` must have a member attribute called ``x``. " "However, there is nothing in Python that tells the interpreter this. What " @@ -1376,7 +1416,7 @@ msgstr "" "變數稱為 ``x``,那在這個 :keyword:`with` 區塊會被使用嗎?如你所見,Python 動" "態的天性使得這種選擇更加困難。" -#: ../../faq/design.rst:696 +#: ../../faq/design.rst:706 msgid "" "The primary benefit of :keyword:`with` and similar language features " "(reduction of code volume) can, however, easily be achieved in Python by " @@ -1385,7 +1425,7 @@ msgstr "" "然而 :keyword:`with` 陳述式或類似的語言特性(減少程式碼量)的主要好處可以透過" "賦值來達成。相較於這樣寫: ::" -#: ../../faq/design.rst:699 +#: ../../faq/design.rst:709 msgid "" "function(args).mydict[index][index].a = 21\n" "function(args).mydict[index][index].b = 42\n" @@ -1395,11 +1435,11 @@ msgstr "" "function(args).mydict[index][index].b = 42\n" "function(args).mydict[index][index].c = 63" -#: ../../faq/design.rst:703 +#: ../../faq/design.rst:713 msgid "write this::" msgstr "應該寫成這樣: ::" -#: ../../faq/design.rst:705 +#: ../../faq/design.rst:715 msgid "" "ref = function(args).mydict[index][index]\n" "ref.a = 21\n" @@ -1411,7 +1451,7 @@ msgstr "" "ref.b = 42\n" "ref.c = 63" -#: ../../faq/design.rst:710 +#: ../../faq/design.rst:720 msgid "" "This also has the side-effect of increasing execution speed because name " "bindings are resolved at run-time in Python, and the second version only " @@ -1420,7 +1460,7 @@ msgstr "" "這也有提升執行速度的副作用,因為 Python 的名稱綁定解析會在執行的時候發生,而" "第二版只需要執行解析一次即可。" -#: ../../faq/design.rst:714 +#: ../../faq/design.rst:724 msgid "" "Similar proposals that would introduce syntax to further reduce code volume, " "such as using a 'leading dot', have been rejected in favour of explicitness " @@ -1430,11 +1470,11 @@ msgstr "" "dot)」,但這些提案已被捨棄,以維持程式的明確性(參閱 https://mail.python.org/" "pipermail/python-ideas/2016-May/040070.html)。" -#: ../../faq/design.rst:720 +#: ../../faq/design.rst:730 msgid "Why don't generators support the with statement?" msgstr "為何產生器 (generator) 不支援 with 陳述式?" -#: ../../faq/design.rst:722 +#: ../../faq/design.rst:732 msgid "" "For technical reasons, a generator used directly as a context manager would " "not work correctly. When, as is most common, a generator is used as an " @@ -1447,18 +1487,18 @@ msgstr "" "要的話,你可以在 :keyword:`with` 陳述式裡" "用 :func:`contextlib.closing(generator) ` 來包裝他。" -#: ../../faq/design.rst:730 +#: ../../faq/design.rst:740 msgid "Why are colons required for the if/while/def/class statements?" msgstr "為何 if、while、def、class 陳述式裡需要冒號?" -#: ../../faq/design.rst:732 +#: ../../faq/design.rst:742 msgid "" "The colon is required primarily to enhance readability (one of the results " "of the experimental ABC language). Consider this::" msgstr "" "需要冒號主要是為了增加可讀性(由 ABC 語言的實驗得知)。試想如下範例: ::" -#: ../../faq/design.rst:735 +#: ../../faq/design.rst:745 msgid "" "if a == b\n" " print(a)" @@ -1466,11 +1506,11 @@ msgstr "" "if a == b\n" " print(a)" -#: ../../faq/design.rst:738 +#: ../../faq/design.rst:748 msgid "versus ::" msgstr "以及: ::" -#: ../../faq/design.rst:740 +#: ../../faq/design.rst:750 msgid "" "if a == b:\n" " print(a)" @@ -1478,7 +1518,7 @@ msgstr "" "if a == b:\n" " print(a)" -#: ../../faq/design.rst:743 +#: ../../faq/design.rst:753 msgid "" "Notice how the second one is slightly easier to read. Notice further how a " "colon sets off the example in this FAQ answer; it's a standard usage in " @@ -1487,7 +1527,7 @@ msgstr "" "注意第二個例子稍微易讀一些的原因。可以更進一步觀察,一個冒號是如何放在這個 " "FAQ 答案的例子裡的,這是標準的英文用法。" -#: ../../faq/design.rst:746 +#: ../../faq/design.rst:756 msgid "" "Another minor reason is that the colon makes it easier for editors with " "syntax highlighting; they can look for colons to decide when indentation " @@ -1497,17 +1537,17 @@ msgstr "" "另一個小原因是冒號會使編輯器更容易做語法突顯,他們只需要看冒號的位置就可以決" "定是否需要更多縮排,而不用做更多繁複精密的程式碼剖析。" -#: ../../faq/design.rst:752 +#: ../../faq/design.rst:762 msgid "Why does Python allow commas at the end of lists and tuples?" msgstr "為何 Python 允許在串列和元組末端加上逗號?" -#: ../../faq/design.rst:754 +#: ../../faq/design.rst:764 msgid "" "Python lets you add a trailing comma at the end of lists, tuples, and " "dictionaries::" msgstr "Python 允許你在串列、元組和字典的結尾加上逗號: ::" -#: ../../faq/design.rst:757 +#: ../../faq/design.rst:767 msgid "" "[1, 2, 3,]\n" "('a', 'b', 'c',)\n" @@ -1516,12 +1556,18 @@ msgid "" " \"B\": [6, 7], # last trailing comma is optional but good style\n" "}" msgstr "" +"[1, 2, 3,]\n" +"('a', 'b', 'c',)\n" +"d = {\n" +" \"A\": [1, 5],\n" +" \"B\": [6, 7], # last trailing comma is optional but good style\n" +"}" -#: ../../faq/design.rst:765 +#: ../../faq/design.rst:775 msgid "There are several reasons to allow this." msgstr "這有許多原因可被允許。" -#: ../../faq/design.rst:767 +#: ../../faq/design.rst:777 msgid "" "When you have a literal value for a list, tuple, or dictionary spread across " "multiple lines, it's easier to add more elements because you don't have to " @@ -1531,13 +1577,13 @@ msgstr "" "當你要把串列、元組或字典的值寫成多行時,這樣做會讓你新增元素時較為方便,因為" "你不需要在前一行加上逗號。這幾行的值也可以被重新排序,而不會導致語法錯誤。" -#: ../../faq/design.rst:772 +#: ../../faq/design.rst:782 msgid "" "Accidentally omitting the comma can lead to errors that are hard to " "diagnose. For example::" msgstr "不小心遺漏了逗號會導致難以發現的錯誤,例如: ::" -#: ../../faq/design.rst:775 +#: ../../faq/design.rst:785 msgid "" "x = [\n" " \"fee\",\n" @@ -1553,7 +1599,7 @@ msgstr "" " \"fum\"\n" "]" -#: ../../faq/design.rst:782 +#: ../../faq/design.rst:792 msgid "" "This list looks like it has four elements, but it actually contains three: " "\"fee\", \"fiefoo\" and \"fum\". Always adding the comma avoids this source " @@ -1562,7 +1608,7 @@ msgstr "" "這個串列看起來有四個元素,但他其實只有三個:「fee」、「fiefoo」、「fum」。永" "遠記得加上逗號以避免這種錯誤。" -#: ../../faq/design.rst:785 +#: ../../faq/design.rst:795 msgid "" "Allowing the trailing comma may also make programmatic code generation " "easier." From f897b17d55bece5ba4353c893da60393a8eec11c Mon Sep 17 00:00:00 2001 From: BIT074 Date: Mon, 5 Oct 2026 23:04:18 +0800 Subject: [PATCH 2/2] resolve comment --- faq/design.po | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/faq/design.po b/faq/design.po index 4b54903a2e5..8b640fdae54 100644 --- a/faq/design.po +++ b/faq/design.po @@ -1269,13 +1269,13 @@ msgid "" " pass\n" "..." msgstr "" -"class label(Exception): pass # declare a label\n" +"class label(Exception): pass # 宣告一個標籤\n" "\n" "try:\n" " ...\n" -" if condition: raise label() # goto label\n" +" if condition: raise label() # goto 標籤\n" " ...\n" -"except label: # where to goto\n" +"except label: # 跳轉目標\n" " pass\n" "..." @@ -1322,7 +1322,7 @@ msgstr "" #: ../../faq/design.rst:661 msgid "f = open(\"/mydir/file.txt\") # works fine!" -msgstr "f = open(\"/mydir/file.txt\") # works fine!" +msgstr "f = open(\"/mydir/file.txt\") # 正常運作!" #: ../../faq/design.rst:663 msgid "" @@ -1359,7 +1359,7 @@ msgid "" " total = total + 1 # obj.total = obj.total + 1" msgstr "" "with obj:\n" -" a = 1 # equivalent to obj.a = 1\n" +" a = 1 # 等同於 obj.a = 1\n" " total = total + 1 # obj.total = obj.total + 1" #: ../../faq/design.rst:681 @@ -1560,7 +1560,7 @@ msgstr "" "('a', 'b', 'c',)\n" "d = {\n" " \"A\": [1, 5],\n" -" \"B\": [6, 7], # last trailing comma is optional but good style\n" +" \"B\": [6, 7], # 最尾的逗號是可選的,但符合良好的規範\n" "}" #: ../../faq/design.rst:775