diff --git a/howto/functional.po b/howto/functional.po index 85b44dc8cb..c0c9d59e05 100644 --- a/howto/functional.po +++ b/howto/functional.po @@ -7,9 +7,9 @@ msgid "" msgstr "" "Project-Id-Version: Python 3.14\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2025-07-04 00:16+0000\n" -"PO-Revision-Date: 2018-05-23 14:36+0000\n" -"Last-Translator: Adrian Liaw \n" +"POT-Creation-Date: 2025-09-08 15:25+0800\n" +"PO-Revision-Date: 2025-11-21 18:25+0000\n" +"Last-Translator: Matt Wang \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" "Language: zh_TW\n" @@ -20,7 +20,7 @@ msgstr "" #: ../../howto/functional.rst:5 msgid "Functional Programming HOWTO" -msgstr "函式程式設計 HOWTO" +msgstr "如何撰寫函式程式設計 (Functional Programming)" #: ../../howto/functional.rst:0 msgid "Author" @@ -46,6 +46,9 @@ msgid "" "term:`iterator`\\s and :term:`generator`\\s and relevant library modules " "such as :mod:`itertools` and :mod:`functools`." msgstr "" +"在本文件中,我們將介紹適合以函式風格實作程式的 Python 功能。在介紹了函式程" +"式設計的概念之後,我們將了解諸如 :term:`iterator` 和 :term:`generator` 等語言" +"特性以及相關的函式庫模組,例如 :mod:`itertools` 和 :mod:`functools`。" #: ../../howto/functional.rst:18 msgid "Introduction" @@ -57,11 +60,13 @@ msgid "" "just interested in learning about Python language features, skip to the next " "section on :ref:`functional-howto-iterators`." msgstr "" +"本節說明函式程式設計的基本概念;如果你只想學習有關 Python 語言功能,請跳至下" +"一節 :ref:`functional-howto-iterators`。" #: ../../howto/functional.rst:24 msgid "" "Programming languages support decomposing problems in several different ways:" -msgstr "" +msgstr "程式語言支援多種不同的方式來拆解問題:" #: ../../howto/functional.rst:26 msgid "" @@ -69,6 +74,8 @@ msgid "" "instructions that tell the computer what to do with the program's input. C, " "Pascal, and even Unix shells are procedural languages." msgstr "" +"大部分的程式語言都是\\ **程序式**\\ 的:程式是由一連串指令所組成,這些指令告訴電" +"腦要如何處理程式的輸入。C、Pascal 以及甚至 UNIX shells 都屬於程序式語言。" #: ../../howto/functional.rst:30 msgid "" @@ -79,6 +86,10 @@ msgid "" "to retrieve, and the SQL engine decides whether to scan tables or use " "indexes, which subclauses should be performed first, etc." msgstr "" +"在\\ **宣告式 (declarative)** 語言中,你撰寫的規範描述待解決的問題,而語言" +"的實作會決定如何高效地執行運算。SQL 是你最可能熟悉的宣告式語言;一個 SQL 查詢描述" +"了你要擷取的資料集,而 SQL 引擎會決定是否掃描表或使用索引、哪些副子句應優先執" +"行等等。" #: ../../howto/functional.rst:37 msgid "" @@ -88,6 +99,9 @@ msgid "" "and Python are languages that support object-oriented programming, but don't " "force the use of object-oriented features." msgstr "" +"**物件導向 (Object-oriented)** 程式會操作物件的集合。物件具有內部狀態,並支" +"援可以查詢或修改此內部狀態的方法。Smalltalk 和 Java 是物件導向語言。C++ 和 " +"Python 則是支援物件導向程式設計的語言,但不強制使用物件導向特性。" #: ../../howto/functional.rst:43 msgid "" @@ -97,6 +111,9 @@ msgid "" "known functional languages include the ML family (Standard ML, OCaml, and " "other variants) and Haskell." msgstr "" +"**函式 (Functional)** 程式設計將問題分解為一組函式。理想情況下,函式只接收" +"輸入並產生輸出,且不具備任何會影響特定輸入所產生輸出的內部狀態。知名的函式程" +"式語言包括 ML 系列 (Standard ML、OCaml 和其他變種) 和 Haskell。" #: ../../howto/functional.rst:49 msgid "" @@ -110,6 +127,12 @@ msgid "" "GUI might be object-oriented while the processing logic is procedural or " "functional, for example." msgstr "" +"某些電腦語言的設計者會選擇強調某一特定的程式設計方法,這通常使得採用不同方法" +"撰寫程式變得困難。其他語言則是多範型 (multi-paradigm) 語言,支援數種不同的方" +"法。Lisp、C++ 和 Python 都是多範型語言;你可以在這些語言中撰寫主要為程序式、" +"物件導向或函式式的程式或函式庫。在大型程式中,不同的區塊可能會使用不同的方" +"法;例如,圖形使用者介面 (GUI) 可能是物件導向,而處理邏輯則可能是程序式或函" +"式式。" #: ../../howto/functional.rst:60 msgid "" @@ -121,6 +144,10 @@ msgid "" "side effects means not using data structures that get updated as a program " "runs; every function's output must only depend on its input." msgstr "" +"在函式程式中,輸入會流經一組函式。每個函式都會對其輸入進行操作並產生某些輸" +"出。函式風格不鼓勵帶有 side effects 的函式,即修改內部狀態或做出其他在函式回傳值中看" +"不到的變化。完全沒有 side effects 的函式稱為\\ **純函式 (purely functional)**。避免 " +"side effects 意味著不使用隨程式執行而更新的資料結構;每個函式的輸出必須僅依賴其輸入。" #: ../../howto/functional.rst:68 msgid "" @@ -132,6 +159,10 @@ msgid "" "their side effects of sending some text to the screen or pausing execution " "for a second." msgstr "" +"有些語言對純度要求非常嚴格,甚至沒有賦值陳述式(例如 ``a=3`` 或 ``c = a + " +"b``),但很難避免所有 side effects,例如將內容輸出至螢幕或寫入磁碟檔案。另一個例子是" +"呼叫 :func:`print` 或 :func:`time.sleep` 函式,這兩者都不回傳有用的值。它們僅" +"因其 side effects 而被呼叫,例如向螢幕傳送一些文字或暫停執行 1 秒鐘。" #: ../../howto/functional.rst:75 msgid "" @@ -142,6 +173,9 @@ msgid "" "assignments to local variables, but won't modify global variables or have " "other side effects." msgstr "" +"以函式風格撰寫的 Python 程式通常不會極端地避免所有 I/O 或所有賦值;相反地,它" +"們會提供一個看起來像函式的介面,但在內部仍然使用非函式特性。例如,函式的實作" +"仍會使用賦值給區域變數,但不會修改全域變數或產生其他 side effects。" #: ../../howto/functional.rst:81 msgid "" @@ -154,6 +188,11 @@ msgid "" "approaches by writing functions that take and return instances representing " "objects in your application (e-mail messages, transactions, etc.)." msgstr "" +"函式程式設計可視為物件導向程式設計的相反。物件是包含一些內部狀態以及一組方法" +"呼叫的小膠囊,這些方法呼叫讓你可以修改此狀態,而程式則包括進行正確的狀態變" +"更。函式程式設計希望盡可能避免狀態變更,並且利用在函式之間流動的資料來運作。" +"在 Python 中,你可以透過撰寫接受並回傳實例(代表應用程式中的物件,例如電子郵" +"件訊息、交易等)的函式來結合這兩種方法。" #: ../../howto/functional.rst:90 msgid "" @@ -161,6 +200,8 @@ msgid "" "you avoid objects and side effects? There are theoretical and practical " "advantages to the functional style:" msgstr "" +"函式設計看似是一個奇怪的限制。為什麼要避免使用物件和 side effects 呢?函式風格在理論" +"和實務上都有其優勢:" #: ../../howto/functional.rst:94 msgid "Formal provability." @@ -176,7 +217,7 @@ msgstr "可組合性 (Composability)。" #: ../../howto/functional.rst:97 msgid "Ease of debugging and testing." -msgstr "容易除錯與測試。" +msgstr "除錯和測試的便利性。" #: ../../howto/functional.rst:101 msgid "Formal provability" @@ -187,6 +228,7 @@ msgid "" "A theoretical benefit is that it's easier to construct a mathematical proof " "that a functional program is correct." msgstr "" +"理論上的好處是,建構數學證明來驗證函式程式的正確性會比較容易。" #: ../../howto/functional.rst:106 msgid "" @@ -197,6 +239,9 @@ msgid "" "looks right; the goal is instead a rigorous proof that a program produces " "the right result for all possible inputs." msgstr "" +"長久以來,研究者一直致力於尋找以數學方式證明程式正確性的方法。這與在大量輸入" +"上測試程式並總結其輸出通常是正確的,或閱讀程式的原始碼並總結程式碼看起來正確" +"不同;目標是嚴格證明程式對於所有可能的輸入都能產生正確的結果。" #: ../../howto/functional.rst:113 msgid "" @@ -208,6 +253,10 @@ msgid "" "This continues until you reach the end of the program, at which point the " "invariants should match the desired conditions on the program's output." msgstr "" +"證明程式正確性所使用的技術是寫下\\ **不變式 (invariants)**,即輸入資料和程式" +"變數永遠為真的屬性。對於每一行程式碼,你需要證明如果不變式 X 和 Y 在該行執行" +"\\ **之前**\\ 為真,那麼稍微不同的不變式 X' 和 Y' 在該行執行\\ **之後**\\ 也為" +"真。這個過程持續到程式結束,此時不變式應該符合程式輸出的預期條件。" #: ../../howto/functional.rst:121 msgid "" @@ -216,6 +265,8 @@ msgid "" "invariants that were true before the assignment without producing any new " "invariants that can be propagated onward." msgstr "" +"函式程式設計避免賦值的原因在於,使用這種技術時賦值很難處理;賦值可能會破壞在" +"賦值之前為真的不變式,而不會產生任何可以向後傳遞的新不變式。" #: ../../howto/functional.rst:126 msgid "" @@ -228,6 +279,11 @@ msgid "" "the question of verifying the proof; maybe there's an error in it, and you " "wrongly believe you've proved the program correct." msgstr "" +"不幸的是,證明程式的正確性在很大程度上是不切實際的,且與 Python 軟體無關。即" +"使是簡單的程式,其正確性證明也需要好幾頁;中等複雜度程式的正確性證明將會非常" +"龐大,而你每天使用的程式(Python 直譯器、XML 剖析器、網頁瀏覽器)很少或甚至" +"沒有任何一個能被證明是正確的。即使你寫下或產生了一個證明,接下來還有驗證該證" +"明的問題;也許證明中存在錯誤,而你錯誤地相信自己已經證明了程式的正確性。" #: ../../howto/functional.rst:137 msgid "Modularity" @@ -241,14 +297,17 @@ msgid "" "thing than a large function that performs a complicated transformation. " "Small functions are also easier to read and to check for errors." msgstr "" +"函式程式設計更實際的好處是它強迫你將問題拆解成小塊。因此程式會變得更模組化。" +"相較於執行複雜轉換的大型函式,指定和撰寫只做一件事的小型函式會更容易。小型函" +"式也更容易閱讀和檢查錯誤。" #: ../../howto/functional.rst:147 msgid "Ease of debugging and testing" -msgstr "容易除錯與測試" +msgstr "除錯和測試的便利性" #: ../../howto/functional.rst:149 msgid "Testing and debugging a functional-style program is easier." -msgstr "" +msgstr "測試和除錯函式風格的程式比較容易。" #: ../../howto/functional.rst:151 msgid "" @@ -258,6 +317,9 @@ msgid "" "intermediate inputs and outputs to quickly isolate the function that's " "responsible for a bug." msgstr "" +"除錯被簡化,因為函式通常都很小且規格明確。當程式無法運作時,每個函式都是一" +"個介面點,你可以在此檢查資料是否正確。你可以查看中間的輸入和輸出,快速找出造" +"成錯誤的函式。" #: ../../howto/functional.rst:156 msgid "" @@ -266,6 +328,8 @@ msgid "" "before running a test; instead you only have to synthesize the right input " "and then check that the output matches expectations." msgstr "" +"測試變得更容易,因為每個函式都可能成為單元測試的對象。函式不依賴於需要在執行" +"測試前複製的系統狀態;你只需要合成正確的輸入,然後檢查輸出是否符合預期。" #: ../../howto/functional.rst:163 msgid "Composability" @@ -281,6 +345,10 @@ msgid "" "that takes a filename and returns its contents, can be applied to many " "different situations." msgstr "" +"當你開發函式風格的程式時,你會撰寫許多具有不同輸入和輸出的函式。其中一些函式" +"將不可避免地針對特定應用而特製化,但其他函式則可在各式各樣的程式中使用。例" +"如,一個接受目錄路徑並回傳該目錄中所有 XML 檔案的函式,或一個接受檔名並回傳其" +"內容的函式,可以應用於許多不同的情況。" #: ../../howto/functional.rst:172 msgid "" @@ -288,6 +356,8 @@ msgid "" "assemble new programs by arranging existing functions in a new configuration " "and writing a few functions specialized for the current task." msgstr "" +"隨著時間推移,你會建立個人的工具函式庫。通常你只需要以新的配置安排現有的函" +"式,並撰寫一些針對目前任務特製化的函式,就能組裝新程式。" #: ../../howto/functional.rst:180 msgid "Iterators" @@ -298,6 +368,7 @@ msgid "" "I'll start by looking at a Python language feature that's an important " "foundation for writing functional-style programs: iterators." msgstr "" +"我將從一個對於撰寫函式風格程式來說非常重要的 Python 語言特性開始:疊代器。" #: ../../howto/functional.rst:185 msgid "" @@ -309,6 +380,11 @@ msgid "" "exception. Iterators don't have to be finite, though; it's perfectly " "reasonable to write an iterator that produces an infinite stream of data." msgstr "" +"疊代器 (iterator) 是代表資料串流的物件;此物件會一次回傳一個元素。Python 疊代" +"器必須支援名為 :meth:`~iterator.__next__` 的方法,該方法不接受任何引數,且總" +"是回傳串流中的下一個元素。如果串流中沒有更多元素,:meth:`~iterator.__next__` " +"必須引發 :exc:`StopIteration` 例外。不過疊代器不一定要是有限的;撰寫產生無限" +"資料串流的疊代器是完全合理的。" #: ../../howto/functional.rst:193 msgid "" @@ -319,10 +395,14 @@ msgid "" "lists and dictionaries. An object is called :term:`iterable` if you can get " "an iterator for it." msgstr "" +"內建的 :func:`iter` 函式接受任意物件,並嘗試回傳一個會回傳該物件內容或元素的" +"疊代器,如果該物件不支援疊代則引發 :exc:`TypeError`。Python 的數種內建資料型" +"別支援疊代,最常見的是串列和字典。如果你能夠取得物件的疊代器,該物件就稱為 :" +"term:`iterable`。" #: ../../howto/functional.rst:200 msgid "You can experiment with the iteration interface manually:" -msgstr "" +msgstr "你可以手動實驗疊代介面:" #: ../../howto/functional.rst:218 msgid "" @@ -331,6 +411,9 @@ msgid "" "Y``, Y must be an iterator or some object for which :func:`iter` can create " "an iterator. These two statements are equivalent::" msgstr "" +"Python 在數種不同的情境中預期可疊代物件,其中最重要的是 :keyword:`for` 陳述" +"式。在陳述式 ``for X in Y`` 中,Y 必須是疊代器或 :func:`iter` 可以為其建立疊" +"代器的物件。以下兩個陳述式是等價的:" #: ../../howto/functional.rst:224 msgid "" @@ -351,12 +434,15 @@ msgid "" "Iterators can be materialized as lists or tuples by using the :func:`list` " "or :func:`tuple` constructor functions:" msgstr "" +"使用 :func:`list` 或 :func:`tuple` 建構函式可以將疊代器具現為串列或元組:" #: ../../howto/functional.rst:239 msgid "" "Sequence unpacking also supports iterators: if you know an iterator will " "return N elements, you can unpack them into an N-tuple:" msgstr "" +"序列解包 (Sequence unpacking) 也支援疊代器:如果你知道一個疊代器會回傳 N 個元" +"素,你可以將它們解包成一個 N-tuple:" #: ../../howto/functional.rst:248 msgid "" @@ -368,6 +454,11 @@ msgid "" "func:`min` will never return, and if the element X never appears in the " "stream, the ``\"in\"`` and ``\"not in\"`` operators won't return either." msgstr "" +"內建函式如 :func:`max` 和 :func:`min` 可以接受單一疊代器引數,並回傳最大或最" +"小的元素。``\"in\"`` 和 ``\"not in\"`` 運算子也支援疊代器:如果在疊代器回傳的" +"串流中找到 X,則 ``X in iterator`` 為真。如果疊代器是無限的,你會遇到明顯的問" +"題;:func:`max` 和 :func:`min` 永遠不會回傳,而如果元素 X 永遠不會出現在串流" +"中,``\"in\"`` 和 ``\"not in\"`` 運算子也不會回傳。" #: ../../howto/functional.rst:256 msgid "" @@ -379,10 +470,14 @@ msgid "" "need to do something different with the same stream, you'll have to create a " "new iterator." msgstr "" +"注意,你只能在疊代器中向前移動;沒有辦法取得先前的元素、重置疊代器或複製它。" +"疊代器物件可以選擇性地提供這些額外功能,但疊代協定只規定了 :meth:`~iterator." +"__next__` 方法。因此函式可能會耗盡疊代器的所有輸出,如果你需要對相同的串流做" +"不同的事情,則必須建立新的疊代器。" #: ../../howto/functional.rst:266 msgid "Data Types That Support Iterators" -msgstr "" +msgstr "支援疊代器的資料型別" #: ../../howto/functional.rst:268 msgid "" @@ -390,12 +485,15 @@ msgid "" "Python sequence type, such as strings, will automatically support creation " "of an iterator." msgstr "" +"我們已經看過了串列和元組如何支援疊代器。事實上,任何 Python 序列型別(例如字" +"串)都會自動支援建立疊代器。" #: ../../howto/functional.rst:272 msgid "" "Calling :func:`iter` on a dictionary returns an iterator that will loop over " "the dictionary's keys::" msgstr "" +"對字典呼叫 :func:`iter` 會回傳一個疊代器,該疊代器會遍歷字典的鍵:" #: ../../howto/functional.rst:275 msgid "" @@ -439,20 +537,27 @@ msgid "" "to be the same as the insertion order. In earlier versions, the behaviour " "was unspecified and could vary between implementations." msgstr "" +"注意,從 Python 3.7 開始,字典的疊代順序保證與插入順序相同。在較早的版本中," +"此行為未定義且可能因實作而異。" #: ../../howto/functional.rst:296 msgid "" "Applying :func:`iter` to a dictionary always loops over the keys, but " "dictionaries have methods that return other iterators. If you want to " -"iterate over values or key/value pairs, you can explicitly call the :meth:" -"`~dict.values` or :meth:`~dict.items` methods to get an appropriate iterator." +"iterate over values or key/value pairs, you can explicitly call " +"the :meth:`~dict.values` or :meth:`~dict.items` methods to get an " +"appropriate iterator." msgstr "" +"將 :func:`iter` 應用於字典時總是會遍歷鍵,但字典有回傳其他疊代器的方法。如果" +"你想要疊代值或鍵/值配對,可以明確呼叫 :meth:`~dict.values` 或 :meth:`~dict." +"items` 方法來取得適當的疊代器。" #: ../../howto/functional.rst:302 msgid "" "The :func:`dict` constructor can accept an iterator that returns a finite " "stream of ``(key, value)`` tuples:" msgstr "" +":func:`dict` 建構式可接受回傳有限 ``(key, value)`` 元組之串流的疊代器:" #: ../../howto/functional.rst:309 msgid "" @@ -460,6 +565,9 @@ msgid "" "method until there are no more lines in the file. This means you can read " "each line of a file like this::" msgstr "" +"檔案還支援疊代,我們可以呼叫 :meth:`~io.TextIOBase.readline` 方法來逐行讀取檔" +"案,直到檔案結束為止。也就是說,我們可以透過以下方式來逐一讀取檔案中的每一" +"行: ::" #: ../../howto/functional.rst:313 msgid "" @@ -467,12 +575,16 @@ msgid "" " # do something for each line\n" " ..." msgstr "" +"for line in file:\n" +" # 為每一列都做某些事情\n" +" ..." #: ../../howto/functional.rst:317 msgid "" "Sets can take their contents from an iterable and let you iterate over the " "set's elements::" msgstr "" +"集合 (set) 可以從可疊代物件取得其內容,並讓你疊代集合的元素:" #: ../../howto/functional.rst:320 msgid "" @@ -498,7 +610,7 @@ msgstr "" #: ../../howto/functional.rst:333 msgid "Generator expressions and list comprehensions" -msgstr "產生器運算式與串列綜合運算式" +msgstr "產生器運算式與串列綜合運算" #: ../../howto/functional.rst:335 msgid "" @@ -508,6 +620,9 @@ msgid "" "strip off trailing whitespace from each line or extract all the strings " "containing a given substring." msgstr "" +"對疊代器的輸出進行的兩個常見操作是 1) 對每個元素執行某些操作、2) 選擇符合某些" +"條件的元素子集。例如,給定一個字串串列,你可能想要去除每行尾端的空白字元,或" +"擷取所有包含特定子字串的字串。" #: ../../howto/functional.rst:341 msgid "" @@ -516,6 +631,10 @@ msgid "" "functional programming language Haskell (https://www.haskell.org/). You can " "strip all the whitespace from a stream of strings with the following code::" msgstr "" +"串列綜合運算和產生器運算式(list comprehensions 與 generator expressions,簡" +"稱 \"listcomps\" 和 \"genexps\")是對於這類操作的一個簡潔的表示法,為借用自函" +"式程式語言 Haskell (https://www.haskell.org/)。你可以使用以下程式碼從字串串流" +"中刪除所有空格: ::" #: ../../howto/functional.rst:346 msgid "" @@ -532,13 +651,13 @@ msgstr "" ">>> # 產生器運算式 -- 回傳疊代器\n" ">>> stripped_iter = (line.strip() for line in line_list)\n" "\n" -">>> # 串列綜合運算式 -- 回傳串列\n" +">>> # 串列綜合運算 -- 回傳串列\n" ">>> stripped_list = [line.strip() for line in line_list]" #: ../../howto/functional.rst:354 msgid "" "You can select only certain elements by adding an ``\"if\"`` condition::" -msgstr "" +msgstr "你可以透過加入 ``\"if\"`` 條件來只選擇出特定的元素: ::" #: ../../howto/functional.rst:356 msgid "" @@ -558,6 +677,10 @@ msgid "" "infinite stream or a very large amount of data. Generator expressions are " "preferable in these situations." msgstr "" +"使用串列綜合運算,你會得到一個 Python 串列;``stripped_list`` 是包含結果行的" +"串列,而不是疊代器。產生器運算式回傳一個會根據需要計算值的疊代器,不需要一次" +"將所有值具現化。這意味著如果你正在使用回傳無限串流或非常大量資料的疊代器,串" +"列綜合運算就不適用。在這些情況下,產生器運算式較為適合。" #: ../../howto/functional.rst:366 msgid "" @@ -565,6 +688,8 @@ msgid "" "comprehensions are surrounded by square brackets (\"[]\"). Generator " "expressions have the form::" msgstr "" +"產生器運算式由小括號(\"()\")包圍,而串列綜合運算由方括號(\"[]\")包圍。產" +"生器運算式的形式為:" #: ../../howto/functional.rst:370 msgid "" @@ -593,6 +718,8 @@ msgid "" "Again, for a list comprehension only the outside brackets are different " "(square brackets instead of parentheses)." msgstr "" +"再次提醒,串列綜合運算只有外層括號不同(使用方括號 (square brackets) 而非小括" +"號 (parentheses))。" #: ../../howto/functional.rst:383 msgid "" @@ -601,6 +728,8 @@ msgid "" "``expression`` is only evaluated and added to the result when ``condition`` " "is true." msgstr "" +"產生的輸出元素將是 ``expression`` 的連續值。``if`` 子句都是可選的;如果存在," +"只有當 ``condition`` 為真時,``expression`` 才會被求值並加入結果中。" #: ../../howto/functional.rst:387 msgid "" @@ -608,6 +737,8 @@ msgid "" "parentheses signalling a function call also count. If you want to create an " "iterator that will be immediately passed to a function you can write::" msgstr "" +"產生器運算式總是必須寫在括號內,但表示函式呼叫的括號也算在內。如果你想要建立" +"一個會立即傳遞給函式的疊代器,可以寫成:" #: ../../howto/functional.rst:391 msgid "obj_total = sum(obj.count for obj in list_all_objects())" @@ -622,12 +753,17 @@ msgid "" "looped over for each resulting pair of elements from ``sequence1`` and " "``sequence2``." msgstr "" +"``for...in`` 子句包含要疊代的序列。序列的長度不必相同,因為它們是從左到右疊" +"代,**不是**並行的。對於 ``sequence1`` 中的每個元素,``sequence2`` 都會從頭" +"開始迴圈。然後對於來自 ``sequence1`` 和 ``sequence2`` 的每個結果元素配對," +"``sequence3`` 都會迴圈。" #: ../../howto/functional.rst:399 msgid "" "To put it another way, a list comprehension or generator expression is " "equivalent to the following Python code::" msgstr "" +"換句話說,串列綜合運算或產生器運算式相當於以下 Python 程式碼:" #: ../../howto/functional.rst:402 msgid "" @@ -645,6 +781,18 @@ msgid "" " # Output the value of\n" " # the expression." msgstr "" +"for expr1 in sequence1:\n" +" if not (condition1):\n" +" continue # 跳過此元素\n" +" for expr2 in sequence2:\n" +" if not (condition2):\n" +" continue # 跳過此元素\n" +" ...\n" +" for exprN in sequenceN:\n" +" if not (conditionN):\n" +" continue # 跳過此元素\n" +"\n" +" # 輸出運算式的值" #: ../../howto/functional.rst:416 msgid "" @@ -653,6 +801,8 @@ msgid "" "the lengths of all the sequences. If you have two lists of length 3, the " "output list is 9 elements long:" msgstr "" +"這表示當有多個 ``for...in`` 子句但沒有 ``if`` 子句時,輸出的長度會等於所有序" +"列長度的乘積。如果你有兩個長度為 3 的串列,則輸出串列的長度為 9 個元素:" #: ../../howto/functional.rst:428 msgid "" @@ -660,6 +810,8 @@ msgid "" "is creating a tuple, it must be surrounded with parentheses. The first list " "comprehension below is a syntax error, while the second one is correct::" msgstr "" +"為避免在 Python 文法中引入歧義,如果 ``expression`` 要建立元組,它必須用括號" +"包圍。下面的第一個串列綜合運算是語法錯誤,而第二個是正確的:" #: ../../howto/functional.rst:432 msgid "" @@ -683,6 +835,8 @@ msgid "" "writing iterators. Regular functions compute a value and return it, but " "generators return an iterator that returns a stream of values." msgstr "" +"產生器是一種特殊類別的函式,可簡化撰寫疊代器的任務。一般函式會計算一個值並回" +"傳它,但產生器回傳一個會回傳值的串流的疊代器。" #: ../../howto/functional.rst:445 msgid "" @@ -696,10 +850,16 @@ msgid "" "function where it left off? This is what generators provide; they can be " "thought of as resumable functions." msgstr "" +"你無疑熟悉在 Python 或 C 中一般函式呼叫的運作方式。當你呼叫一個函式時,它會取" +"得一個私有的命名空間來建立其區域變數。當函式到達 ``return`` 陳述式時,區域變" +"數會被銷毀,而值會被回傳給呼叫者。之後對同一個函式的呼叫會建立新的私有命名空" +"間和一組全新的區域變數。但是,如果在離開函式時不丟棄區域變數會怎樣?如果你稍" +"後可以從函式離開的地方恢復它會怎樣?這就是產生器所提供的;它們可以被視為可恢" +"復的函式。" #: ../../howto/functional.rst:454 msgid "Here's the simplest example of a generator function:" -msgstr "以下是最簡單的產生器函式範例:" +msgstr "以下是產生器函式最簡單的範例:" #: ../../howto/functional.rst:460 msgid "" @@ -707,6 +867,8 @@ msgid "" "this is detected by Python's :term:`bytecode` compiler which compiles the " "function specially as a result." msgstr "" +"任何包含 :keyword:`yield` 關鍵字的函式都是產生器函式;Python 的 :term:" +"`bytecode` 編譯器會偵測到這一點,並因此特別地編譯該函式。" #: ../../howto/functional.rst:464 msgid "" @@ -719,16 +881,23 @@ msgid "" "preserved. On the next call to the generator's :meth:`~generator.__next__` " "method, the function will resume executing." msgstr "" +"當你呼叫產生器函式時,它不會回傳單一的值;而是回傳支援疊代器協定的產生器物" +"件。在執行 ``yield`` 運算式時,產生器會輸出 ``i`` 的值,類似於 ``return`` 陳" +"述式。``yield`` 和 ``return`` 陳述式之間的最大差別在於,到達 ``yield`` 時,產" +"生器的執行狀態會被暫停,且區域變數會被保留。在下次呼叫產生器的 :meth:" +"`~generator.__next__` 方法時,函式會恢復執行。" #: ../../howto/functional.rst:473 msgid "Here's a sample usage of the ``generate_ints()`` generator:" -msgstr "以下是 ``generate_ints()`` 產生器的使用範例:" +msgstr "以下是 ``generate_ints()`` 產生器的一個範例使用:" #: ../../howto/functional.rst:490 msgid "" "You could equally write ``for i in generate_ints(5)``, or ``a, b, c = " "generate_ints(3)``." msgstr "" +"你可以同樣寫為 ``for i in generate_ints(5)``,或者是 ``a, b, c = " +"generate_ints(3)``。" #: ../../howto/functional.rst:493 msgid "" @@ -737,6 +906,9 @@ msgid "" "method. Once this happens, or the bottom of the function is reached, the " "procession of values ends and the generator cannot yield any further values." msgstr "" +"在產生器函式中,使用 ``return value`` 會導致從 :meth:`~generator.__next__` 方" +"法中拋出 ``StopIteration(value)``。一旦此情況發生或函式已執行到最深層,數值序" +"列即停止進展且該產生器函式無法再繼續或提供後續之值。" #: ../../howto/functional.rst:498 msgid "" @@ -747,6 +919,10 @@ msgid "" "method increment ``self.count`` and return it. However, for a moderately " "complicated generator, writing a corresponding class can be much messier." msgstr "" +"你可以透過手動編寫類別,並將所有產生器的區域變數存儲為實例變數感受到產生器效" +"果。例如,回傳一個整數串列可透過設置「self.count」為0,並在: meth:`〜" +"iterator. __ next__`方法中增加「self.count」並回傳它來完成。但是,對於中等程" +"度複雜的產生器 ,編寫相應的類可能會更加混亂。" #: ../../howto/functional.rst:506 msgid "" @@ -755,6 +931,8 @@ msgid "" "one generator that implements an in-order traversal of a tree using " "generators recursively. ::" msgstr "" +"Python 函式庫內所附的測試套件 :source:`Lib/test/test_generators.py` 包含許多" +"有趣的範例。以下是一個使用遞迴單元產生器實作樹之中序走訪程式範例。 ::" #: ../../howto/functional.rst:511 msgid "" @@ -769,6 +947,16 @@ msgid "" " for x in inorder(t.right):\n" " yield x" msgstr "" +"# 一個遞迴產生器,以中序產生樹葉節點。\n" +"def inorder(t):\n" +" if t:\n" +" for x in inorder(t.left):\n" +" yield x\n" +"\n" +" yield t.label\n" +"\n" +" for x in inorder(t.right):\n" +" yield x" #: ../../howto/functional.rst:522 msgid "" @@ -778,10 +966,13 @@ msgid "" "knight to every square of an NxN chessboard without visiting any square " "twice)." msgstr "" +"在 ``test_generators.py`` 中有另外兩個例子解決 N 皇后問題 (在 NxN 的象棋盤上" +"放置 N 個皇后,使得每個皇后不互相威脅)以及騎士巡迴(找到一條路徑讓騎士遍歷 " +"NxN 的棋盤且不重複造訪任何一格)。" #: ../../howto/functional.rst:530 msgid "Passing values into a generator" -msgstr "" +msgstr "傳遞參數至產生器" #: ../../howto/functional.rst:532 msgid "" @@ -792,13 +983,18 @@ msgid "" "variable or by passing in some mutable object that callers then modify, but " "these approaches are messy." msgstr "" +"在 Python 2.4 及之前的版本中,產生器只能產生輸出。一旦啟動產生器程式碼以建立" +"疊代器,當其執行被恢復時就無法傳遞任何新資訊進入函式中。你可以透過使產生器查" +"看全局變數或傳入某些可變對象然後由調用者修改來編寫此功能,但這種方法很麻煩。" #: ../../howto/functional.rst:539 msgid "" -"In Python 2.5 there's a simple way to pass values into a generator. :keyword:" -"`yield` became an expression, returning a value that can be assigned to a " -"variable or otherwise operated on::" +"In Python 2.5 there's a simple way to pass values into a " +"generator. :keyword:`yield` became an expression, returning a value that can " +"be assigned to a variable or otherwise operated on::" msgstr "" +"在Python 2.5中,有一種簡單的方法可以將值傳遞給產生器。``yield`` 成為一個表達" +"式,會回傳可指派給變數或進行其他操作的值: ::" #: ../../howto/functional.rst:543 msgid "val = (yield i)" @@ -811,6 +1007,8 @@ msgid "" "above example. The parentheses aren't always necessary, but it's easier to " "always add them instead of having to remember when they're needed." msgstr "" +"建議當處理 ``yield`` 回傳的值時,請**總是**加上括號,如前述範例所示。這樣做並" +"非必要,但如果一直加上括號會比記得何時需要更容易。" #: ../../howto/functional.rst:550 msgid "" @@ -820,20 +1018,28 @@ msgid "" "write ``val = yield i`` but have to use parentheses when there's an " "operation, as in ``val = (yield i) + 12``.)" msgstr "" +"(:pep:`342` 指出準確的規則,即必須為 ``yield``-expression 加入括號,除非它出" +"現在指派運算式右側的最上層運算式。這意味著如果有運算,你需要使用括號,例如: " +"``val = (yield i) + 12``,但是你也可以直接寫成 ``val = yield i`` 避免使用括號" +"以作為最上層運算式。)" #: ../../howto/functional.rst:556 msgid "" "Values are sent into a generator by calling its :meth:`send(value) " "` method. This method resumes the generator's code and the " -"``yield`` expression returns the specified value. If the regular :meth:" -"`~generator.__next__` method is called, the ``yield`` returns ``None``." +"``yield`` expression returns the specified value. If the " +"regular :meth:`~generator.__next__` method is called, the ``yield`` returns " +"``None``." msgstr "" +"透過呼叫產生器的 :meth:`send(value) ` 方法,可以將值送入產生" +"器。此方法重新啟動產生器執行程式並回傳指定的值,若是以一" +"般 :meth:`~generator.__next__` 方法呼叫時,則會回傳 `None`。" #: ../../howto/functional.rst:561 msgid "" "Here's a simple counter that increments by 1 and allows changing the value " "of the internal counter." -msgstr "" +msgstr "這裡有一個簡單的計數器,可以使內部變數增量1並允許改變其值。" #: ../../howto/functional.rst:564 msgid "" @@ -851,7 +1057,7 @@ msgstr "" " i = 0\n" " while i < maximum:\n" " val = (yield i)\n" -" # 如有提供值則改變計數器\n" +" # 如果提供了值,則改變計數器\n" " if val is not None:\n" " i = val\n" " else:\n" @@ -859,7 +1065,7 @@ msgstr "" #: ../../howto/functional.rst:576 msgid "And here's an example of changing the counter:" -msgstr "" +msgstr "下面是更改計數器的範例:" #: ../../howto/functional.rst:593 msgid "" @@ -868,12 +1074,15 @@ msgid "" "that the :meth:`~generator.send` method will be the only method used to " "resume your generator function." msgstr "" +"因為 ``yield`` 非常容易回傳成 `None`,所以你必須特別留意這種情況。除非你確定" +"只會使用 :meth:`~generator.send` 來重新啟動產生器函式,否則不要直接在運算中使" +"用其的回傳值。" #: ../../howto/functional.rst:598 msgid "" "In addition to :meth:`~generator.send`, there are two other methods on " "generators:" -msgstr "" +msgstr "除了 ``generator.send`` 方法之外,產生器還有另外兩種方法:" #: ../../howto/functional.rst:601 msgid "" @@ -881,30 +1090,40 @@ msgid "" "the generator; the exception is raised by the ``yield`` expression where the " "generator's execution is paused." msgstr "" +":meth:`throw(value) ` 用於在產生器內部引發異常;該異常由暫停" +"產生器執行的 ``yield`` 運算式引發。" #: ../../howto/functional.rst:605 msgid "" ":meth:`~generator.close` sends a :exc:`GeneratorExit` exception to the " "generator to terminate the iteration. On receiving this exception, the " -"generator's code must either raise :exc:`GeneratorExit` or :exc:" -"`StopIteration`; catching the exception and doing anything else is illegal " -"and will trigger a :exc:`RuntimeError`. :meth:`~generator.close` will also " -"be called by Python's garbage collector when the generator is garbage-" -"collected." -msgstr "" +"generator's code must either raise :exc:`GeneratorExit` " +"or :exc:`StopIteration`; catching the exception and doing anything else is " +"illegal and will trigger a :exc:`RuntimeError`. :meth:`~generator.close` " +"will also be called by Python's garbage collector when the generator is " +"garbage-collected." +msgstr "" +":meth:`~generator.close` 會向產生器傳送 :exc:`GeneratorExit` 例外來終止疊" +"代。在接收到此例外時,產生器的程式碼必須引發 :exc:`GeneratorExit` 或 :exc:" +"`StopIteration`;捕獲此例外並執行任何其他操作是不合法的,並會觸發 :exc:" +"`RuntimeError`。當產生器被垃圾回收時,Python 的垃圾回收器也會呼叫 :meth:" +"`~generator.close`。" #: ../../howto/functional.rst:613 msgid "" "If you need to run cleanup code when a :exc:`GeneratorExit` occurs, I " -"suggest using a ``try: ... finally:`` suite instead of catching :exc:" -"`GeneratorExit`." +"suggest using a ``try: ... finally:`` suite instead of " +"catching :exc:`GeneratorExit`." msgstr "" +"如果你需要在 :exc:`GeneratorExit` 發生時運行清理程式,我建議使用 ``try: ... " +"finally:`` 套件區塊替代捕獲 :exc:`GeneratorExit`。" #: ../../howto/functional.rst:616 msgid "" "The cumulative effect of these changes is to turn generators from one-way " "producers of information into both producers and consumers." msgstr "" +"這些變更的累積效應是,將產生資訊的單向產生器轉換為同時兼具生成及消費能力。" #: ../../howto/functional.rst:619 msgid "" @@ -914,6 +1133,9 @@ msgid "" "can be entered, exited, and resumed at many different points (the ``yield`` " "statements)." msgstr "" +"產生器也可以變成 **協程(coroutine)** ,一種更廣義的子程序(subroutine)形式。子" +"程序從一個點進入並在另一個點退出(函式頂部和“return”語句),但是協程可以在許多" +"不同的點中被進入、退出和恢復(``yield`` 語句)。" #: ../../howto/functional.rst:626 msgid "Built-in functions" @@ -922,29 +1144,34 @@ msgstr "內建函式" #: ../../howto/functional.rst:628 msgid "" "Let's look in more detail at built-in functions often used with iterators." -msgstr "" +msgstr "讓我們更詳細地看一下與疊代器經常使用的內建函式。" #: ../../howto/functional.rst:630 msgid "" "Two of Python's built-in functions, :func:`map` and :func:`filter` duplicate " "the features of generator expressions:" msgstr "" +"Python 內建函式 :func:`map` 和 :func:`filter` 兩個函式,都有產生器運算 expr " +"的功能。" #: ../../howto/functional.rst:633 msgid "" ":func:`map(f, iterA, iterB, ...) ` returns an iterator over the sequence" -msgstr ":func:`map(f, iterA, iterB, ...) ` 回傳一個元素為序列的疊代器" +msgstr "" +":func:`map(f, iterA, iterB...) ` 回傳一個疊代器,此疊代器針對序列進行映" +"射。" #: ../../howto/functional.rst:634 msgid "" "``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...``." msgstr "" -"``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ..." -"``。" +"``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2])...``。" +"(註解:這個是用在 Python 中「將若干個 iterable 作為參數餵給一個函式」的快速" +"寫法,`zip` 在這種情況下表現得特別優秀。)" #: ../../howto/functional.rst:644 msgid "You can of course achieve the same effect with a list comprehension." -msgstr "" +msgstr "你當然可以使用串列綜合運算來達到相同的效果。" #: ../../howto/functional.rst:646 msgid "" @@ -954,10 +1181,14 @@ msgid "" "truth value of some condition; for use with :func:`filter`, the predicate " "must take a single value." msgstr "" +":func:`filter(predicate, iter) ` 會回傳一個疊代器,其中包含符合特定條" +"件的所有序列元素。此功能與串列綜合運算類似地運作。**predicate(謂詞)** 是一" +"種函式,用於回傳某些情況下的真實值;對於 :func:`filter` 的使用,謂詞必須接受" +"單個值作為引數。" #: ../../howto/functional.rst:659 msgid "This can also be written as a list comprehension:" -msgstr "" +msgstr "這個也可以被形式化為串列綜合運算:" #: ../../howto/functional.rst:665 msgid "" @@ -965,6 +1196,8 @@ msgid "" "iterable returning 2-tuples containing the count (from *start*) and each " "element. ::" msgstr "" +":func:`enumerate(iter, start=0) ` 函式可用来列舉 iterable 中元素的" +"序号并回傳一个二元组,包含了序号(从 *start* 开始)以及每个元素。 ::" #: ../../howto/functional.rst:669 msgid "" @@ -985,6 +1218,7 @@ msgid "" ":func:`enumerate` is often used when looping through a list and recording " "the indexes at which certain conditions are met::" msgstr "" +"在遍歷串列並記錄符合特定條件的索引時,常使用 :func:`enumerate` 函式: ::" #: ../../howto/functional.rst:678 msgid "" @@ -1005,6 +1239,9 @@ msgid "" "result. The *key* and *reverse* arguments are passed through to the " "constructed list's :meth:`~list.sort` method. ::" msgstr "" +":func:`sorted(iterable, key=None, reverse=False) <排序>` 會將可疊代的 " +"iterable 內容放到一個串列中,再依照順序進行排序並回傳。*key* 和 *reverse* 引" +"數也會被傳遞給建立的串列物件使用 :meth:`~list.sort` 方法。 ::" #: ../../howto/functional.rst:688 msgid "" @@ -1031,7 +1268,7 @@ msgstr "" #: ../../howto/functional.rst:698 msgid "" "(For a more detailed discussion of sorting, see the :ref:`sortinghowto`.)" -msgstr "" +msgstr "(關於排序的更詳細討論,請參閱 :ref:`sortinghowto`。)" #: ../../howto/functional.rst:701 msgid "" @@ -1040,12 +1277,17 @@ msgid "" "any element in the iterable is a true value, and :func:`all` returns " "``True`` if all of the elements are true values:" msgstr "" +"`:func:`any(iter) ` 與 :func:`all(iter) ` 是內建函式,用於檢查可疊" +"代物件的真實值。:func:`any` 會在可疊代物件中任意一個元素為真時回傳``True``;" +"而 :func:`all` 則會在所有元素都為真時回傳``True``:" #: ../../howto/functional.rst:720 msgid "" ":func:`zip(iterA, iterB, ...) ` takes one element from each iterable " "and returns them in a tuple::" msgstr "" +":func:`zip(iterA, iterB, ...) ` 針對每個可疊代物件取出一個元素,並以 " +"tuple 的形式回傳: ::" #: ../../howto/functional.rst:723 msgid "" @@ -1062,6 +1304,9 @@ msgid "" "they're requested. (The technical term for this behaviour is `lazy " "evaluation `__.)" msgstr "" +"它並未構建一個記憶體串列而在回傳前耗盡所有輸入的疊代器;若要求時,它只有構建" +"元組才會回傳。(這種行為的技術名詞是「`惰性計算 `__。)" #: ../../howto/functional.rst:731 msgid "" @@ -1069,6 +1314,8 @@ msgid "" "length. If the iterables are of different lengths, the resulting stream " "will be the same length as the shortest iterable. ::" msgstr "" +"此疊代器適用於長度相同的可疊代物件。如果這些可疊代物件的長度不同,則結果將與" +"最短可疊代物件一樣長。 ::" #: ../../howto/functional.rst:735 msgid "" @@ -1084,10 +1331,12 @@ msgid "" "the longer iterators and discarded. This means you can't go on to use the " "iterators further because you risk skipping a discarded element." msgstr "" +"你應避免這樣做,因為可能從較長的疊代器中取出元素並將其丟棄。這意味著你不能再" +"延續使用此疊代器,否則會有被跳過的舊元素風險。" #: ../../howto/functional.rst:744 msgid "The itertools module" -msgstr "itertools 模組" +msgstr "Python 模組 itertools" #: ../../howto/functional.rst:746 msgid "" @@ -1095,26 +1344,28 @@ msgid "" "well as functions for combining several iterators. This section will " "introduce the module's contents by showing small examples." msgstr "" +"``itertools`` (疊代工具) 模組包含許多常用的疊代器,以及合併數個疊代器的函式。" +"本節會透過一些小例子來介紹模組內容。" #: ../../howto/functional.rst:750 msgid "The module's functions fall into a few broad classes:" -msgstr "" +msgstr "這個模組的函式可以分為幾個廣泛的類別:" #: ../../howto/functional.rst:752 msgid "Functions that create a new iterator based on an existing iterator." -msgstr "" +msgstr "基於現有的iterator建立新的遍歷器函式。" #: ../../howto/functional.rst:753 msgid "Functions for treating an iterator's elements as function arguments." -msgstr "" +msgstr "將疊代器的元素當作函式引數來處理的函式。" #: ../../howto/functional.rst:754 msgid "Functions for selecting portions of an iterator's output." -msgstr "" +msgstr "選取疊代器輸出的某些部分的函式。" #: ../../howto/functional.rst:755 msgid "A function for grouping an iterator's output." -msgstr "" +msgstr "一個用於將疊代器輸出分組的函式。" #: ../../howto/functional.rst:758 msgid "Creating new iterators" @@ -1127,6 +1378,9 @@ msgid "" "number, which defaults to 0, and the interval between numbers, which " "defaults to 1::" msgstr "" +":func:`itertools.count(start, step) ` 回傳一個無限的序列,其" +"中包含均勻分佈的值。你可以選擇提供起始數字(預設為 0)和數字間隔時間(預設為 " +"1): ::" #: ../../howto/functional.rst:764 msgid "" @@ -1151,6 +1405,9 @@ msgid "" "from first to last. The new iterator will repeat these elements " "infinitely. ::" msgstr "" +":func:`itertools.cycle(iter) ` 會儲存提供的可疊代物件之內容" +"並回傳一個新型別 `iterator`,該 `iterator` 將從頭到尾依序回傳它所儲存的元素。" +"新的 `iterator` 會無限次數重複這些元素。 ::" #: ../../howto/functional.rst:775 msgid "" @@ -1166,6 +1423,8 @@ msgid "" "element *n* times, or returns the element endlessly if *n* is not " "provided. ::" msgstr "" +":func:`itertools.repeat(elem, [n]) ` 回傳提供的元素 *n* " +"次,若未提供 *n* 值就無限次回傳此元素。 ::" #: ../../howto/functional.rst:781 msgid "" @@ -1186,6 +1445,9 @@ msgid "" "first iterator, then all the elements of the second, and so on, until all of " "the iterables have been exhausted. ::" msgstr "" +":func:`itertools.chain(iterA, iterB, ...) ` 會以多個疊代器為" +"輸入,回傳第一個疊代器中的所有元素,然後是第二個疊代器的所有元素以此類推直到" +"每個疊代器都已被耗盡。 ::" #: ../../howto/functional.rst:791 msgid "" @@ -1205,6 +1467,12 @@ msgid "" "and list slicing, you can't use negative values for *start*, *stop*, or " "*step*. ::" msgstr "" +":func:`itertools.islice(iter, [start], stop, [step]) ` 會回" +"傳一個疊代器的子序列 (slice)。只有一個 *stop* 參數時,會回傳前面的第 *stop* " +"個元素。如果你提供起始索引(start),會取得你想要的區間長度 *stop-start* 內元" +"素;同理,若你提供步距(step)參數值,將被省略指定元素格數後, 回傳接續該點剩下" +"满足长宽要求之子序列內容物件。請注意與 Python 的字串及清單$subscript$方式" +"(slicing)不同,在此無法使用負數來定義 $start$、$stop$ 或 $step$。 ::" #: ../../howto/functional.rst:801 msgid "" @@ -1231,6 +1499,11 @@ msgid "" "iterator, so this can consume significant memory if the iterator is large " "and one of the new iterators is consumed more than the others. ::" msgstr "" +":func:`itertools.tee(iter, [n]) ` 複製一個疊代器;它會回傳 " +"*n* 個互相獨立的疊代器,這些新的疊代器都會回傳源頭(iter)的內容。如果你對於 " +"*n* 沒有給定值,那麼預設值就是2。複製一個疊代器需要保存源頭(iter)部分內容,所" +"以如果其中一個新的iterator被 more than the others 消耗掉比其他 iterators多很" +"多時,此操作可能會消耗大量記憶體。 ::" #: ../../howto/functional.rst:816 msgid "" @@ -1254,16 +1527,22 @@ msgstr "" #: ../../howto/functional.rst:827 msgid "Calling functions on elements" -msgstr "" +msgstr "對元素呼叫函式" #: ../../howto/functional.rst:829 msgid "" "The :mod:`operator` module contains a set of functions corresponding to " -"Python's operators. Some examples are :func:`operator.add(a, b) ` (adds two values), :func:`operator.ne(a, b) ` (same as " -"``a != b``), and :func:`operator.attrgetter('id') ` " -"(returns a callable that fetches the ``.id`` attribute)." -msgstr "" +"Python's operators. Some examples are :func:`operator.add(a, b) " +"` (adds two values), :func:`operator.ne(a, b) ` " +"(same as ``a != b``), and :func:`operator.attrgetter('id') " +"` (returns a callable that fetches the ``.id`` " +"attribute)." +msgstr "" +"``:mod:`operator` 模組包含一系列對應於 Python 運算子的函式。其中有些例子如" +"下:func:`operator.add(a, b) ` (adds two " +"values), :func:`operator.ne(a, b) `\\ (與 ``a != b`` 相同)" +"和 :func:`operator.attrgetter('id') `\\ (回傳會取得 " +"``.id`` 屬性的可呼叫物件 (callable))。" #: ../../howto/functional.rst:835 msgid "" @@ -1271,6 +1550,8 @@ msgid "" "iterable will return a stream of tuples, and calls *func* using these tuples " "as the arguments::" msgstr "" +":func:`itertools.starmap(func, iter) ` 假設可疊代物件會回" +"傳元組的串流,並使用這些元組當作引數呼叫 *func*: ::" #: ../../howto/functional.rst:839 msgid "" @@ -1288,13 +1569,13 @@ msgstr "" #: ../../howto/functional.rst:847 msgid "Selecting elements" -msgstr "" +msgstr "選取元素" #: ../../howto/functional.rst:849 msgid "" "Another group of functions chooses a subset of an iterator's elements based " "on a predicate." -msgstr "" +msgstr "另一組函式是利用預定條件篩選出疊代器中的元素子集。" #: ../../howto/functional.rst:852 msgid "" @@ -1302,6 +1583,8 @@ msgid "" "the opposite of :func:`filter`, returning all elements for which the " "predicate returns false::" msgstr "" +":func:`itertools.filterfalse(predicate, iter) ` " +"是 :func:`filter` 的相反,會回傳所有於述詞函式中回傳「False」的元素: ::" #: ../../howto/functional.rst:856 msgid "" @@ -1317,6 +1600,9 @@ msgid "" "elements for as long as the predicate returns true. Once the predicate " "returns false, the iterator will signal the end of its results. ::" msgstr "" +"`itertools.takewhile(predicate, iter) `函式會回傳一直到" +"斷言條件返 回 False 為止的元素。一旦是因為斷言條件回傳了 False 而停止,該遍歷" +"器就會回報內容的結束。 ::" #: ../../howto/functional.rst:863 msgid "" @@ -1344,6 +1630,9 @@ msgid "" "elements while the predicate returns true, and then returns the rest of the " "iterable's results. ::" msgstr "" +":func:`itertools.dropwhile(predicate, iter) ` 會在 " +"predicate 回傳 True 時,捨棄元素,一直到 predicate 回傳 False 為止再回傳 " +"iterable 中剩下的結果。 ::" #: ../../howto/functional.rst:876 msgid "" @@ -1366,6 +1655,9 @@ msgid "" "corresponding element of *selectors* is true, stopping whenever either one " "is exhausted::" msgstr "" +":func:`itertools.compress(data, selectors) ` 函式需要兩個" +"疊代器,並回傳當 *selectors* 中對應的元素為 true 時 *data* 對應的元素。若其中" +"一個疊代器耗盡,該函式也會停止運行: ::" #: ../../howto/functional.rst:886 msgid "" @@ -1377,7 +1669,7 @@ msgstr "" #: ../../howto/functional.rst:891 msgid "Combinatoric functions" -msgstr "" +msgstr "組合函式" #: ../../howto/functional.rst:893 msgid "" @@ -1385,6 +1677,9 @@ msgid "" "returns an iterator giving all possible *r*-tuple combinations of the " "elements contained in *iterable*. ::" msgstr "" +":func:`itertools.combinations(iterable, r) ` 函式會回" +"傳一個疊代器物件,其中包含從 *iterable* 物件中選擇出所有可能的長度為 *r* 的元" +"素並組合成 tuple 所形成之疊代器。 ::" #: ../../howto/functional.rst:897 msgid "" @@ -1414,10 +1709,15 @@ msgstr "" msgid "" "The elements within each tuple remain in the same order as *iterable* " "returned them. For example, the number 1 is always before 2, 3, 4, or 5 in " -"the examples above. A similar function, :func:`itertools." -"permutations(iterable, r=None) `, removes this " -"constraint on the order, returning all possible arrangements of length *r*::" +"the examples above. A similar " +"function, :func:`itertools.permutations(iterable, r=None) " +"`, removes this constraint on the order, returning " +"all possible arrangements of length *r*::" msgstr "" +"每個 tuple 內的元素順序與 *iterable* 回傳時相同。例如,上述範例中數字 1 總是" +"在 2、3、4 或 5 的前面。一個類似的函" +"式 :func:`itertools.permutations(iterable, r=None) ` " +"移除了此限制,並回傳所有長度為 *r* 的可能排列: ::" #: ../../howto/functional.rst:915 msgid "" @@ -1450,12 +1750,16 @@ msgid "" "If you don't supply a value for *r* the length of the iterable is used, " "meaning that all the elements are permuted." msgstr "" +"如果你不提供 *r* 的值,將使用可疊代物件的長度來進行排列,也就是說所有元素都會" +"被重組。" #: ../../howto/functional.rst:930 msgid "" "Note that these functions produce all of the possible combinations by " "position and don't require that the contents of *iterable* are unique::" msgstr "" +"請注意,這些函式按位置產生所有可能的組合,不需要 *iterable* 的內容是唯一" +"的: ::" #: ../../howto/functional.rst:933 msgid "" @@ -1472,15 +1776,20 @@ msgid "" "The identical tuple ``('a', 'a', 'b')`` occurs twice, but the two 'a' " "strings came from different positions." msgstr "" +"相同的元組 ``('a', 'a', 'b')`` 出現了兩次,但是兩個 'a' 是來自不同的位置。" #: ../../howto/functional.rst:940 msgid "" -"The :func:`itertools.combinations_with_replacement(iterable, r) ` function relaxes a different constraint: " -"elements can be repeated within a single tuple. Conceptually an element is " -"selected for the first position of each tuple and then is replaced before " -"the second element is selected. ::" +"The :func:`itertools.combinations_with_replacement(iterable, r) " +"` function relaxes a different " +"constraint: elements can be repeated within a single tuple. Conceptually an " +"element is selected for the first position of each tuple and then is " +"replaced before the second element is selected. ::" msgstr "" +"該函式 :func:`itertools.combinations_with_replacement(iterable, r) " +"` 可以放寬限制,讓相同的元素可以在同" +"一個 tuple 內重覆出現。此函式是先選擇第一個位置的元素,接著再更換該元素單打才" +"繼續選取下一個;如此類推,直到組合成完整的 tuple。 ::" #: ../../howto/functional.rst:946 msgid "" @@ -1500,7 +1809,7 @@ msgstr "" #: ../../howto/functional.rst:955 msgid "Grouping elements" -msgstr "" +msgstr "將元素分組" #: ../../howto/functional.rst:957 msgid "" @@ -1510,6 +1819,10 @@ msgid "" "element returned by the iterable. If you don't supply a key function, the " "key is simply each element itself." msgstr "" +"最後一個要談的函式是 :func:`itertools.groupby(iter, key_func=None) " +"`。它是最複雜的之一,``key_func(elem)`` 是用來計算可疊代物" +"件中每個元素的關鍵值(key value) 的函式。如果沒有提供關鍵字 (Key function)," +"則以各個元素本身當作其鍵(key)。" #: ../../howto/functional.rst:962 msgid "" @@ -1517,6 +1830,8 @@ msgid "" "underlying iterable that have the same key value, and returns a stream of 2-" "tuples containing a key value and an iterator for the elements with that key." msgstr "" +":func:`~itertools.groupby` 會將具有相同鍵值的連續元素從底層可疊代物件中收集起" +"來,並回傳包含鍵值和該鍵下元素的疊代器2個項目的流程。" #: ../../howto/functional.rst:968 msgid "" @@ -1571,6 +1886,9 @@ msgid "" "also use the underlying iterable, so you have to consume the results of " "iterator-1 before requesting iterator-2 and its corresponding key." msgstr "" +":func:`~itertools.groupby` 假設底層的可疊代物件已基於鍵值排序。需要注意的是," +"所回傳之 iterators 也使用了原始的可疊代物件,因此必須先消耗掉 iterator-1 的結" +"果,再取用 iterator-2 及其對應鍵值。" #: ../../howto/functional.rst:997 msgid "The functools module" @@ -1580,9 +1898,12 @@ msgstr "functools 模組" msgid "" "The :mod:`functools` module contains some higher-order functions. A **higher-" "order function** takes one or more functions as input and returns a new " -"function. The most useful tool in this module is the :func:`functools." -"partial` function." +"function. The most useful tool in this module is " +"the :func:`functools.partial` function." msgstr "" +"Python 中 :mod:`functools` 模組包含了一些高階函式。**高階函式** 可以接受一個" +"或多個函式作為輸入並回傳新的函式。這個模組中最有用的工具" +"是 :func:`functools.partial` 函式。" #: ../../howto/functional.rst:1004 msgid "" @@ -1593,6 +1914,10 @@ msgid "" "filling in a value for one of ``f()``'s parameters. This is called " "\"partial function application\"." msgstr "" +"對於以函式風格撰寫的程式,有時你可能想要建立現有函式的變體,其中填入某些引" +"數。考慮一個 Python 函式 ``f(a, b, c)``;你可以希望建立一個新函式 ``g(b, " +"c)``,它等效於 ``f(1, b, c)``;這相當於為 ``f()`` 的一個參數填寫了值。這稱為" +"「部分函式應用」(\"partial function application\")。" #: ../../howto/functional.rst:1010 msgid "" @@ -1601,6 +1926,9 @@ msgid "" "resulting object is callable, so you can just call it to invoke ``function`` " "with the filled-in arguments." msgstr "" +"`:func:`~functools.partial` 的建構子接受 ``(function, arg1, arg2, ..., " +"kwarg1=value1, kwarg2=value2)`` 引數。該物件是可呼叫的(callable),因此你只需" +"呼叫該物件以填入引數,然後調用 ``function``。" #: ../../howto/functional.rst:1015 msgid "Here's a small but realistic example::" @@ -1618,21 +1946,37 @@ msgid "" "server_log = functools.partial(log, subsystem='server')\n" "server_log('Unable to open socket')" msgstr "" +"import functools\n" +"\n" +"def log(message, subsystem):\n" +" \"\"\"將 'message' 的內容寫入指定的子系統。\"\"\"\n" +" print('%s: %s' % (subsystem, message))\n" +" ...\n" +"\n" +"server_log = functools.partial(log, subsystem='server')\n" +"server_log('Unable to open socket')" #: ../../howto/functional.rst:1027 msgid "" ":func:`functools.reduce(func, iter, [initial_value]) ` " "cumulatively performs an operation on all the iterable's elements and, " "therefore, can't be applied to infinite iterables. *func* must be a function " -"that takes two elements and returns a single value. :func:`functools." -"reduce` takes the first two elements A and B returned by the iterator and " -"calculates ``func(A, B)``. It then requests the third element, C, " -"calculates ``func(func(A, B), C)``, combines this result with the fourth " -"element returned, and continues until the iterable is exhausted. If the " -"iterable returns no values at all, a :exc:`TypeError` exception is raised. " -"If the initial value is supplied, it's used as a starting point and " -"``func(initial_value, A)`` is the first calculation. ::" -msgstr "" +"that takes two elements and returns a single " +"value. :func:`functools.reduce` takes the first two elements A and B " +"returned by the iterator and calculates ``func(A, B)``. It then requests " +"the third element, C, calculates ``func(func(A, B), C)``, combines this " +"result with the fourth element returned, and continues until the iterable is " +"exhausted. If the iterable returns no values at all, a :exc:`TypeError` " +"exception is raised. If the initial value is supplied, it's used as a " +"starting point and ``func(initial_value, A)`` is the first calculation. ::" +msgstr "" +":func:`functools.reduce(func, iter, [initial_value]) ` 會對" +"可疊代的所有元素施行運算,因此無法應用在無限的可疊代物件上。*func* 必須是一個" +"接受兩個元素並回傳單一值的函式。:func:`functools.reduce` 取得由疊代器回傳的第" +"一個和第二個元素 A 和 B,並計算 ``func(A,B)``。然後請求第三個元素 C ,計算 " +"``func(func(A,B),C)``,結合此結果和回傳之第四個物件持續進行直到耗盡該疊代器。" +"如果沒有任何回傳值將引發 :exc:`TypeError` 錯誤。如果提供了起始值,則使用它做" +"為起點並進行「初始」處理,在首次遠算時會呼叫 `` func(initial_value,A)``。 ::" #: ../../howto/functional.rst:1039 msgid "" @@ -1666,12 +2010,17 @@ msgid "" "all the elements of the iterable. This case is so common that there's a " "special built-in called :func:`sum` to compute it:" msgstr "" +"如果你在 :func:`functools.reduce` 中使用 :func:`operator.add`,它將會加總可疊" +"代物件中的所有元素。這個情況非常常見,因此 Python 已內建一個名為 :func:`sum` " +"的函式用來計算:" #: ../../howto/functional.rst:1063 msgid "" "For many uses of :func:`functools.reduce`, though, it can be clearer to just " "write the obvious :keyword:`for` loop::" msgstr "" +"很多時候,在使用 :func:`functools.reduce` 時,只要寫一個明顯" +"的 :keyword:`for` 迴圈可能會更清楚易懂: ::" #: ../../howto/functional.rst:1066 msgid "" @@ -1684,14 +2033,26 @@ msgid "" "for i in [1, 2, 3]:\n" " product *= i" msgstr "" +"import functools\n" +"# 這樣:\n" +"product = functools.reduce(operator.mul, [1, 2, 3], 1)\n" +"\n" +"# 你可改寫成:\n" +"product = 1\n" +"for i in [1, 2, 3]:\n" +" product *= i" #: ../../howto/functional.rst:1075 msgid "" -"A related function is :func:`itertools.accumulate(iterable, func=operator." -"add) `. It performs the same calculation, but instead " -"of returning only the final result, :func:`~itertools.accumulate` returns an " -"iterator that also yields each partial result::" +"A related function is :func:`itertools.accumulate(iterable, " +"func=operator.add) `. It performs the same " +"calculation, but instead of returning only the final " +"result, :func:`~itertools.accumulate` returns an iterator that also yields " +"each partial result::" msgstr "" +"一個相關的函式是 :func:`itertools.accumulate(iterable, func=operator.add) " +"`。它執行相同的計算,但不僅會回傳最終結果,也會產生每個" +"部分結果的疊代器: ::" #: ../../howto/functional.rst:1080 msgid "" @@ -1718,54 +2079,58 @@ msgid "" "useful in functional-style code because they save you from writing trivial " "functions that perform a single operation." msgstr "" +"之前有提到 :mod:`operator` 模組,它包含一些對應 Python 運算子的函式。這些函式" +"常用於函式風格的程式碼中,因為可以省去寫只完成單一運算子操作的瑣碎函式。" #: ../../howto/functional.rst:1095 msgid "Some of the functions in this module are:" -msgstr "" +msgstr "本模組中的一些函式包括:" #: ../../howto/functional.rst:1097 msgid "" "Math operations: ``add()``, ``sub()``, ``mul()``, ``floordiv()``, " "``abs()``, ..." msgstr "" -"數學運算:``add()``、``sub()``、``mul()``、``floordiv()``、``abs()``..." +"數學運算:``add()``、 ``sub()``、``mul()``、``floordiv()``、``abs()`` 等等。" #: ../../howto/functional.rst:1098 msgid "Logical operations: ``not_()``, ``truth()``." -msgstr "" +msgstr "邏輯運算:``not_()``(非運算)、 ``truth()``(真值判斷)." #: ../../howto/functional.rst:1099 msgid "Bitwise operations: ``and_()``, ``or_()``, ``invert()``." -msgstr "" +msgstr "位元運算:``and_()``, ``or_()``, ``invert()``。" #: ../../howto/functional.rst:1100 msgid "" "Comparisons: ``eq()``, ``ne()``, ``lt()``, ``le()``, ``gt()``, and ``ge()``." -msgstr "" +msgstr "比較:``eq()``、``ne()``, ``lt()``, ``le()``, ``gt()`` 和 ``ge()``。" #: ../../howto/functional.rst:1101 msgid "Object identity: ``is_()``, ``is_not()``." -msgstr "" +msgstr "物件識別:``is_()``,``is_not()``。" #: ../../howto/functional.rst:1103 msgid "Consult the operator module's documentation for a complete list." -msgstr "" +msgstr "請參考 operator 模組的說明文件以取得完整清單" #: ../../howto/functional.rst:1107 msgid "Small functions and the lambda expression" -msgstr "" +msgstr "小型函式和 lambda 運算式" #: ../../howto/functional.rst:1109 msgid "" "When writing functional-style programs, you'll often need little functions " "that act as predicates or that combine elements in some way." msgstr "" +"在編寫函式程式時,你通常會需要一些小型的函式來作為斷言(predicate)或以某種方" +"式組合元素之用。" #: ../../howto/functional.rst:1112 msgid "" "If there's a Python built-in or a module function that's suitable, you don't " "need to define a new function at all::" -msgstr "" +msgstr "如果有適合的 Python 內建函式或模組函式,你完全不需要定義新的函式: ::" #: ../../howto/functional.rst:1115 msgid "" @@ -1783,6 +2148,9 @@ msgid "" "parameters, and creates an anonymous function that returns the value of the " "expression::" msgstr "" +"如果你需要的函式不存在,就需要自行撰寫。編寫小型函式的其中一種方法是使用" +"「lambda」運算式。「lambda」接受任意數量的引數及結合這些引數的運算,並建立一" +"個匿名函式以回傳運算值:" #: ../../howto/functional.rst:1123 msgid "" @@ -1798,7 +2166,7 @@ msgstr "" msgid "" "An alternative is to just use the ``def`` statement and define a function in " "the usual way::" -msgstr "" +msgstr "另一種方法是只需使用「def」陳述式,以通常的方式定義函式: ::" #: ../../howto/functional.rst:1130 msgid "" @@ -1818,7 +2186,7 @@ msgstr "" msgid "" "Which alternative is preferable? That's a style question; my usual course " "is to avoid using ``lambda``." -msgstr "" +msgstr "哪種方式比較好呢?這是個風格問題。我通常不使用「lambda」函式庫。" #: ../../howto/functional.rst:1139 msgid "" @@ -1829,6 +2197,10 @@ msgid "" "``lambda`` statement, you'll end up with an overly complicated expression " "that's hard to read. Quick, what's the following code doing? ::" msgstr "" +"我偏好使用lambda的原因之一是它定義函式時所能表達的範圍相對有限。lambda 的結果" +"必須以單一運算式計算,這意味著你無法使用多路 ``if... elif... else`` 比較或" +"``try…except`` 語句。如果你在 ``lambda`` 陳述式中嘗試完成太多工作,最終會得到" +"一個非常複雜且難以閱讀的 expression。你快速看懂以下 code 做些什麼了嗎? ::" #: ../../howto/functional.rst:1146 msgid "" @@ -1844,6 +2216,8 @@ msgid "" "figure out what's going on. Using a short nested ``def`` statements makes " "things a little bit better::" msgstr "" +"你可以自己解讀,但需要花點時間拆解這個運算式才能看懂。使用短的巢狀``def``陳述" +"句會使事情稍微好一些: ::" #: ../../howto/functional.rst:1153 msgid "" @@ -1861,7 +2235,7 @@ msgstr "" #: ../../howto/functional.rst:1159 msgid "But it would be best of all if I had simply used a ``for`` loop::" -msgstr "" +msgstr "但最好的方式還是我直接使用``for``迴圈: ::" #: ../../howto/functional.rst:1161 msgid "" @@ -1875,7 +2249,7 @@ msgstr "" #: ../../howto/functional.rst:1165 msgid "Or the :func:`sum` built-in and a generator expression::" -msgstr "" +msgstr "或使用內建的 :func:`sum` 函式與產生器運算式: ::" #: ../../howto/functional.rst:1167 msgid "total = sum(b for a, b in items)" @@ -1886,44 +2260,46 @@ msgid "" "Many uses of :func:`functools.reduce` are clearer when written as ``for`` " "loops." msgstr "" +"很多使用 :func:`functools.reduce` 的情況,如果改以 ``for`` 迴圈來表達會更清" +"楚。" #: ../../howto/functional.rst:1171 msgid "" "Fredrik Lundh once suggested the following set of rules for refactoring uses " "of ``lambda``:" -msgstr "" +msgstr "Fredrik Lundh 曾建議以下重構 ``lambda`` 使用時應遵循的一些規則:" #: ../../howto/functional.rst:1174 msgid "Write a lambda function." -msgstr "" +msgstr "撰寫一個 lambda 函式。" #: ../../howto/functional.rst:1175 msgid "Write a comment explaining what the heck that lambda does." -msgstr "" +msgstr "寫一個註解來解釋這個 lambda 到底在幹嘛。" #: ../../howto/functional.rst:1176 msgid "" "Study the comment for a while, and think of a name that captures the essence " "of the comment." -msgstr "" +msgstr "仔細閱讀註解一會兒,思考出一個能夠涵蓋該註解核心的名稱。" #: ../../howto/functional.rst:1178 msgid "Convert the lambda to a def statement, using that name." -msgstr "" +msgstr "將 lambda 轉換成 def 陳述式,並使用該名稱。" #: ../../howto/functional.rst:1179 msgid "Remove the comment." -msgstr "" +msgstr "移除註解。" #: ../../howto/functional.rst:1181 msgid "" "I really like these rules, but you're free to disagree about whether this " "lambda-free style is better." -msgstr "" +msgstr "我真的很喜歡這些規則,但如果你贊成不使用 lambda 的風格也可以。" #: ../../howto/functional.rst:1186 msgid "Revision History and Acknowledgements" -msgstr "" +msgstr "修訂歷史與致謝" #: ../../howto/functional.rst:1188 msgid "" @@ -1932,39 +2308,46 @@ msgid "" "Ian Bicking, Nick Coghlan, Nick Efford, Raymond Hettinger, Jim Jewett, Mike " "Krell, Leandro Lameiro, Jussi Salmela, Collin Winter, Blake Winton." msgstr "" +"作者想感謝以下人士對本文各版本提供意見、修正和協助,包括:Ian Bicking、Nick " +"Coghlan、Nick Efford、Raymond Hettinger、Jim Jewett、Mike Krell、Leandro " +"Lameiro、Jussi Salmela Collin Winter 和 Blake Winton。" #: ../../howto/functional.rst:1193 msgid "Version 0.1: posted June 30 2006." -msgstr "" +msgstr "0.1 版本:於 2006 年 6 月 30 日張貼。" #: ../../howto/functional.rst:1195 msgid "Version 0.11: posted July 1 2006. Typo fixes." -msgstr "" +msgstr "0.11 版本:於 2006 年 7 月 1 日發佈。修正文字錯誤。" #: ../../howto/functional.rst:1197 msgid "" "Version 0.2: posted July 10 2006. Merged genexp and listcomp sections into " "one. Typo fixes." msgstr "" +"0.2 版本:於 2006 年 7 月 10 日張貼。將產生器運算式和串列綜合運算的部分合併為" +"一個部分。修正了錯誤拼字。" #: ../../howto/functional.rst:1200 msgid "" "Version 0.21: Added more references suggested on the tutor mailing list." -msgstr "" +msgstr "0.21 版本:新增了於 tutor 郵件列表建議的更多參考資料。" #: ../../howto/functional.rst:1202 msgid "" "Version 0.30: Adds a section on the ``functional`` module written by Collin " "Winter; adds short section on the operator module; a few other edits." msgstr "" +"版本 0.30:新增了一個由 Collin Winter 編寫的「函式」模組部分;加入了有關" +"「operator」模組的簡短介紹;做了幾處其他修訂。" #: ../../howto/functional.rst:1207 msgid "References" -msgstr "" +msgstr "參考文獻" #: ../../howto/functional.rst:1210 msgid "General" -msgstr "" +msgstr "總則" #: ../../howto/functional.rst:1212 msgid "" @@ -1976,6 +2359,11 @@ msgid "" "of the design approaches described in these chapters are applicable to " "functional-style Python code." msgstr "" +"《計算機程序的結構和解釋》(Structure and Interpretation of Computer " +"Programs)是 Harold Abelson 和 Gerald Jay Sussman 與 Julie Sussman 的經典電腦" +"科學教材。該書第 2 章和第 3 章探討了使用序列和流在程式中組織資料流的方法。該" +"書範例以 Scheme 程式語言呈現,但這些章節描述的設計方法對於函式風格 Python 的" +"程式碼也很有適用性。完整文本可以在 https://mitpress.mit.edu/sicp/ 找到。" #: ../../howto/functional.rst:1220 msgid "" @@ -1989,6 +2377,8 @@ msgid "" "https://en.wikipedia.org/wiki/Functional_programming: General Wikipedia " "entry describing functional programming." msgstr "" +"https://en.wikipedia.org/wiki/Functional_programming: 這是維基百科上介紹函式" +"程式設計的條目。" #: ../../howto/functional.rst:1226 msgid "https://en.wikipedia.org/wiki/Coroutine: Entry for coroutines." @@ -2009,7 +2399,7 @@ msgstr "https://en.wikipedia.org/wiki/Currying: currying 概念的條目。" #: ../../howto/functional.rst:1233 msgid "Python-specific" -msgstr "Python 特有的" +msgstr "Python 特有" #: ../../howto/functional.rst:1235 msgid "" @@ -2018,6 +2408,8 @@ msgid "" "text processing, in the section titled \"Utilizing Higher-Order Functions in " "Text Processing\"." msgstr "" +"https://gnosis.cx/TPiP/: David Mertz 的書 :title-reference:`Python 文字處理` " +"第一章,討論利用高階函式在文字處理上的功能程式設計。" #: ../../howto/functional.rst:1240 msgid "" @@ -2026,6 +2418,10 @@ msgid "" "prog/>`__, `part 2 `__, and " "`part 3 `__," msgstr "" +"Mertz 在 IBM 的 DeveloperWorks 上撰寫了分三部分的函式程式設計相關系列文章;請" +"查閱\\ `第一部分 `__、`第二部分 " +"`__\\ 和\\ `第三部分 `__。" #: ../../howto/functional.rst:1248 msgid "Python documentation" @@ -2052,3 +2448,5 @@ msgid "" ":pep:`342`: \"Coroutines via Enhanced Generators\" describes the new " "generator features in Python 2.5." msgstr "" +":pep:`342`:「利用強化型產生器達成協同程式設計」描述的是 Python 2.5 中新增的" +"產生器功能。"