diff --git a/howto/mro.po b/howto/mro.po index e492034a8ed..a480b153b7e 100644 --- a/howto/mro.po +++ b/howto/mro.po @@ -19,7 +19,7 @@ msgstr "" #: ../../howto/mro.rst:4 msgid "The Python 2.3 Method Resolution Order" -msgstr "" +msgstr "Python 2.3 方法解析順序" #: ../../howto/mro.rst:8 msgid "" @@ -27,10 +27,13 @@ msgid "" "documentation. The Method Resolution Order discussed here was *introduced* " "in Python 2.3, but it is still used in later versions -- including Python 3." msgstr "" +"這是一份歷史文件,作為正式文件的附錄提供。此處討論的方法解析順序 (Method " +"Resolution Order) 是在 Python 2.3 中 *引入* 的,但仍在後續版本中使用,包" +"括 Python 3。" #: ../../howto/mro.rst:13 msgid "By `Michele Simionato `__." -msgstr "" +msgstr "作者:`Michele Simionato `__。" #: ../../howto/mro.rst:0 msgid "Abstract" @@ -44,10 +47,13 @@ msgid "" "not aware of other publicly available documents with the same scope, " "therefore it should be useful.*" msgstr "" +"*此文件適用於想要了解 Python 2.3 中使用的 C3 方法解析順序的 Python 程式設計" +"師。雖然它不是為初學者準備的,但透過許多實際範例進行教學。我沒找到其他具有相" +"同範圍的公開文件,因此這應該很有用。*" #: ../../howto/mro.rst:23 msgid "Disclaimer:" -msgstr "" +msgstr "免責聲明:" #: ../../howto/mro.rst:25 msgid "" @@ -56,10 +62,12 @@ msgid "" "follows* should *be correct, but I don't give any warranty. Use it at your " "own risk and peril!*" msgstr "" +"*我根據 Python 2.3 授權條款將此文件捐贈給 Python 軟體基金會。如同往常,我警告" +"讀者,以下內容 *應該* 是正確的,但我不提供任何保證。使用時風險自負!*" #: ../../howto/mro.rst:30 msgid "Acknowledgments:" -msgstr "致謝" +msgstr "致謝:" #: ../../howto/mro.rst:32 msgid "" @@ -70,14 +78,18 @@ msgid "" "Rossum who enthusiastically added this document to the official Python 2.3 " "home-page.*" msgstr "" +"*感謝 Python 郵件列表中所有給予我支持的人。Paul Foley 指出了各種不精確之處,並" +"促使我加入了區域優先順序(local precedence ordering)的部分。David Goodger 協" +"助 reStructuredText 的格式化。David Mertz 協助編輯。最後,Guido van Rossum 熱" +"情地將此文件加入到 Python 2.3 官方首頁。*" #: ../../howto/mro.rst:40 msgid "The beginning" -msgstr "" +msgstr "開端" #: ../../howto/mro.rst:42 msgid "*Felix qui potuit rerum cognoscere causas* -- Virgilius" -msgstr "" +msgstr "*Felix qui potuit rerum cognoscere causas* -- Virgilius" #: ../../howto/mro.rst:44 msgid "" @@ -91,6 +103,12 @@ msgid "" "readable discussion of the C3 algorithm for Pythonistas who want to " "understand the reasons for the change." msgstr "" +"一切始於 Samuele Pedroni 向 Python 開發郵件列表發表的貼文 [#]_。在他的貼文" +"中,Samuele 指出 Python 2.2 的方法解析順序不具單調性(monotonic),並提議以 " +"C3 方法解析順序來取代。Guido 同意他的論點,因此 Python 2.3 現在使用 C3。C3 方" +"法本身與 Python 無關,因為它是由研究 Dylan 語言的人所發明,並在一篇針對 Lisp " +"程式設計師的論文 [#]_ 中描述。本文為想要了解此變更原因的 Python 程式設計師提" +"供了 C3 演算法(希望是)易讀的討論。" #: ../../howto/mro.rst:55 msgid "" @@ -103,10 +121,15 @@ msgid "" "differs from the Python 2.2 method resolution order are so rare that no real " "breaking of code is expected. Therefore:" msgstr "" +"首先讓我指出,我要說的僅適用於 Python 2.2 中引入的 *新式類別(new style " +"classes)*:*經典類別(classic classes)* 維持其舊有的方法解析順序,即深度" +"優先然後由左至右。因此,經典類別的舊程式碼不會受到影響;即使原則上 Python 2.2 " +"新式類別的程式碼可能會受影響,但實際上 C3 解析順序與 Python 2.2 方法解析順序不" +"同的情況極為罕見,因此預期不會真正破壞程式碼。因此:" #: ../../howto/mro.rst:64 msgid "*Don't be scared!*" -msgstr "" +msgstr "*別怕!*" #: ../../howto/mro.rst:66 msgid "" @@ -116,10 +139,13 @@ msgid "" "know how multiple inheritance works, then this paper is for you. The good " "news is that things are not as complicated as you might expect." msgstr "" +"此外,除非你大量使用多重繼承且有複雜的類別階層,否則你無需了解 C3 演算法,可" +"以輕鬆跳過本文。另一方面,如果你真的想知道多重繼承如何運作,那麼本文就是為你" +"準備的。好消息是,事情並不像你想像的那樣複雜。" #: ../../howto/mro.rst:73 msgid "Let me begin with some basic definitions." -msgstr "" +msgstr "讓我從一些基本定義開始。" #: ../../howto/mro.rst:75 msgid "" @@ -127,6 +153,8 @@ msgid "" "trivial task to specify the order in which methods are overridden, i.e. to " "specify the order of the ancestors of C." msgstr "" +"給定複雜多重繼承階層中的類別 C,要指定方法被覆寫(override)的順序並非易事," +"也就是要指定 C 的祖先順序。" #: ../../howto/mro.rst:79 msgid "" @@ -134,6 +162,8 @@ msgid "" "from the nearest ancestor to the furthest, is called the class precedence " "list or the *linearization* of C." msgstr "" +"類別 C 的祖先串列(包含類別本身),從最近的祖先到最遠的祖先排序,稱為類別優先" +"串列(class precedence list)或 C 的\\ *線性化(linearization)*。" #: ../../howto/mro.rst:83 msgid "" @@ -141,6 +171,8 @@ msgid "" "linearization. In the Python literature, the idiom \"the MRO of C\" is also " "used as a synonymous for the linearization of the class C." msgstr "" +"*方法解析順序 (Method Resolution Order, MRO)* 是建構線性化的一組規則。在 " +"Python 文獻中,習慣用語「C 的 MRO」也是 C 類別線性化的同義詞。" #: ../../howto/mro.rst:88 msgid "" @@ -151,6 +183,10 @@ msgid "" "it is more difficult to construct a linearization that respects *local " "precedence ordering* and *monotonicity*." msgstr "" +"例如,在單一繼承階層的情況下,如果 C 是 C1 的子類別,而 C1 是 C2 的子類別,那" +"麼 C 的線性化就是串列 [C, C1, C2]。然而,在多重繼承階層中,線性化的建構更加複" +"雜,因為要建構一個遵守\\ *區域優先順序(local precedence ordering)* 和\\ *" +"單調性(monotonicity)* 的線性化更加困難。" #: ../../howto/mro.rst:96 msgid "" @@ -162,6 +198,10 @@ msgid "" "potentially introducing very subtle bugs. Examples where this happens will " "be shown later." msgstr "" +"我將在稍後討論區域優先順序,但可以在此給出單調性的定義。當以下條件為真時," +"MRO 具有單調性:*如果 C1 在 C 的線性化中先於 C2,那麼 C1 在 C 的任何子類別的" +"線性化中也先於 C2*。否則,衍生新類別這個看似無害的操作可能會改變方法的解析順" +"序,進而引入非常微妙的錯誤。稍後將展示發生這種情況的範例。" #: ../../howto/mro.rst:104 msgid "" @@ -169,10 +209,12 @@ msgid "" "hierarchies, where it is not possible to derive a class such that its " "linearization respects all the desired properties." msgstr "" +"並非所有類別都能進行線性化。在複雜的階層結構中,有些情況下無法衍生出一個類別," +"使其線性化遵守所有所需的屬性。" #: ../../howto/mro.rst:108 msgid "Here I give an example of this situation. Consider the hierarchy" -msgstr "" +msgstr "以下是這種情況的範例。考慮以下階層結構" #: ../../howto/mro.rst:116 msgid "" @@ -180,6 +222,8 @@ msgid "" "denoted with O the ``object`` class, which is the beginning of any hierarchy " "for new style classes:" msgstr "" +"可以用以下繼承圖來表示,其中我用 O 標示 ``object`` 類別,這是新式類別的任何階" +"層結構的起點:" #: ../../howto/mro.rst:120 msgid "" @@ -211,6 +255,8 @@ msgid "" "X precedes Y in A, but Y precedes X in B, therefore the method resolution " "order would be ambiguous in C." msgstr "" +"在這種情況下,不可能從 A 和 B 衍生出新的類別 C,因為 X 在 A 中先於 Y,但 Y 在 " +"B 中先於 X,因此 C 的方法解析順序會產生歧義。" #: ../../howto/mro.rst:137 msgid "" @@ -219,16 +265,19 @@ msgid "" "hierarchies. Python 2.2 instead does not raise an exception, but chooses an " "*ad hoc* ordering (CABXYO in this case)." msgstr "" +"Python 2.3 在這種情況下會引發例外(TypeError: MRO conflict among bases Y, X)," +"防止程式設計師建立有歧義的階層結構。Python 2.2 則不會引發例外,而是選擇 *ad " +"hoc* 順序(在這種情況下為 CABXYO)。" #: ../../howto/mro.rst:143 msgid "The C3 Method Resolution Order" -msgstr "" +msgstr "C3 方法解析順序" #: ../../howto/mro.rst:145 msgid "" "Let me introduce a few simple notations which will be useful for the " "following discussion. I will use the shortcut notation::" -msgstr "" +msgstr "讓我介紹一些簡單的符號標示法,這對以下討論很有用。我將使用簡寫符號: ::" #: ../../howto/mro.rst:148 msgid "C1 C2 ... CN" @@ -236,11 +285,11 @@ msgstr "C1 C2 ... CN" #: ../../howto/mro.rst:150 msgid "to indicate the list of classes [C1, C2, ... , CN]." -msgstr "" +msgstr "用來表示類別串列 [C1, C2, ... , CN]。" #: ../../howto/mro.rst:152 msgid "The *head* of the list is its first element::" -msgstr "" +msgstr "串列的 *head*\\ (頭部)是其第一個元素: ::" #: ../../howto/mro.rst:154 msgid "head = C1" @@ -248,7 +297,7 @@ msgstr "head = C1" #: ../../howto/mro.rst:156 msgid "whereas the *tail* is the rest of the list::" -msgstr "" +msgstr "而 *tail*\\ (尾部)是串列的其餘部分: ::" #: ../../howto/mro.rst:158 msgid "tail = C2 ... CN." @@ -256,7 +305,7 @@ msgstr "tail = C2 ... CN." #: ../../howto/mro.rst:160 msgid "I shall also use the notation::" -msgstr "" +msgstr "我還將使用以下符號: ::" #: ../../howto/mro.rst:162 msgid "C + (C1 C2 ... CN) = C C1 C2 ... CN" @@ -264,11 +313,11 @@ msgstr "C + (C1 C2 ... CN) = C C1 C2 ... CN" #: ../../howto/mro.rst:164 msgid "to denote the sum of the lists [C] + [C1, C2, ... ,CN]." -msgstr "" +msgstr "標示串列的和 [C] + [C1, C2, ..., CN]。" #: ../../howto/mro.rst:166 msgid "Now I can explain how the MRO works in Python 2.3." -msgstr "" +msgstr "現在我就可以繼續解釋 MRO 在 Python 2.3 中的運作方式。" #: ../../howto/mro.rst:168 msgid "" @@ -276,16 +325,18 @@ msgid "" "from the base classes B1, B2, ... , BN. We want to compute the " "linearization L[C] of the class C. The rule is the following:" msgstr "" +"考慮多重繼承階層結構中的類別 C,C 從基底類別 B1、B2、...、BN 繼承。我們想計算" +"類別 C 的線性化 L[C]。規則如下:" #: ../../howto/mro.rst:173 msgid "" "*the linearization of C is the sum of C plus the merge of the linearizations " "of the parents and the list of the parents.*" -msgstr "" +msgstr "*C 的線性化是 C 加上父類別線性化的合併以及父類別串列的和。*" #: ../../howto/mro.rst:176 msgid "In symbolic notation::" -msgstr "" +msgstr "用符號標示: ::" #: ../../howto/mro.rst:178 msgid "L[C(B1 ... BN)] = C + merge(L[B1] ... L[BN], B1 ... BN)" @@ -296,6 +347,7 @@ msgid "" "In particular, if C is the ``object`` class, which has no parents, the " "linearization is trivial::" msgstr "" +"特別是如果 C 是沒有父類別的 ``object`` 類別,那麼線性化是簡單的:" #: ../../howto/mro.rst:183 msgid "L[object] = object." @@ -305,7 +357,7 @@ msgstr "L[object] = object." msgid "" "However, in general one has to compute the merge according to the following " "prescription:" -msgstr "" +msgstr "然而,一般來說必須根據以下規則來計算合併:" #: ../../howto/mro.rst:188 msgid "" @@ -317,6 +369,10 @@ msgid "" "this case, it is impossible to construct the merge, Python 2.3 will refuse " "to create the class C and will raise an exception.*" msgstr "" +"*取第一個串列的頭部,即 L[B1][0];如果此頭部不在其他任何串列的尾部中,那麼將" +"它加入到 C 的線性化中,並從合併中的所有串列移除它;否則查看下一個串列的頭部," +"如果它是一個好的頭部就取它。然後重複此操作,直到所有類別都被移除或無法找到好的" +"頭部。在後面這種情況下,無法建構合併,Python 2.3 將拒絕建立類別 C 並引發例外。*" #: ../../howto/mro.rst:197 msgid "" @@ -325,12 +381,16 @@ msgid "" "preserved (as in the example of serious order disagreement discussed above) " "then the merge cannot be computed." msgstr "" +"此規則確保合併操作\\ *保留*\\ 順序(如果順序可以被保留)。另一方面,如果無法保" +"留順序(如上面討論的嚴重順序分歧的範例),則無法計算合併。" #: ../../howto/mro.rst:202 msgid "" "The computation of the merge is trivial if C has only one parent (single " "inheritance); in this case::" msgstr "" +"如果 C 只有一個父類別(單一繼承),則合併的計算是微不足道的。在這種情況" +"下: ::" #: ../../howto/mro.rst:205 msgid "L[C(B)] = C + merge(L[B],B) = C + L[B]" @@ -341,6 +401,7 @@ msgid "" "However, in the case of multiple inheritance things are more cumbersome and " "I don't expect you can understand the rule without a couple of examples ;-)" msgstr "" +"但是,在多重繼承的情況下,事情更加複雜,我不指望你能在沒有幾個範例的情況下理解這個規則 ;-)" #: ../../howto/mro.rst:212 msgid "Examples" @@ -348,11 +409,11 @@ msgstr "範例" #: ../../howto/mro.rst:214 msgid "First example. Consider the following hierarchy:" -msgstr "" +msgstr "第一個例子,請參考以下階層結構:" #: ../../howto/mro.rst:224 msgid "In this case the inheritance graph can be drawn as:" -msgstr "" +msgstr "在這種情況下,繼承圖可以繪製為:" #: ../../howto/mro.rst:226 msgid "" @@ -378,10 +439,31 @@ msgid "" "Level 0 0 | A | (more specialized)\n" " ---" msgstr "" +" 6\n" +" ---\n" +"Level 3 | O | (更廣泛)\n" +" / --- \\\n" +" / | \\ |\n" +" / | \\ |\n" +" / | \\ |\n" +" --- --- --- |\n" +"Level 2 3 | D | 4| E | | F | 5 |\n" +" --- --- --- |\n" +" \\ \\ _ / | |\n" +" \\ / \\ _ | |\n" +" \\ / \\ | |\n" +" --- --- |\n" +"Level 1 1 | B | | C | 2 |\n" +" --- --- |\n" +" \\ / |\n" +" \\ / \\ /\n" +" ---\n" +"Level 0 0 | A | (更專精)\n" +" ---" #: ../../howto/mro.rst:251 msgid "The linearizations of O,D,E and F are trivial::" -msgstr "" +msgstr "O、D、E 和 F 的線性化很簡單: ::" #: ../../howto/mro.rst:253 msgid "" @@ -390,10 +472,14 @@ msgid "" "L[E] = E O\n" "L[F] = F O" msgstr "" +"L[O] = O\n" +"L[D] = D O\n" +"L[E] = E O\n" +"L[F] = F O" #: ../../howto/mro.rst:258 msgid "The linearization of B can be computed as::" -msgstr "" +msgstr "B 的線性化可以計算為: ::" #: ../../howto/mro.rst:260 msgid "L[B] = B + merge(DO, EO, DE)" @@ -407,6 +493,10 @@ msgid "" "the next sequence. Then we see that E is a good head; we take it and we are " "reduced to compute ``merge(O,O)`` which gives O. Therefore::" msgstr "" +"我們看到 D 是一個好的頭部,因此我們取它,並將其簡化為 ``merge(O,EO,E)``。現" +"在 O 不是一個好的頭部,因為它位於序列 EO 的尾部。在這種情況下,規則說我們必須" +"跳到下一個序列。然後我們看到 E 是一個好的頭部;我們取它,並簡化為計算 " +"``merge(O, O)`` 得出 O。因此: ::" #: ../../howto/mro.rst:268 msgid "L[B] = B D E O" @@ -414,7 +504,7 @@ msgstr "L[B] = B D E O" #: ../../howto/mro.rst:270 msgid "Using the same procedure one finds::" -msgstr "" +msgstr "使用相同的程序可以發現: ::" #: ../../howto/mro.rst:272 msgid "" @@ -430,7 +520,7 @@ msgstr "" #: ../../howto/mro.rst:277 msgid "Now we can compute::" -msgstr "" +msgstr "現在我們可以計算出: ::" #: ../../howto/mro.rst:279 msgid "" @@ -457,19 +547,23 @@ msgid "" "specialized classes) have higher precedence (see the inheritance graph). " "However, this is not the general case." msgstr "" +"在此範例中,線性化是根據繼承層級以相當不錯的方式排序的,因為較低層級(即更專" +"精的類別)具有更高的優先級(請參見繼承圖)。但是這不是一般情況。" #: ../../howto/mro.rst:292 msgid "" "I leave as an exercise for the reader to compute the linearization for my " "second example:" -msgstr "" +msgstr "第二個範例的線性化之計算我留給讀者當作練習:" #: ../../howto/mro.rst:303 msgid "" -"The only difference with the previous example is the change B(D,E) --> B(E," -"D); however even such a little modification completely changes the ordering " -"of the hierarchy:" +"The only difference with the previous example is the change B(D,E) --> " +"B(E,D); however even such a little modification completely changes the " +"ordering of the hierarchy:" msgstr "" +"與上一個範例的唯一區別是 B(D,E) --> B(E,D) 的變化;但是即使是這樣小小的修改,也完" +"全改變了階層結構的順序:" #: ../../howto/mro.rst:307 msgid "" @@ -523,6 +617,8 @@ msgid "" "precedes the class C, which is in the first level of the hierarchy, i.e. E " "is more specialized than C, even if it is in a higher level." msgstr "" +"請注意,階層結構的第二層中的類別 E,先於階層結構第一層的類別 C,即 E 比 C 更" +"專精,即使它處於更高層。" #: ../../howto/mro.rst:336 msgid "" @@ -530,6 +626,8 @@ msgid "" "case it coincides with the Python 2.3 linearization. It is enough to invoke " "the :meth:`~type.mro` method of class A:" msgstr "" +"懶惰的程式設計師可以直接從 Python 2.2 取得 MRO,因為在這種情況下,它與 " +"Python 2.3 線性化一致。呼叫類別 A 的 :meth:`~type.mro` 方法就足夠了:" #: ../../howto/mro.rst:345 msgid "" @@ -537,6 +635,8 @@ msgid "" "involving a serious order disagreement. In this case, it is straightforward " "to compute the linearizations of O, X, Y, A and B:" msgstr "" +"最後,讓我考慮第一部分中討論的範例,涉及嚴重的順序分歧。在這種情況下,計算 " +"O、X、Y、A 和 B 的線性化是很簡單的:" #: ../../howto/mro.rst:349 msgid "" @@ -556,7 +656,7 @@ msgstr "" msgid "" "However, it is impossible to compute the linearization for a class C that " "inherits from A and B::" -msgstr "" +msgstr "但是,我們不可能計算出從 A 和 B 繼承的類別 C 的線性化: ::" #: ../../howto/mro.rst:360 msgid "" @@ -575,10 +675,13 @@ msgid "" "and the C3 algorithm stops. Python 2.3 raises an error and refuses to " "create the class C." msgstr "" +"在這一點上,我們無法合併串列 XYO 和 YXO,因為 X 位於 YXO 的尾部,而 Y 則位於 " +"XYO 的尾部:因此,沒有好的頭部,C3 演算法停止。Python 2.3 會引發錯誤而拒絕建" +"立類別 C。" #: ../../howto/mro.rst:370 msgid "Bad Method Resolution Orders" -msgstr "" +msgstr "不良的方法解析順序" #: ../../howto/mro.rst:372 msgid "" @@ -587,16 +690,18 @@ msgid "" "both the MRO for classic classes and the MRO for new style classes in Python " "2.2 are bad." msgstr "" +"當 MRO 打破諸如區域優先順序和單調性之類的基本屬性時,MRO 是\\ *不良*\\ 的。在本節" +"中,我將證明經典類別的 MRO 和 Python 2.2 的新式類別的 MRO 都是不好的。" #: ../../howto/mro.rst:377 msgid "" "It is easier to start with the local precedence ordering. Consider the " "following example:" -msgstr "" +msgstr "從區域優先順序開始更容易。考慮以下範例:" #: ../../howto/mro.rst:384 msgid "with inheritance diagram" -msgstr "" +msgstr "包含繼承圖" #: ../../howto/mro.rst:386 msgid "" @@ -610,13 +715,25 @@ msgid "" "\n" " (buy eggs or spam ?)" msgstr "" +" O\n" +" |\n" +"(buy spam) F\n" +" | \\\n" +" | E (buy eggs)\n" +" | /\n" +" G\n" +"\n" +" (buy eggs or spam ?)" #: ../../howto/mro.rst:399 msgid "" "We see that class G inherits from F and E, with F *before* E: therefore we " -"would expect the attribute *G.remember2buy* to be inherited by *F." -"remember2buy* and not by *E.remember2buy*: nevertheless Python 2.2 gives" +"would expect the attribute *G.remember2buy* to be inherited by " +"*F.remember2buy* and not by *E.remember2buy*: nevertheless Python 2.2 gives" msgstr "" +"我們看到類別 G 從 F 和 E 繼承,F 在 E *之前*:因此,我們希望屬性 " +"*G.remember2buy* 被 *F.remember2buy* 繼承,而不是 *E.remember2buy*:儘管如" +"此,Python 2.2 給出" #: ../../howto/mro.rst:407 msgid "" @@ -624,10 +741,12 @@ msgid "" "precedence list, i.e. the list of the parents of G, is not preserved in the " "Python 2.2 linearization of G::" msgstr "" +"這是區域優先順序的破壞,因為區域優先串列中的順序,即 G 的父類別串列,在 " +"Python 2.2 的 G 的線性化中不被保留:" #: ../../howto/mro.rst:411 msgid "L[G,P22]= G E F object # F *follows* E" -msgstr "L[G,P22]= G E F object # F *follows* E" +msgstr "L[G,P22]= G E F object # F *跟隨* E" #: ../../howto/mro.rst:413 msgid "" @@ -637,11 +756,14 @@ msgid "" "quite non-intuitive and error prone. This is particularly true since it is " "a different from old style classes:" msgstr "" +"有人可能會說,F 在 Python 2.2 線性化中跟隨 E 的原因是 F 比 E 特化程度較低," +"因為 F 是 E 的超類別;然而,局部優先順序的破壞非常不直覺且容易出錯。尤其如" +"此,因為它與舊式類別不同:" #: ../../howto/mro.rst:425 msgid "" "In this case the MRO is GFEF and the local precedence ordering is preserved." -msgstr "" +msgstr "在這種情況下,MRO 是 GFEF 並且保留區域優先順序。" #: ../../howto/mro.rst:428 msgid "" @@ -651,6 +773,9 @@ msgid "" "effectively stopping the programmer from generating ambiguous hierarchies. " "The reason for that is that the C3 algorithm fails when the merge::" msgstr "" +"通常應避免諸如上一個等階層結構,因為尚不清楚 F 是否應覆蓋 E 或反過來。 " +"Python 2.3 透過在建立類別 G 時引發例外來解決歧義,從而有效地阻止程式設計師產" +"生模棱兩可的階層結構。原因是當這樣合併時 C3 演算法會失敗: ::" #: ../../howto/mro.rst:435 msgid "merge(FO,EFO,FE)" @@ -660,7 +785,7 @@ msgstr "merge(FO,EFO,FE)" msgid "" "cannot be computed, because F is in the tail of EFO and E is in the tail of " "FE." -msgstr "" +msgstr "無法計算,因為 F 在 EFO 的尾部,而 E 在 FE 的尾部。" #: ../../howto/mro.rst:440 msgid "" @@ -668,6 +793,8 @@ msgid "" "from E and F (the more specific first) and not from F and E; in this case " "the MRO is GEF without any doubt." msgstr "" +"真正的解決方案是設計一個非歧義的階層結構,即源自 E 和 F(更具體的第一)而不" +"是 F 和 E;在這種情況下,MRO 毫無疑問是 GEF。" #: ../../howto/mro.rst:444 msgid "" @@ -680,12 +807,20 @@ msgid "" " G\n" " (eggs, no doubt)" msgstr "" +" O\n" +" |\n" +" F (spam)\n" +" / |\n" +"(eggs) E |\n" +" \\ |\n" +" G\n" +" (eggs, no doubt)" #: ../../howto/mro.rst:456 msgid "" "Python 2.3 forces the programmer to write good hierarchies (or, at least, " "less error-prone ones)." -msgstr "" +msgstr "Python 2.3 迫使程式設計師要編寫良好(或至少較不易於出錯)的階層結構。" #: ../../howto/mro.rst:459 msgid "" @@ -693,29 +828,32 @@ msgid "" "enough to recognize obvious mistakes, as the duplication of classes in the " "list of parents:" msgstr "" +"與之相關的是,我指出 Python 2.3 演算法足夠聰明,可以識別出明顯的錯誤,例" +"如父類別串列中類別的重複:" #: ../../howto/mro.rst:469 msgid "" "Python 2.2 (both for classic classes and new style classes) in this " "situation, would not raise any exception." msgstr "" +"在這種情況下,Python 2.2(無論是經典類別還是新式類別)不會引發任何例外。" #: ../../howto/mro.rst:472 msgid "" "Finally, I would like to point out two lessons we have learned from this " "example:" -msgstr "" +msgstr "最後,我想指出我們從這個範例中學到的兩個教訓:" #: ../../howto/mro.rst:475 msgid "" "despite the name, the MRO determines the resolution order of attributes, not " "only of methods;" -msgstr "" +msgstr "儘管名稱如此,但不僅是方法的解析順序,MRO 也決定了屬性的解析順序;" #: ../../howto/mro.rst:478 msgid "" "the default food for Pythonistas is spam ! (but you already knew that ;-)" -msgstr "" +msgstr "Pythonistas 的預設食物是 spam!(但是你已經知道 ;-)" #: ../../howto/mro.rst:481 msgid "" @@ -723,12 +861,14 @@ msgid "" "the issue of monotonicity. My goal is to show that neither the MRO for " "classic classes nor that for Python 2.2 new style classes is monotonic." msgstr "" +"在討論了區域優先順序的問題之後,現在讓我考慮單調性問題。我的目標是表明經典類" +"別的 MRO 或 Python 2.2 新式類別都不是單調的。" #: ../../howto/mro.rst:486 msgid "" "To prove that the MRO for classic classes is non-monotonic is rather " "trivial, it is enough to look at the diamond diagram:" -msgstr "" +msgstr "為了證明經典類別的 MRO 是非單調的,查看鑽石圖就足夠了:" #: ../../howto/mro.rst:489 msgid "" @@ -750,19 +890,21 @@ msgstr "" #: ../../howto/mro.rst:500 msgid "One easily discerns the inconsistency::" -msgstr "" +msgstr "可以很容易地辨別出這種不一致:" #: ../../howto/mro.rst:502 msgid "" "L[B,P21] = B C # B precedes C : B's methods win\n" "L[D,P21] = D A C B C # B follows C : C's methods win!" msgstr "" +"L[B,P21] = B C # B 優先於 C:B 的方法勝出\n" +"L[D,P21] = D A C B C # B 跟隨 C:C 的方法勝出!" #: ../../howto/mro.rst:505 msgid "" "On the other hand, there are no problems with the Python 2.2 and 2.3 MROs, " "they give both::" -msgstr "" +msgstr "另一方面,Python 2.2 和 2.3 MRO 沒有問題,它們兩個都給出了: ::" #: ../../howto/mro.rst:508 msgid "L[D] = D A B C" @@ -775,6 +917,9 @@ msgid "" "all new style classes inherit from ``object``, therefore diamonds are " "unavoidable and inconsistencies shows up in every multiple inheritance graph." msgstr "" +"Guido 在他的文章 [#]_ 中指出,經典的 MRO 在實踐中還不錯,因為通常可以避開經典" +"類別的鑽石圖。但是所有新式類別都從 ``object`` 繼承,因此鑽石圖是不可避免的," +"並且在每個多重繼承圖中都出現了不一致之處。" #: ../../howto/mro.rst:516 msgid "" @@ -782,6 +927,8 @@ msgid "" "impossible. The following example, originally provided by Samuele Pedroni, " "shows that the MRO of Python 2.2 is non-monotonic:" msgstr "" +"Python 2.2 的 MRO 讓打破單調性變得困難,但並非不可能。以下最初由 Samuele " +"Pedroni 提供的範例展示 Python 2.2 的 MRO 是非單調的:" #: ../../howto/mro.rst:530 msgid "" @@ -789,6 +936,8 @@ msgid "" "verify these linearizations as an exercise and draw the inheritance " "diagram ;-) ::" msgstr "" +"以下是根據 C3 MRO 的線性化(讀者應練習驗證這些線性化並繪製繼承" +"圖 ;-) : ::" #: ../../howto/mro.rst:534 msgid "" @@ -817,6 +966,8 @@ msgid "" "Python 2.2 gives exactly the same linearizations for A, B, C, D, E, K1, K2 " "and K3, but a different linearization for Z::" msgstr "" +"Python 2.2 給出了 A、B、C、D、E、K1、K2 和 K3 完全相同的線性化,但是 Z 的線性" +"化卻不同: ::" #: ../../howto/mro.rst:547 msgid "L[Z,P22] = Z K1 K3 A K2 D B C E O" @@ -834,10 +985,16 @@ msgid "" "the linearization of Z K2 *follows* K3. These problems explain why the 2.2 " "rule has been dismissed in favor of the C3 rule." msgstr "" +"顯然,這種線性化是\\ *錯誤*\\ 的,因為 A 在 D 之前,而在 K3 的線性化中,A 在 " +"D *之後*。換句話說,在 K3 中由 D 衍生的方法會覆寫由 A 衍生的方法,但在 Z 中" +"(它仍然是 K3 的子類別),由 A 衍生的方法卻覆寫由 D 衍生的方法!這是對單調性" +"的違反。此外,Z 的 Python 2.2 線性化也與局部優先順序不一致,因為類別 Z 的局部" +"優先串列是 [K1, K2, K3](K2 先於 K3),但在 Z 的線性化中 K2 *跟隨* K3。這些問" +"題解釋了為什麼 2.2 規則被摒棄而採用 C3 規則。" #: ../../howto/mro.rst:561 msgid "The end" -msgstr "" +msgstr "結語" #: ../../howto/mro.rst:563 msgid "" @@ -852,6 +1009,12 @@ msgid "" "the last line to play with the various examples I have discussed in this " "paper.::" msgstr "" +"本節適用於那些不耐煩、所有先前部分都跳過並直接滑到最後的讀者,也適" +"用於懶得鍛鍊大腦的程式設計師。最後,這也是針對一些自負的程式" +"設計師,不然她/他也不會想閱讀有關多重繼承階層中 C3 方法解析順序的文章;-)" +"擁有這三種美德(注意是同時擁有,不是分開)就值得獲得獎品:獎品是一個簡短的 Python 2.2 腳" +"本,可以幫你計算 2.3 MRO 而不用傷腦筋。只需更改最後一行就可以試跑我在本" +"文中討論的各種範例。 ::" #: ../../howto/mro.rst:574 msgid "" @@ -938,30 +1101,115 @@ msgid "" "\n" "#" msgstr "" +"#\n" +"\n" +"\"\"\"Samuele Pedroni 撰寫的 C3 演算法(由我改善了可讀性)。\"\"\"\n" +"\n" +"class __metaclass__(type):\n" +" \"所有類別都會被神奇地修改以便美觀地列印\"\n" +" __repr__ = lambda cls: cls.__name__\n" +"\n" +"class ex_2:\n" +" \"嚴重的順序分歧\" # 來自 Guido\n" +" class O: pass\n" +" class X(O): pass\n" +" class Y(O): pass\n" +" class A(X,Y): pass\n" +" class B(Y,X): pass\n" +" try:\n" +" class Z(A,B): pass # 在 Python 2.2 中會建立 Z(A,B)\n" +" except TypeError:\n" +" pass # 在 Python 2.3 中無法建立 Z(A,B)\n" +"\n" +"class ex_5:\n" +" \"我的第一個範例\"\n" +" class O: pass\n" +" class F(O): pass\n" +" class E(O): pass\n" +" class D(O): pass\n" +" class C(D,F): pass\n" +" class B(D,E): pass\n" +" class A(B,C): pass\n" +"\n" +"class ex_6:\n" +" \"我的第二個範例\"\n" +" class O: pass\n" +" class F(O): pass\n" +" class E(O): pass\n" +" class D(O): pass\n" +" class C(D,F): pass\n" +" class B(E,D): pass\n" +" class A(B,C): pass\n" +"\n" +"class ex_9:\n" +" \"Python 2.2 MRO 與 C3 的差異\" # 來自 Samuele\n" +" class O: pass\n" +" class A(O): pass\n" +" class B(O): pass\n" +" class C(O): pass\n" +" class D(O): pass\n" +" class E(O): pass\n" +" class K1(A,B,C): pass\n" +" class K2(D,B,E): pass\n" +" class K3(D,A): pass\n" +" class Z(K1,K2,K3): pass\n" +"\n" +"def merge(seqs):\n" +" print '\\n\\nCPL[%s]=%s' % (seqs[0][0],seqs),\n" +" res = []; i=0\n" +" while 1:\n" +" nonemptyseqs=[seq for seq in seqs if seq]\n" +" if not nonemptyseqs: return res\n" +" i+=1; print '\\n',i,'round: candidates...',\n" +" for seq in nonemptyseqs: # 在序列頭部中尋找合併候選者\n" +" cand = seq[0]; print ' ',cand,\n" +" nothead=[s for s in nonemptyseqs if cand in s[1:]]\n" +" if nothead: cand=None # 拒絕候選者\n" +" else: break\n" +" if not cand: raise \"不一致的階層結構\"\n" +" res.append(cand)\n" +" for seq in nonemptyseqs: # 移除候選者\n" +" if seq[0] == cand: del seq[0]\n" +"\n" +"def mro(C):\n" +" \"根據 C3 計算類別優先串列 (mro)\"\n" +" return merge([[C]]+map(mro,C.__bases__)+[list(C.__bases__)])\n" +"\n" +"def print_mro(C):\n" +" print '\\nMRO[%s]=%s' % (C,mro(C))\n" +" print '\\nP22 MRO[%s]=%s' % (C,C.mro())\n" +"\n" +"print_mro(ex_9.Z)\n" +"\n" +"#" #: ../../howto/mro.rst:656 msgid "That's all folks," -msgstr "" +msgstr "就這樣,各位," #: ../../howto/mro.rst:658 msgid "enjoy !" -msgstr "" +msgstr "祝使用愉快!" #: ../../howto/mro.rst:662 msgid "Resources" -msgstr "" +msgstr "資源" #: ../../howto/mro.rst:664 msgid "" "The thread on python-dev started by Samuele Pedroni: https://mail.python.org/" "pipermail/python-dev/2002-October/029035.html" msgstr "" +"Samuele Pedroni 發起的 python-dev 主題討論:https://mail.python.org/" +"pipermail/python-dev/2002-October/029035.html" #: ../../howto/mro.rst:667 msgid "" -"The paper *A Monotonic Superclass Linearization for Dylan*: https://doi." -"org/10.1145/236337.236343" +"The paper *A Monotonic Superclass Linearization for Dylan*: https://doi.org/" +"10.1145/236337.236343" msgstr "" +"論文 *A Monotonic Superclass Linearization for Dylan*: https://doi.org/" +"10.1145/236337.236343" #: ../../howto/mro.rst:670 msgid "" @@ -969,3 +1217,6 @@ msgid "" "https://web.archive.org/web/20140210194412/http://www.python.org/download/" "releases/2.2.2/descrintro" msgstr "" +"Guido van Rossum 的文章 *Unifying types and classes in Python 2.2*:https://" +"web.archive.org/web/20140210194412/http://www.python.org/download/releases/" +"2.2.2/descrintro"