@@ -8,18 +8,19 @@ msgstr ""
8
8
"Project-Id-Version : Python 3.12\n "
9
9
"Report-Msgid-Bugs-To : \n "
10
10
"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 "
13
13
"Language-Team : Chinese - TAIWAN (https://github.com/python/python-docs-zh- "
14
14
"tw)\n "
15
15
"Language : zh_TW\n "
16
16
"MIME-Version : 1.0\n "
17
17
"Content-Type : text/plain; charset=UTF-8\n "
18
18
"Content-Transfer-Encoding : 8bit\n "
19
+ "X-Generator : Poedit 3.4.1\n "
19
20
20
21
#: ../../howto/annotations.rst:5
21
22
msgid "Annotations Best Practices"
22
- msgstr ""
23
+ msgstr "註釋 (annotation) 最佳實踐 "
23
24
24
25
#: ../../howto/annotations.rst:0
25
26
msgid "author"
@@ -40,6 +41,9 @@ msgid ""
40
41
"``__annotations__`` on Python objects, we encourage you to follow the "
41
42
"guidelines described below."
42
43
msgstr ""
44
+ "本文件旨在封裝 (encapsulate) 使用註釋字典 (annotations dicts) 的最佳實踐。如"
45
+ "果你寫 Python 程式碼並在調查 Python 物件上的 ``__annotations__`` ,我們鼓勵你"
46
+ "遵循下面描述的準則。"
43
47
44
48
#: ../../howto/annotations.rst:16
45
49
msgid ""
@@ -49,6 +53,9 @@ msgid ""
49
53
"and older, other best practices for ``__annotations__`` that apply to any "
50
54
"Python version, and quirks of ``__annotations__``."
51
55
msgstr ""
56
+ "本文件分為四個部分:在 Python 3.10 及更高版本中存取物件註釋的最佳實踐、在 "
57
+ "Python 3.9 及更早版本中存取物件註釋的最佳實踐、適用於任何Python 版本 "
58
+ "``__annotations__`` 的最佳實踐,以及 ``__annotations__`` 的奇異之處。"
52
59
53
60
#: ../../howto/annotations.rst:26
54
61
msgid ""
@@ -57,10 +64,13 @@ msgid ""
57
64
"information on how to use \" type hints\" in your code, please see the :mod:"
58
65
"`typing` module."
59
66
msgstr ""
67
+ "請注意,本文件是特別說明 ``__annotations__`` 的使用,而非\\ *如何使用*\\ 註"
68
+ "釋。如果你正在尋找如何在你的程式碼中使用「型別提示 (type hint)」的資訊,請查"
69
+ "閱模組 (module) :mod:`typing`。"
60
70
61
71
#: ../../howto/annotations.rst:33
62
72
msgid "Accessing The Annotations Dict Of An Object In Python 3.10 And Newer"
63
- msgstr ""
73
+ msgstr "在 Python 3.10 及更高版本中存取物件的註釋字典 "
64
74
65
75
#: ../../howto/annotations.rst:35
66
76
msgid ""
@@ -70,6 +80,9 @@ msgid ""
70
80
"supports annotations. This function can also \" un-stringize\" stringized "
71
81
"annotations for you."
72
82
msgstr ""
83
+ "Python 3.10 在標準函式庫中新增了一個新函式::func:`inspect.get_annotations`。"
84
+ "在 Python 3.10 及更高版本中,呼叫此函式是存取任何支援註釋的物件的註釋字典的最"
85
+ "佳實踐。此函式也可以為你「取消字串化 (un-stringize)」字串化註釋。"
73
86
74
87
#: ../../howto/annotations.rst:42
75
88
msgid ""
@@ -81,6 +94,11 @@ msgid ""
81
94
"of these three *specific* objects, you may simply use ``o.__annotations__`` "
82
95
"to get at the object's annotations dict."
83
96
msgstr ""
97
+ "若由於某種原因 :func:`inspect.get_annotations` 對你的場合不可行,你可以手動存"
98
+ "取 ``__annotations__`` 資料成員。 Python 3.10 中的最佳實踐也已經改變:從 "
99
+ "Python 3.10 開始,保證 ``o.__annotations__`` \\ *始終*\\ 適用於 Python 函式、"
100
+ "類別 (class) 和模組。如果你確定正在檢查的物件是這三個\\ *特定*\\ 物件之一,你"
101
+ "可以簡單地使用 ``o.__annotations__`` 來取得物件的註釋字典。"
84
102
85
103
#: ../../howto/annotations.rst:52
86
104
msgid ""
@@ -90,6 +108,11 @@ msgid ""
90
108
"practice in Python versions 3.10 and newer is to call :func:`getattr` with "
91
109
"three arguments, for example ``getattr(o, '__annotations__', None)``."
92
110
msgstr ""
111
+ "但是,其他型別的 callable(可呼叫物件)(例如,由 :func:`functools.partial` "
112
+ "建立的 callable)可能沒有定義 ``__annotations__`` 屬性 (attribute)。當存取可"
113
+ "能未知的物件的 ``__annotations__`` 時,Python 3.10 及更高版本中的最佳實踐是使"
114
+ "用三個參數呼叫 :func:`getattr`,例如 ``getattr(o, '__annotations__', "
115
+ "None)``。"
93
116
94
117
#: ../../howto/annotations.rst:60
95
118
msgid ""
@@ -98,17 +121,22 @@ msgid ""
98
121
"parent's ``__annotations__``. In Python 3.10 and newer, the child class's "
99
122
"annotations will be an empty dict instead."
100
123
msgstr ""
124
+ "在 Python 3.10 之前,存取未定義註釋但具有註釋的父類別的類別上的 "
125
+ "``__annotations__`` 將傳回父類別的 `` __annotations__``。在 Python 3.10 及更"
126
+ "高版本中,子類別的註釋將會是一個空字典。"
101
127
102
128
#: ../../howto/annotations.rst:68
103
129
msgid "Accessing The Annotations Dict Of An Object In Python 3.9 And Older"
104
- msgstr ""
130
+ msgstr "在 Python 3.9 及更早版本中存取物件的註釋字典 "
105
131
106
132
#: ../../howto/annotations.rst:70
107
133
msgid ""
108
134
"In Python 3.9 and older, accessing the annotations dict of an object is much "
109
135
"more complicated than in newer versions. The problem is a design flaw in "
110
136
"these older versions of Python, specifically to do with class annotations."
111
137
msgstr ""
138
+ "在 Python 3.9 及更早版本中,存取物件的註釋字典比新版本複雜得多。問題出在於這"
139
+ "些舊版 Python 中有設計缺陷,特別是與類別註釋有關的設計缺陷。"
112
140
113
141
#: ../../howto/annotations.rst:75
114
142
msgid ""
@@ -118,6 +146,9 @@ msgid ""
118
146
"should use three-argument :func:`getattr` to access the object's "
119
147
"``__annotations__`` attribute."
120
148
msgstr ""
149
+ "存取其他物件(如函式、其他 callable 和模組)的註釋字典的最佳實踐與 3.10 的最"
150
+ "佳實踐相同,假設你沒有呼叫 :func:`inspect.get_annotations`:你應該使用三個:"
151
+ "參數 :func:`getattr` 來存取物件的 ``__annotations__`` 屬性。"
121
152
122
153
#: ../../howto/annotations.rst:82
123
154
msgid ""
@@ -127,10 +158,14 @@ msgid ""
127
158
"``__annotations__`` attribute of a class may inadvertently return the "
128
159
"annotations dict of a *base class.* As an example::"
129
160
msgstr ""
161
+ "不幸的是,這不是類別的最佳實踐。問題是,由於 ``__annotations__`` 在類別上是選"
162
+ "填的 (optional),並且因為類別可以從其基底類別 (base class) 繼承屬性,所以存取"
163
+ "類別的 ``__annotations__`` 屬性可能會無意中回傳\\ *基底類別的註釋字典。*\\ 舉"
164
+ "例來說: ::"
130
165
131
166
#: ../../howto/annotations.rst:98
132
167
msgid "This will print the annotations dict from ``Base``, not ``Derived``."
133
- msgstr ""
168
+ msgstr "這將印出 (print) 來自 ``Base`` 的註釋字典,而不是 ``Derived``。 "
134
169
135
170
#: ../../howto/annotations.rst:101
136
171
msgid ""
@@ -141,31 +176,43 @@ msgid ""
141
176
"dictionary. Since the class may or may not have annotations defined, best "
142
177
"practice is to call the ``get`` method on the class dict."
143
178
msgstr ""
179
+ "如果你正在檢查的物件是一個類別 (``isinstance(o, type)``),你的程式碼將必須有"
180
+ "一個單獨的程式碼路徑。在這種情況下,最佳實踐依賴 Python 3.9 及之前版本的實作"
181
+ "細節 (implementation detail):如果一個類別定義了註釋,它們將儲存在該類別的 "
182
+ "``__dict__`` 字典中。由於類別可能定義了註釋,也可能沒有定義,因此最佳實踐是在"
183
+ "類別字典上呼叫 ``get`` 方法。"
144
184
145
185
#: ../../howto/annotations.rst:109
146
186
msgid ""
147
187
"To put it all together, here is some sample code that safely accesses the "
148
188
"``__annotations__`` attribute on an arbitrary object in Python 3.9 and "
149
189
"before::"
150
190
msgstr ""
191
+ "總而言之,以下是一些範例程式碼,可以安全地存取 Python 3.9 及先前版本中任意物"
192
+ "件上的 ``__annotations__`` 屬性:"
151
193
152
194
#: ../../howto/annotations.rst:118
153
195
msgid ""
154
196
"After running this code, ``ann`` should be either a dictionary or ``None``. "
155
197
"You're encouraged to double-check the type of ``ann`` using :func:"
156
198
"`isinstance` before further examination."
157
199
msgstr ""
200
+ "運行此程式碼後,``ann`` 應該是字典或 ``None``。我們鼓勵你在進一步檢查之前使"
201
+ "用 :func:`isinstance` 仔細檢查 ``ann`` 的型別。"
158
202
159
203
#: ../../howto/annotations.rst:123
160
204
msgid ""
161
205
"Note that some exotic or malformed type objects may not have a ``__dict__`` "
162
206
"attribute, so for extra safety you may also wish to use :func:`getattr` to "
163
207
"access ``__dict__``."
164
208
msgstr ""
209
+ "請注意,某些外來 (exotic) 或格式錯誤 (malform) 的型別物件可能沒有 "
210
+ "``__dict__`` 屬性,因此為了額外的安全,你可能還希望使用 :func:`getattr` 來存"
211
+ "取 ``__dict__`` 。"
165
212
166
213
#: ../../howto/annotations.rst:129
167
214
msgid "Manually Un-Stringizing Stringized Annotations"
168
- msgstr ""
215
+ msgstr "手動取消字串化註釋 "
169
216
170
217
#: ../../howto/annotations.rst:131
171
218
msgid ""
@@ -174,6 +221,8 @@ msgid ""
174
221
"really is best to call :func:`inspect.get_annotations` to do this work for "
175
222
"you."
176
223
msgstr ""
224
+ "在某些註釋可能被「字串化」的情況下,並且你希望評估這些字串以產生它們表示的 "
225
+ "Python 值,最好呼叫 :func:`inspect.get_annotations` 來為你完成這項工作。"
177
226
178
227
#: ../../howto/annotations.rst:137
179
228
msgid ""
@@ -182,25 +231,33 @@ msgid ""
182
231
"encouraged to examine the implementation of :func:`inspect.get_annotations` "
183
232
"in the current Python version and follow a similar approach."
184
233
msgstr ""
234
+ "如果你使用的是 Python 3.9 或更早版本,或者由於某種原因你無法使用 :func:"
235
+ "`inspect.get_annotations`,則需要複製其邏輯。我們鼓勵你檢查目前 Python 版本"
236
+ "中 :func:`inspect.get_annotations` 的實作並遵循類似的方法。"
185
237
186
238
#: ../../howto/annotations.rst:143
187
239
msgid ""
188
240
"In a nutshell, if you wish to evaluate a stringized annotation on an "
189
241
"arbitrary object ``o``:"
190
- msgstr ""
242
+ msgstr "簡而言之,如果你希望評估任意物件 ``o`` 上的字串化註釋: :: "
191
243
192
244
#: ../../howto/annotations.rst:146
193
245
msgid ""
194
246
"If ``o`` is a module, use ``o.__dict__`` as the ``globals`` when calling :"
195
247
"func:`eval`."
196
248
msgstr ""
249
+ "如果 ``o`` 是一個模組,則在呼叫 :func:`eval` 時使用 ``o.__dict__`` 作為\\ ``"
250
+ "全域變數``\\ 。"
197
251
198
252
#: ../../howto/annotations.rst:148
199
253
msgid ""
200
254
"If ``o`` is a class, use ``sys.modules[o.__module__].__dict__`` as the "
201
255
"``globals``, and ``dict(vars(o))`` as the ``locals``, when calling :func:"
202
256
"`eval`."
203
257
msgstr ""
258
+ "如果 ``o`` 是一個類別,當呼叫 :func:`eval` 時,則使用 ``sys.modules[o."
259
+ "__module__].__dict__`` 作為\\ ``全域變數``\\ ,使用 ``dict(vars(o))`` 作為\\ "
260
+ "``區域變數``\\ 。"
204
261
205
262
#: ../../howto/annotations.rst:151
206
263
msgid ""
@@ -209,12 +266,17 @@ msgid ""
209
266
"accessing either ``o.__wrapped__`` or ``o.func`` as appropriate, until you "
210
267
"have found the root unwrapped function."
211
268
msgstr ""
269
+ "如果 ``o`` 是使用 :func:`functools.update_wrapper`、:func:`functools.wraps` "
270
+ "或 :func:`functools.partial` 包裝的 callable ,請依據需求,透過存取 ``o."
271
+ "__wrapped__`` 或``o.func`` 來疊代解開它,直到找到根解包函式。"
212
272
213
273
#: ../../howto/annotations.rst:155
214
274
msgid ""
215
275
"If ``o`` is a callable (but not a class), use ``o.__globals__`` as the "
216
276
"globals when calling :func:`eval`."
217
277
msgstr ""
278
+ "如果 ``o`` 是 callable(但不是類別),則在呼叫 :func:`eval` 時使用 ``o."
279
+ "__globals__`` 作為全域變數。"
218
280
219
281
#: ../../howto/annotations.rst:158
220
282
msgid ""
@@ -224,18 +286,23 @@ msgid ""
224
286
"hints that require annotating with string values that specifically *can't* "
225
287
"be evaluated. For example:"
226
288
msgstr ""
289
+ "然而,並非所有用作註釋的字串值都可以透過 :func:`eval` 成功轉換為 Python 值。"
290
+ "理論上,字串值可以包含任何有效的字串,並且在實踐中,型別提示存在有效的用例,"
291
+ "需要使用特定「無法」評估的字串值進行註釋。例如: ::"
227
292
228
293
#: ../../howto/annotations.rst:165
229
294
msgid ""
230
295
":pep:`604` union types using ``|``, before support for this was added to "
231
296
"Python 3.10."
232
297
msgstr ""
298
+ "在 Python 3.10 支援 :pep:`604` 聯合型別 (union type) ``|`` 之前使用它。"
233
299
234
300
#: ../../howto/annotations.rst:167
235
301
msgid ""
236
302
"Definitions that aren't needed at runtime, only imported when :const:`typing."
237
303
"TYPE_CHECKING` is true."
238
304
msgstr ""
305
+ "Runtime 中不需要的定義,僅在 :const:`typing.TYPE_CHECKING` 為 true 時匯入。"
239
306
240
307
#: ../../howto/annotations.rst:170
241
308
msgid ""
@@ -244,42 +311,50 @@ msgid ""
244
311
"it's recommended to only attempt to evaluate string values when explicitly "
245
312
"requested to by the caller."
246
313
msgstr ""
314
+ "如果 :func:`eval` 嘗試計算這類型的值,它將失敗並引發例外。因此,在設計使用註"
315
+ "釋的函式庫 API 時,建議僅在呼叫者 (caller) 明確請求時嘗試評估字串值。"
247
316
248
317
#: ../../howto/annotations.rst:178
249
318
msgid "Best Practices For ``__annotations__`` In Any Python Version"
250
- msgstr ""
319
+ msgstr "任何 Python 版本中 ``__annotations__`` 的最佳實踐 "
251
320
252
321
#: ../../howto/annotations.rst:180
253
322
msgid ""
254
323
"You should avoid assigning to the ``__annotations__`` member of objects "
255
324
"directly. Let Python manage setting ``__annotations__``."
256
325
msgstr ""
326
+ "你應該避免直接指派給物件的 ``__annotations__`` 成員。讓 Python 管理設定 "
327
+ "``__annotations__``。"
257
328
258
329
#: ../../howto/annotations.rst:183
259
330
msgid ""
260
331
"If you do assign directly to the ``__annotations__`` member of an object, "
261
332
"you should always set it to a ``dict`` object."
262
333
msgstr ""
334
+ "如果你直接指派給物件的 `` __annotations__`` 成員,則應始終將其設為 ``dict`` "
335
+ "物件。"
263
336
264
337
#: ../../howto/annotations.rst:186
265
338
msgid ""
266
339
"If you directly access the ``__annotations__`` member of an object, you "
267
340
"should ensure that it's a dictionary before attempting to examine its "
268
341
"contents."
269
342
msgstr ""
343
+ "如果直接存取物件的 ``__annotations__`` 成員,則應在嘗試檢查其內容之前確保它是"
344
+ "字典。"
270
345
271
346
#: ../../howto/annotations.rst:190
272
347
msgid "You should avoid modifying ``__annotations__`` dicts."
273
- msgstr ""
348
+ msgstr "你應該避免修改 ``__annotations__`` 字典。 "
274
349
275
350
#: ../../howto/annotations.rst:192
276
351
msgid ""
277
352
"You should avoid deleting the ``__annotations__`` attribute of an object."
278
- msgstr ""
353
+ msgstr "你應該避免刪除物件的 ``__annotations__`` 屬性。 "
279
354
280
355
#: ../../howto/annotations.rst:197
281
356
msgid "``__annotations__`` Quirks"
282
- msgstr ""
357
+ msgstr "``__annotations__`` 奇異之處 "
283
358
284
359
#: ../../howto/annotations.rst:199
285
360
msgid ""
@@ -292,12 +367,18 @@ msgid ""
292
367
"an ``AttributeError``; using ``del fn.__annotations__`` twice in a row is "
293
368
"guaranteed to always throw an ``AttributeError``."
294
369
msgstr ""
370
+ "在 Python 3 的所有版本中,如果沒有在該物件上定義註釋,則函式物件會延遲建立 "
371
+ "(lazy-create) 註釋字典。你可以使用 ``del fn.__annotations__`` 刪除 "
372
+ "``__annotations__`` 屬性,但如果你隨後存取 ``fn.__annotations__``,該物件將建"
373
+ "立一個新的空字典,它將作為註釋儲存並傳回。在函式延遲建立註釋字典之前刪除函式"
374
+ "上的註釋將拋出 ``AttributeError``;連續兩次使用 ``del fn.__annotations__`` 保"
375
+ "證總是拋出 ``AttributeError`` 。"
295
376
296
377
#: ../../howto/annotations.rst:209
297
378
msgid ""
298
379
"Everything in the above paragraph also applies to class and module objects "
299
380
"in Python 3.10 and newer."
300
- msgstr ""
381
+ msgstr "上一段的所有內容也適用於 Python 3.10 及更高版本中的類別和模組物件。 "
301
382
302
383
#: ../../howto/annotations.rst:212
303
384
msgid ""
@@ -309,6 +390,11 @@ msgid ""
309
390
"``__annotations__`` to any Python value, and will retain whatever value is "
310
391
"set."
311
392
msgstr ""
393
+ "在 Python 3 的所有版本中,你可以將函式物件上的 ``__annotations__`` 設定為 "
394
+ "``None``。但是,隨後使用 ``fn.__annotations__`` 存取該物件上的註釋將根據本節"
395
+ "第一段的內容延遲建立一個空字典。對於任何 Python 版本中的模組和類別來說,情況"
396
+ "\\ *並非如此*\\ ;這些物件允許將 ``__annotations__`` 設定為任何 Python 值,並"
397
+ "且將保留設定的任何值。"
312
398
313
399
#: ../../howto/annotations.rst:220
314
400
msgid ""
@@ -317,9 +403,14 @@ msgid ""
317
403
"will itself be quoted. In effect the annotation is quoted *twice.* For "
318
404
"example::"
319
405
msgstr ""
406
+ "如果 Python 為你字串化你的註釋(使用 ``from __future__ import "
407
+ "annotations``),並且你指定一個字串作為註釋,則該字串本身將被引用。實際上,註"
408
+ "釋被引用了\\ *兩次。*\\ 例如: ::"
320
409
321
410
#: ../../howto/annotations.rst:231
322
411
msgid ""
323
412
"This prints ``{'a': \" 'str'\" }``. This shouldn't really be considered a "
324
413
"\" quirk\" ; it's mentioned here simply because it might be surprising."
325
414
msgstr ""
415
+ "這會印出 ``{'a': \" 'str'\" }``。這不應該被認為是一個「奇異的事」,他在這裡被簡單"
416
+ "提及,因為他可能會讓人意想不到。"
0 commit comments