Skip to content

Commit a2d619b

Browse files
authored
Update Translation of howto/annotations (#699)
* Finish Translation * Update Info * Update with reviewer's comment
1 parent 3214269 commit a2d619b

File tree

1 file changed

+104
-13
lines changed

1 file changed

+104
-13
lines changed

howto/annotations.po

+104-13
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ msgstr ""
88
"Project-Id-Version: Python 3.12\n"
99
"Report-Msgid-Bugs-To: \n"
1010
"POT-Creation-Date: 2022-12-25 00:16+0000\n"
11-
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
12-
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
11+
"PO-Revision-Date: 2023-11-08 23:11+0800\n"
12+
"Last-Translator: rockleon <therockleona@gmail.com>\n"
1313
"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-"
1414
"tw)\n"
1515
"Language: zh_TW\n"
1616
"MIME-Version: 1.0\n"
1717
"Content-Type: text/plain; charset=UTF-8\n"
1818
"Content-Transfer-Encoding: 8bit\n"
19+
"X-Generator: Poedit 3.4.1\n"
1920

2021
#: ../../howto/annotations.rst:5
2122
msgid "Annotations Best Practices"
22-
msgstr ""
23+
msgstr "註釋 (annotation) 最佳實踐"
2324

2425
#: ../../howto/annotations.rst:0
2526
msgid "author"
@@ -40,6 +41,9 @@ msgid ""
4041
"``__annotations__`` on Python objects, we encourage you to follow the "
4142
"guidelines described below."
4243
msgstr ""
44+
"本文件旨在封裝 (encapsulate) 使用註釋字典 (annotations dicts) 的最佳實踐。如"
45+
"果你寫 Python 程式碼並在調查 Python 物件上的 ``__annotations__`` ,我們鼓勵你"
46+
"遵循下面描述的準則。"
4347

4448
#: ../../howto/annotations.rst:16
4549
msgid ""
@@ -49,6 +53,9 @@ msgid ""
4953
"and older, other best practices for ``__annotations__`` that apply to any "
5054
"Python version, and quirks of ``__annotations__``."
5155
msgstr ""
56+
"本文件分為四個部分:在 Python 3.10 及更高版本中存取物件註釋的最佳實踐、在 "
57+
"Python 3.9 及更早版本中存取物件註釋的最佳實踐、適用於任何Python 版本 "
58+
"``__annotations__`` 的最佳實踐,以及 ``__annotations__`` 的奇異之處。"
5259

5360
#: ../../howto/annotations.rst:26
5461
msgid ""
@@ -57,10 +64,13 @@ msgid ""
5764
"information on how to use \"type hints\" in your code, please see the :mod:"
5865
"`typing` module."
5966
msgstr ""
67+
"請注意,本文件是特別說明 ``__annotations__`` 的使用,而非\\ *如何使用*\\ 註"
68+
"釋。如果你正在尋找如何在你的程式碼中使用「型別提示 (type hint)」的資訊,請查"
69+
"閱模組 (module) :mod:`typing`。"
6070

6171
#: ../../howto/annotations.rst:33
6272
msgid "Accessing The Annotations Dict Of An Object In Python 3.10 And Newer"
63-
msgstr ""
73+
msgstr "在 Python 3.10 及更高版本中存取物件的註釋字典"
6474

6575
#: ../../howto/annotations.rst:35
6676
msgid ""
@@ -70,6 +80,9 @@ msgid ""
7080
"supports annotations. This function can also \"un-stringize\" stringized "
7181
"annotations for you."
7282
msgstr ""
83+
"Python 3.10 在標準函式庫中新增了一個新函式::func:`inspect.get_annotations`。"
84+
"在 Python 3.10 及更高版本中,呼叫此函式是存取任何支援註釋的物件的註釋字典的最"
85+
"佳實踐。此函式也可以為你「取消字串化 (un-stringize)」字串化註釋。"
7386

7487
#: ../../howto/annotations.rst:42
7588
msgid ""
@@ -81,6 +94,11 @@ msgid ""
8194
"of these three *specific* objects, you may simply use ``o.__annotations__`` "
8295
"to get at the object's annotations dict."
8396
msgstr ""
97+
"若由於某種原因 :func:`inspect.get_annotations` 對你的場合不可行,你可以手動存"
98+
"取 ``__annotations__`` 資料成員。 Python 3.10 中的最佳實踐也已經改變:從 "
99+
"Python 3.10 開始,保證 ``o.__annotations__`` \\ *始終*\\ 適用於 Python 函式、"
100+
"類別 (class) 和模組。如果你確定正在檢查的物件是這三個\\ *特定*\\ 物件之一,你"
101+
"可以簡單地使用 ``o.__annotations__`` 來取得物件的註釋字典。"
84102

85103
#: ../../howto/annotations.rst:52
86104
msgid ""
@@ -90,6 +108,11 @@ msgid ""
90108
"practice in Python versions 3.10 and newer is to call :func:`getattr` with "
91109
"three arguments, for example ``getattr(o, '__annotations__', None)``."
92110
msgstr ""
111+
"但是,其他型別的 callable(可呼叫物件)(例如,由 :func:`functools.partial` "
112+
"建立的 callable)可能沒有定義 ``__annotations__`` 屬性 (attribute)。當存取可"
113+
"能未知的物件的 ``__annotations__`` 時,Python 3.10 及更高版本中的最佳實踐是使"
114+
"用三個參數呼叫 :func:`getattr`,例如 ``getattr(o, '__annotations__', "
115+
"None)``。"
93116

94117
#: ../../howto/annotations.rst:60
95118
msgid ""
@@ -98,17 +121,22 @@ msgid ""
98121
"parent's ``__annotations__``. In Python 3.10 and newer, the child class's "
99122
"annotations will be an empty dict instead."
100123
msgstr ""
124+
"在 Python 3.10 之前,存取未定義註釋但具有註釋的父類別的類別上的 "
125+
"``__annotations__`` 將傳回父類別的 `` __annotations__``。在 Python 3.10 及更"
126+
"高版本中,子類別的註釋將會是一個空字典。"
101127

102128
#: ../../howto/annotations.rst:68
103129
msgid "Accessing The Annotations Dict Of An Object In Python 3.9 And Older"
104-
msgstr ""
130+
msgstr "在 Python 3.9 及更早版本中存取物件的註釋字典"
105131

106132
#: ../../howto/annotations.rst:70
107133
msgid ""
108134
"In Python 3.9 and older, accessing the annotations dict of an object is much "
109135
"more complicated than in newer versions. The problem is a design flaw in "
110136
"these older versions of Python, specifically to do with class annotations."
111137
msgstr ""
138+
"在 Python 3.9 及更早版本中,存取物件的註釋字典比新版本複雜得多。問題出在於這"
139+
"些舊版 Python 中有設計缺陷,特別是與類別註釋有關的設計缺陷。"
112140

113141
#: ../../howto/annotations.rst:75
114142
msgid ""
@@ -118,6 +146,9 @@ msgid ""
118146
"should use three-argument :func:`getattr` to access the object's "
119147
"``__annotations__`` attribute."
120148
msgstr ""
149+
"存取其他物件(如函式、其他 callable 和模組)的註釋字典的最佳實踐與 3.10 的最"
150+
"佳實踐相同,假設你沒有呼叫 :func:`inspect.get_annotations`:你應該使用三個:"
151+
"參數 :func:`getattr` 來存取物件的 ``__annotations__`` 屬性。"
121152

122153
#: ../../howto/annotations.rst:82
123154
msgid ""
@@ -127,10 +158,14 @@ msgid ""
127158
"``__annotations__`` attribute of a class may inadvertently return the "
128159
"annotations dict of a *base class.* As an example::"
129160
msgstr ""
161+
"不幸的是,這不是類別的最佳實踐。問題是,由於 ``__annotations__`` 在類別上是選"
162+
"填的 (optional),並且因為類別可以從其基底類別 (base class) 繼承屬性,所以存取"
163+
"類別的 ``__annotations__`` 屬性可能會無意中回傳\\ *基底類別的註釋字典。*\\ 舉"
164+
"例來說: ::"
130165

131166
#: ../../howto/annotations.rst:98
132167
msgid "This will print the annotations dict from ``Base``, not ``Derived``."
133-
msgstr ""
168+
msgstr "這將印出 (print) 來自 ``Base`` 的註釋字典,而不是 ``Derived``。"
134169

135170
#: ../../howto/annotations.rst:101
136171
msgid ""
@@ -141,31 +176,43 @@ msgid ""
141176
"dictionary. Since the class may or may not have annotations defined, best "
142177
"practice is to call the ``get`` method on the class dict."
143178
msgstr ""
179+
"如果你正在檢查的物件是一個類別 (``isinstance(o, type)``),你的程式碼將必須有"
180+
"一個單獨的程式碼路徑。在這種情況下,最佳實踐依賴 Python 3.9 及之前版本的實作"
181+
"細節 (implementation detail):如果一個類別定義了註釋,它們將儲存在該類別的 "
182+
"``__dict__`` 字典中。由於類別可能定義了註釋,也可能沒有定義,因此最佳實踐是在"
183+
"類別字典上呼叫 ``get`` 方法。"
144184

145185
#: ../../howto/annotations.rst:109
146186
msgid ""
147187
"To put it all together, here is some sample code that safely accesses the "
148188
"``__annotations__`` attribute on an arbitrary object in Python 3.9 and "
149189
"before::"
150190
msgstr ""
191+
"總而言之,以下是一些範例程式碼,可以安全地存取 Python 3.9 及先前版本中任意物"
192+
"件上的 ``__annotations__`` 屬性:"
151193

152194
#: ../../howto/annotations.rst:118
153195
msgid ""
154196
"After running this code, ``ann`` should be either a dictionary or ``None``. "
155197
"You're encouraged to double-check the type of ``ann`` using :func:"
156198
"`isinstance` before further examination."
157199
msgstr ""
200+
"運行此程式碼後,``ann`` 應該是字典或 ``None``。我們鼓勵你在進一步檢查之前使"
201+
"用 :func:`isinstance` 仔細檢查 ``ann`` 的型別。"
158202

159203
#: ../../howto/annotations.rst:123
160204
msgid ""
161205
"Note that some exotic or malformed type objects may not have a ``__dict__`` "
162206
"attribute, so for extra safety you may also wish to use :func:`getattr` to "
163207
"access ``__dict__``."
164208
msgstr ""
209+
"請注意,某些外來 (exotic) 或格式錯誤 (malform) 的型別物件可能沒有 "
210+
"``__dict__`` 屬性,因此為了額外的安全,你可能還希望使用 :func:`getattr` 來存"
211+
"取 ``__dict__`` 。"
165212

166213
#: ../../howto/annotations.rst:129
167214
msgid "Manually Un-Stringizing Stringized Annotations"
168-
msgstr ""
215+
msgstr "手動取消字串化註釋"
169216

170217
#: ../../howto/annotations.rst:131
171218
msgid ""
@@ -174,6 +221,8 @@ msgid ""
174221
"really is best to call :func:`inspect.get_annotations` to do this work for "
175222
"you."
176223
msgstr ""
224+
"在某些註釋可能被「字串化」的情況下,並且你希望評估這些字串以產生它們表示的 "
225+
"Python 值,最好呼叫 :func:`inspect.get_annotations` 來為你完成這項工作。"
177226

178227
#: ../../howto/annotations.rst:137
179228
msgid ""
@@ -182,25 +231,33 @@ msgid ""
182231
"encouraged to examine the implementation of :func:`inspect.get_annotations` "
183232
"in the current Python version and follow a similar approach."
184233
msgstr ""
234+
"如果你使用的是 Python 3.9 或更早版本,或者由於某種原因你無法使用 :func:"
235+
"`inspect.get_annotations`,則需要複製其邏輯。我們鼓勵你檢查目前 Python 版本"
236+
"中 :func:`inspect.get_annotations` 的實作並遵循類似的方法。"
185237

186238
#: ../../howto/annotations.rst:143
187239
msgid ""
188240
"In a nutshell, if you wish to evaluate a stringized annotation on an "
189241
"arbitrary object ``o``:"
190-
msgstr ""
242+
msgstr "簡而言之,如果你希望評估任意物件 ``o`` 上的字串化註釋: ::"
191243

192244
#: ../../howto/annotations.rst:146
193245
msgid ""
194246
"If ``o`` is a module, use ``o.__dict__`` as the ``globals`` when calling :"
195247
"func:`eval`."
196248
msgstr ""
249+
"如果 ``o`` 是一個模組,則在呼叫 :func:`eval` 時使用 ``o.__dict__`` 作為\\ ``"
250+
"全域變數``\\ 。"
197251

198252
#: ../../howto/annotations.rst:148
199253
msgid ""
200254
"If ``o`` is a class, use ``sys.modules[o.__module__].__dict__`` as the "
201255
"``globals``, and ``dict(vars(o))`` as the ``locals``, when calling :func:"
202256
"`eval`."
203257
msgstr ""
258+
"如果 ``o`` 是一個類別,當呼叫 :func:`eval` 時,則使用 ``sys.modules[o."
259+
"__module__].__dict__`` 作為\\ ``全域變數``\\ ,使用 ``dict(vars(o))`` 作為\\ "
260+
"``區域變數``\\ 。"
204261

205262
#: ../../howto/annotations.rst:151
206263
msgid ""
@@ -209,12 +266,17 @@ msgid ""
209266
"accessing either ``o.__wrapped__`` or ``o.func`` as appropriate, until you "
210267
"have found the root unwrapped function."
211268
msgstr ""
269+
"如果 ``o`` 是使用 :func:`functools.update_wrapper`、:func:`functools.wraps` "
270+
"或 :func:`functools.partial` 包裝的 callable ,請依據需求,透過存取 ``o."
271+
"__wrapped__`` 或``o.func`` 來疊代解開它,直到找到根解包函式。"
212272

213273
#: ../../howto/annotations.rst:155
214274
msgid ""
215275
"If ``o`` is a callable (but not a class), use ``o.__globals__`` as the "
216276
"globals when calling :func:`eval`."
217277
msgstr ""
278+
"如果 ``o`` 是 callable(但不是類別),則在呼叫 :func:`eval` 時使用 ``o."
279+
"__globals__`` 作為全域變數。"
218280

219281
#: ../../howto/annotations.rst:158
220282
msgid ""
@@ -224,18 +286,23 @@ msgid ""
224286
"hints that require annotating with string values that specifically *can't* "
225287
"be evaluated. For example:"
226288
msgstr ""
289+
"然而,並非所有用作註釋的字串值都可以透過 :func:`eval` 成功轉換為 Python 值。"
290+
"理論上,字串值可以包含任何有效的字串,並且在實踐中,型別提示存在有效的用例,"
291+
"需要使用特定「無法」評估的字串值進行註釋。例如: ::"
227292

228293
#: ../../howto/annotations.rst:165
229294
msgid ""
230295
":pep:`604` union types using ``|``, before support for this was added to "
231296
"Python 3.10."
232297
msgstr ""
298+
"在 Python 3.10 支援 :pep:`604` 聯合型別 (union type) ``|`` 之前使用它。"
233299

234300
#: ../../howto/annotations.rst:167
235301
msgid ""
236302
"Definitions that aren't needed at runtime, only imported when :const:`typing."
237303
"TYPE_CHECKING` is true."
238304
msgstr ""
305+
"Runtime 中不需要的定義,僅在 :const:`typing.TYPE_CHECKING` 為 true 時匯入。"
239306

240307
#: ../../howto/annotations.rst:170
241308
msgid ""
@@ -244,42 +311,50 @@ msgid ""
244311
"it's recommended to only attempt to evaluate string values when explicitly "
245312
"requested to by the caller."
246313
msgstr ""
314+
"如果 :func:`eval` 嘗試計算這類型的值,它將失敗並引發例外。因此,在設計使用註"
315+
"釋的函式庫 API 時,建議僅在呼叫者 (caller) 明確請求時嘗試評估字串值。"
247316

248317
#: ../../howto/annotations.rst:178
249318
msgid "Best Practices For ``__annotations__`` In Any Python Version"
250-
msgstr ""
319+
msgstr "任何 Python 版本中 ``__annotations__`` 的最佳實踐"
251320

252321
#: ../../howto/annotations.rst:180
253322
msgid ""
254323
"You should avoid assigning to the ``__annotations__`` member of objects "
255324
"directly. Let Python manage setting ``__annotations__``."
256325
msgstr ""
326+
"你應該避免直接指派給物件的 ``__annotations__`` 成員。讓 Python 管理設定 "
327+
"``__annotations__``。"
257328

258329
#: ../../howto/annotations.rst:183
259330
msgid ""
260331
"If you do assign directly to the ``__annotations__`` member of an object, "
261332
"you should always set it to a ``dict`` object."
262333
msgstr ""
334+
"如果你直接指派給物件的 `` __annotations__`` 成員,則應始終將其設為 ``dict`` "
335+
"物件。"
263336

264337
#: ../../howto/annotations.rst:186
265338
msgid ""
266339
"If you directly access the ``__annotations__`` member of an object, you "
267340
"should ensure that it's a dictionary before attempting to examine its "
268341
"contents."
269342
msgstr ""
343+
"如果直接存取物件的 ``__annotations__`` 成員,則應在嘗試檢查其內容之前確保它是"
344+
"字典。"
270345

271346
#: ../../howto/annotations.rst:190
272347
msgid "You should avoid modifying ``__annotations__`` dicts."
273-
msgstr ""
348+
msgstr "你應該避免修改 ``__annotations__`` 字典。"
274349

275350
#: ../../howto/annotations.rst:192
276351
msgid ""
277352
"You should avoid deleting the ``__annotations__`` attribute of an object."
278-
msgstr ""
353+
msgstr "你應該避免刪除物件的 ``__annotations__`` 屬性。"
279354

280355
#: ../../howto/annotations.rst:197
281356
msgid "``__annotations__`` Quirks"
282-
msgstr ""
357+
msgstr "``__annotations__`` 奇異之處"
283358

284359
#: ../../howto/annotations.rst:199
285360
msgid ""
@@ -292,12 +367,18 @@ msgid ""
292367
"an ``AttributeError``; using ``del fn.__annotations__`` twice in a row is "
293368
"guaranteed to always throw an ``AttributeError``."
294369
msgstr ""
370+
"在 Python 3 的所有版本中,如果沒有在該物件上定義註釋,則函式物件會延遲建立 "
371+
"(lazy-create) 註釋字典。你可以使用 ``del fn.__annotations__`` 刪除 "
372+
"``__annotations__`` 屬性,但如果你隨後存取 ``fn.__annotations__``,該物件將建"
373+
"立一個新的空字典,它將作為註釋儲存並傳回。在函式延遲建立註釋字典之前刪除函式"
374+
"上的註釋將拋出 ``AttributeError``;連續兩次使用 ``del fn.__annotations__`` 保"
375+
"證總是拋出 ``AttributeError`` 。"
295376

296377
#: ../../howto/annotations.rst:209
297378
msgid ""
298379
"Everything in the above paragraph also applies to class and module objects "
299380
"in Python 3.10 and newer."
300-
msgstr ""
381+
msgstr "上一段的所有內容也適用於 Python 3.10 及更高版本中的類別和模組物件。"
301382

302383
#: ../../howto/annotations.rst:212
303384
msgid ""
@@ -309,6 +390,11 @@ msgid ""
309390
"``__annotations__`` to any Python value, and will retain whatever value is "
310391
"set."
311392
msgstr ""
393+
"在 Python 3 的所有版本中,你可以將函式物件上的 ``__annotations__`` 設定為 "
394+
"``None``。但是,隨後使用 ``fn.__annotations__`` 存取該物件上的註釋將根據本節"
395+
"第一段的內容延遲建立一個空字典。對於任何 Python 版本中的模組和類別來說,情況"
396+
"\\ *並非如此*\\;這些物件允許將 ``__annotations__`` 設定為任何 Python 值,並"
397+
"且將保留設定的任何值。"
312398

313399
#: ../../howto/annotations.rst:220
314400
msgid ""
@@ -317,9 +403,14 @@ msgid ""
317403
"will itself be quoted. In effect the annotation is quoted *twice.* For "
318404
"example::"
319405
msgstr ""
406+
"如果 Python 為你字串化你的註釋(使用 ``from __future__ import "
407+
"annotations``),並且你指定一個字串作為註釋,則該字串本身將被引用。實際上,註"
408+
"釋被引用了\\ *兩次。*\\ 例如: ::"
320409

321410
#: ../../howto/annotations.rst:231
322411
msgid ""
323412
"This prints ``{'a': \"'str'\"}``. This shouldn't really be considered a "
324413
"\"quirk\"; it's mentioned here simply because it might be surprising."
325414
msgstr ""
415+
"這會印出 ``{'a': \"'str'\"}``。這不應該被認為是一個「奇異的事」,他在這裡被簡單"
416+
"提及,因為他可能會讓人意想不到。"

0 commit comments

Comments
 (0)