diff --git a/library/dataclasses.po b/library/dataclasses.po index 55431d8f40..b7a722f87c 100644 --- a/library/dataclasses.po +++ b/library/dataclasses.po @@ -6,7 +6,7 @@ msgstr "" "Project-Id-Version: Python 3.11\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2023-01-07 00:15+0000\n" -"PO-Revision-Date: 2018-07-15 18:56+0800\n" +"PO-Revision-Date: 2023-02-11 15:02+0800\n" "Last-Translator: \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" @@ -14,64 +14,83 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 3.2.2\n" #: ../../library/dataclasses.rst:2 msgid ":mod:`dataclasses` --- Data Classes" -msgstr "" +msgstr ":mod:`dataclasses` --- Data Classes" #: ../../library/dataclasses.rst:10 msgid "**Source code:** :source:`Lib/dataclasses.py`" msgstr "**原始碼:**\\ :source:`Lib/dataclasses.py`" #: ../../library/dataclasses.rst:14 +#, fuzzy msgid "" "This module provides a decorator and functions for automatically adding " "generated :term:`special method`\\s such as :meth:`__init__` and :meth:" "`__repr__` to user-defined classes. It was originally described in :pep:" "`557`." msgstr "" +"該模組提供了一個裝飾器和函式,用於自動新增生成的特殊方法,例如 :meth:" +"`__init__` 和 :meth:`__repr__` 到使用者定義的類。它最初在 :pep:`557` 中描述。" #: ../../library/dataclasses.rst:19 +#, fuzzy msgid "" "The member variables to use in these generated methods are defined using :" "pep:`526` type annotations. For example, this code::" msgstr "" +"在這些生成的方法中使用的成員變數是使用 :pep:`526` 型別註釋定義的。例如,這段" +"程式碼:" #: ../../library/dataclasses.rst:34 +#, fuzzy msgid "will add, among other things, a :meth:`__init__` that looks like::" -msgstr "" +msgstr "將新增,除其他事項外,一個 :meth:`__init__` 看起來像:" #: ../../library/dataclasses.rst:41 +#, fuzzy msgid "" "Note that this method is automatically added to the class: it is not " "directly specified in the ``InventoryItem`` definition shown above." msgstr "" +"請注意,此方法會自動新增到類中:它不會在上面顯示的“InventoryItem”定義中直接指" +"定。" #: ../../library/dataclasses.rst:47 +#, fuzzy msgid "Module contents" -msgstr "" +msgstr "模組內容" #: ../../library/dataclasses.rst:51 +#, fuzzy msgid "" "This function is a :term:`decorator` that is used to add generated :term:" "`special method`\\s to classes, as described below." -msgstr "" +msgstr "此函式是一個裝飾器,用於將生成的特殊方法新增到類中,如下所述。" #: ../../library/dataclasses.rst:54 +#, fuzzy msgid "" "The :func:`dataclass` decorator examines the class to find ``field``\\s. A " "``field`` is defined as a class variable that has a :term:`type annotation " "`. With two exceptions described below, nothing in :" "func:`dataclass` examines the type specified in the variable annotation." msgstr "" +":func:`dataclass` 裝飾器檢查類以找到 ``field``\\s。 ``field`` 被定義為具有 :" +"term:`type annotation ` 的類變數。除了下面描述的兩個例" +"外,:func:`dataclass` 中沒有任何內容檢查變數註釋中指定的型別。" #: ../../library/dataclasses.rst:60 +#, fuzzy msgid "" "The order of the fields in all of the generated methods is the order in " "which they appear in the class definition." -msgstr "" +msgstr "所有生成的方法中欄位的順序是它們在類定義中出現的順序。" #: ../../library/dataclasses.rst:63 +#, fuzzy msgid "" "The :func:`dataclass` decorator will add various \"dunder\" methods to the " "class, described below. If any of the added methods already exist in the " @@ -79,29 +98,39 @@ msgid "" "decorator returns the same class that it is called on; no new class is " "created." msgstr "" +":func:`dataclass` 裝飾器將向類新增各種“dunder”方法,如下所述。如果類中已存在" +"任何新增的方法,則行為取決於參數,如下所述。裝飾器回傳呼叫它的同一個類;沒有" +"建立新類。" #: ../../library/dataclasses.rst:69 +#, fuzzy msgid "" "If :func:`dataclass` is used just as a simple decorator with no parameters, " "it acts as if it has the default values documented in this signature. That " "is, these three uses of :func:`dataclass` are equivalent::" msgstr "" +"如果 :func:`dataclass` 僅用作不帶參數的簡單裝飾器,它的行為就好像它具有此簽名" +"中記錄的預設值一樣。也就是說,:func:`dataclass` 的這三種用法是等價的::" #: ../../library/dataclasses.rst:87 +#, fuzzy msgid "The parameters to :func:`dataclass` are:" -msgstr "" +msgstr ":func:`dataclass` 的參數是:" #: ../../library/dataclasses.rst:89 +#, fuzzy msgid "" "``init``: If true (the default), a :meth:`__init__` method will be generated." -msgstr "" +msgstr "``init``:如果為真(預設值),將生成一個 :meth:`__init__` 方法。" #: ../../library/dataclasses.rst:92 +#, fuzzy msgid "" "If the class already defines :meth:`__init__`, this parameter is ignored." -msgstr "" +msgstr "如果該類已經定義了 :meth:`__init__`,則忽略此參數。" #: ../../library/dataclasses.rst:95 +#, fuzzy msgid "" "``repr``: If true (the default), a :meth:`__repr__` method will be " "generated. The generated repr string will have the class name and the name " @@ -110,24 +139,34 @@ msgid "" "example: ``InventoryItem(name='widget', unit_price=3.0, " "quantity_on_hand=10)``." msgstr "" +"``repr``:如果為真(預設值),將生成一個 :meth:`__repr__` 方法。生成的 repr " +"字串將包含類名以及每個欄位的名稱和 repr,按照它們在類中定義的順序排列。不包括" +"標記為從 repr 中排除的欄位。例如:``InventoryItem(name='widget', " +"unit_price=3.0, quantity_on_hand=10)``。" #: ../../library/dataclasses.rst:102 +#, fuzzy msgid "" "If the class already defines :meth:`__repr__`, this parameter is ignored." -msgstr "" +msgstr "如果該類已經定義了 :meth:`__repr__`,則忽略此參數。" #: ../../library/dataclasses.rst:105 +#, fuzzy msgid "" "``eq``: If true (the default), an :meth:`__eq__` method will be generated. " "This method compares the class as if it were a tuple of its fields, in " "order. Both instances in the comparison must be of the identical type." msgstr "" +"``eq``:如果為真(預設值),將生成一個 :meth:`__eq__` 方法。此方法按順序比較" +"類,就好像它是其欄位的元組一樣。比較中的兩個實例必須屬於同一型別。" #: ../../library/dataclasses.rst:110 +#, fuzzy msgid "If the class already defines :meth:`__eq__`, this parameter is ignored." -msgstr "" +msgstr "如果該類已經定義了 :meth:`__eq__`,則忽略此參數。" #: ../../library/dataclasses.rst:113 +#, fuzzy msgid "" "``order``: If true (the default is ``False``), :meth:`__lt__`, :meth:" "`__le__`, :meth:`__gt__`, and :meth:`__ge__` methods will be generated. " @@ -135,20 +174,31 @@ msgid "" "instances in the comparison must be of the identical type. If ``order`` is " "true and ``eq`` is false, a :exc:`ValueError` is raised." msgstr "" +"``order``:如果為真(預設為 ``False``),:meth:`__lt__`、:meth:`__le__`、:" +"meth:`__gt__` 和 :meth:`__ge__` 方法將是產生。它們按順序比較類,就好像它是其" +"欄位的元組一樣。比較中的兩個實例必須屬於同一型別。如果 ``order`` 為真且 " +"``eq`` 為假,則會引發 :exc:`ValueError`。" #: ../../library/dataclasses.rst:120 +#, fuzzy msgid "" "If the class already defines any of :meth:`__lt__`, :meth:`__le__`, :meth:" "`__gt__`, or :meth:`__ge__`, then :exc:`TypeError` is raised." msgstr "" +"如果該類已經定義了 :meth:`__lt__`、:meth:`__le__`、:meth:`__gt__` 或 :meth:" +"`__ge__` 中的任何一個,則引發 :exc:`TypeError`。" #: ../../library/dataclasses.rst:124 +#, fuzzy msgid "" "``unsafe_hash``: If ``False`` (the default), a :meth:`__hash__` method is " "generated according to how ``eq`` and ``frozen`` are set." msgstr "" +"``unsafe_hash``:如果``False``(預設值),將根據``eq`` 和``frozen`` 的設定生" +"成一個:meth:`__hash__` 方法。" #: ../../library/dataclasses.rst:127 +#, fuzzy msgid "" ":meth:`__hash__` is used by built-in :meth:`hash()`, and when objects are " "added to hashed collections such as dictionaries and sets. Having a :meth:" @@ -157,8 +207,13 @@ msgid "" "existence and behavior of :meth:`__eq__`, and the values of the ``eq`` and " "``frozen`` flags in the :func:`dataclass` decorator." msgstr "" +":meth:`__hash__` 由內置的:meth:`hash()` 使用,當對像被新增到散列集合(如字典" +"和集合)時。擁有 :meth:`__hash__` 意味著該類的實例是不可變的。可變性是一個複" +"雜的屬性,它取決於程序員的意圖、__eq__ 的存在和行為,以及 dataclass 裝飾器中" +"的 eq 和 frozen 旗標的值." #: ../../library/dataclasses.rst:134 +#, fuzzy msgid "" "By default, :func:`dataclass` will not implicitly add a :meth:`__hash__` " "method unless it is safe to do so. Neither will it add or change an " @@ -166,8 +221,13 @@ msgid "" "attribute ``__hash__ = None`` has a specific meaning to Python, as described " "in the :meth:`__hash__` documentation." msgstr "" +"預設情況下,:func:`dataclass` 不會隱式新增 :meth:`__hash__` 方法,除非這樣做" +"是安全的。它也不會新增或更改現有的明確定義的 :meth:`__hash__` 方法。設定類屬" +"性 ``__hash__ = None`` 對 Python 具有特定含義,如 :meth:`__hash__` 文檔中所" +"述。" #: ../../library/dataclasses.rst:140 +#, fuzzy msgid "" "If :meth:`__hash__` is not explicitly defined, or if it is set to ``None``, " "then :func:`dataclass` *may* add an implicit :meth:`__hash__` method. " @@ -176,16 +236,26 @@ msgid "" "class is logically immutable but can nonetheless be mutated. This is a " "specialized use case and should be considered carefully." msgstr "" +"如果 :meth:`__hash__` 沒有明確定義,或者如果它被設定為 ``None``,那麼 :func:" +"`dataclass` *可能* 新增一個隱式的 :meth:`__hash__` 方法。雖然不推薦,但您可以" +"強制 :func:`dataclass` 使用 ``unsafe_hash=True`` 建立一個 :meth:`__hash__` 方" +"法。如果您的類在邏輯上是不可變的但仍然可以改變,則可能是這種情況。這是一個特" +"殊的用例,應該仔細考慮。" #: ../../library/dataclasses.rst:147 +#, fuzzy msgid "" "Here are the rules governing implicit creation of a :meth:`__hash__` " "method. Note that you cannot both have an explicit :meth:`__hash__` method " "in your dataclass and set ``unsafe_hash=True``; this will result in a :exc:" "`TypeError`." msgstr "" +"以下是管理隱式建立 :meth:`__hash__` 方法的規則。請注意,您不能在資料類中既有" +"顯式的 :meth:`__hash__` 方法又設定 ``unsafe_hash=True``;這將導致 :exc:" +"`TypeError`。" #: ../../library/dataclasses.rst:152 +#, fuzzy msgid "" "If ``eq`` and ``frozen`` are both true, by default :func:`dataclass` will " "generate a :meth:`__hash__` method for you. If ``eq`` is true and " @@ -195,16 +265,27 @@ msgid "" "superclass will be used (if the superclass is :class:`object`, this means it " "will fall back to id-based hashing)." msgstr "" +"如果``eq`` 和``frozen`` 都為真,預設情況下:func:`dataclass` 會為你生成一個:" +"meth:`__hash__` 方法。如果 ``eq`` 為真且 ``frozen`` 為假,:meth:`__hash__` 將" +"被設定為 ``None``,將其標記為不可散列(它是不可散列的,因為它是可變的)。如" +"果 ``eq`` 為假,:meth:`__hash__` 將保持不變,這意味著將使用超類的 :meth:" +"`__hash__` 方法(如果超類是 :class:`object`,這意味著它將回退到基於 id 的散" +"列)。" #: ../../library/dataclasses.rst:160 +#, fuzzy msgid "" "``frozen``: If true (the default is ``False``), assigning to fields will " "generate an exception. This emulates read-only frozen instances. If :meth:" "`__setattr__` or :meth:`__delattr__` is defined in the class, then :exc:" "`TypeError` is raised. See the discussion below." msgstr "" +"``frozen``:如果為真(預設為``False``),分配給欄位將產生例外。這模擬了只讀的" +"凍結實例。如果 :meth:`__setattr__` 或 :meth:`__delattr__` 在類中定義,則 :" +"exc:`TypeError` 被引發。請參閱下面的討論。" #: ../../library/dataclasses.rst:165 +#, fuzzy msgid "" "``match_args``: If true (the default is ``True``), the ``__match_args__`` " "tuple will be created from the list of parameters to the generated :meth:" @@ -212,8 +293,13 @@ msgid "" "If false, or if ``__match_args__`` is already defined in the class, then " "``__match_args__`` will not be generated." msgstr "" +"``match_args``:如果為真(預設為 ``True``),``__match_args__`` 元組將從參數" +"列表建立到生成的 :meth:`__init__` 方法(即使 :meth: `__init__` 未生成,見上" +"文)。如果為 false,或者類中已經定義了 __match_args__ ,則不會生成 " +"__match_args__ 。" #: ../../library/dataclasses.rst:174 +#, fuzzy msgid "" "``kw_only``: If true (the default value is ``False``), then all fields will " "be marked as keyword-only. If a field is marked as keyword-only, then the " @@ -223,16 +309,26 @@ msgid "" "term:`parameter` glossary entry for details. Also see the :const:`KW_ONLY` " "section." msgstr "" +"``kw_only``:如果為 true(預設值為 ``False``),則所有欄位將被標記為僅限關鍵" +"字。如果一個欄位被標記為僅限關鍵字,那麼唯一的影響是從僅限關鍵字欄位生成的 :" +"meth:`__init__` 參數必須在呼叫 :meth:`__init__` 時指定關鍵字。對資料類的任何" +"其他方面都沒有影響。有關詳細資訊,請參閱:term:`parameter` 詞彙表條目。另請參" +"閱:const:`KW_ONLY` 部分。" #: ../../library/dataclasses.rst:185 +#, fuzzy msgid "" "``slots``: If true (the default is ``False``), :attr:`__slots__` attribute " "will be generated and new class will be returned instead of the original " "one. If :attr:`__slots__` is already defined in the class, then :exc:" "`TypeError` is raised." msgstr "" +"``slots``:如果為 true(預設為 ``False``),將生成:attr:`__slots__` 屬性並回" +"傳新類而不是原始類。如果 :attr:`__slots__` 已經在類中定義,則 :exc:" +"`TypeError` 被引發。" #: ../../library/dataclasses.rst:192 +#, fuzzy msgid "" "If a field name is already included in the ``__slots__`` of a base class, it " "will not be included in the generated ``__slots__`` to prevent :ref:" @@ -241,35 +337,51 @@ msgid "" "instead. To be able to determine inherited slots, base class ``__slots__`` " "may be any iterable, but *not* an iterator." msgstr "" +"如果欄位名稱已經包含在基底類別的 ``__slots__`` 中,它將不會包含在生成的 " +"``__slots__`` 中以防止 :ref:`覆蓋它們 `。因此,不要使" +"用 __slots__ 來檢索資料類的欄位名稱。使用 :func:`fields` 代替。為了能夠確定繼" +"承的插槽,基底類別 ``__slots__`` 可以是任何可疊代的,但*不是*疊代器。" #: ../../library/dataclasses.rst:202 +#, fuzzy msgid "" "``weakref_slot``: If true (the default is ``False``), add a slot named " "\"__weakref__\", which is required to make an instance weakref-able. It is " "an error to specify ``weakref_slot=True`` without also specifying " "``slots=True``." msgstr "" +"``weakref_slot``:如果為真(預設為``False``),新增一個名為“__weakref__”的插" +"槽,這是使實例可弱引用所必需的。在沒有指定 ``slots=True`` 的情況下指定 " +"``weakref_slot=True`` 是錯誤的。" #: ../../library/dataclasses.rst:209 +#, fuzzy msgid "" "``field``\\s may optionally specify a default value, using normal Python " "syntax::" -msgstr "" +msgstr "``field``\\s 可以選擇指定一個預設值,使用普通的 Python 語法:" #: ../../library/dataclasses.rst:217 +#, fuzzy msgid "" "In this example, both ``a`` and ``b`` will be included in the added :meth:" "`__init__` method, which will be defined as::" msgstr "" +"在此示例中,``a`` 和``b`` 都將包含在新增的 :meth:`__init__` 方法中,該方法將" +"定義為:" #: ../../library/dataclasses.rst:222 +#, fuzzy msgid "" ":exc:`TypeError` will be raised if a field without a default value follows a " "field with a default value. This is true whether this occurs in a single " "class, or as a result of class inheritance." msgstr "" +":exc:`TypeError` 如果沒有預設值的欄位跟在具有預設值的欄位之後,將引發。無論這" +"發生在單個類中還是作為類繼承的結果,都是如此。" #: ../../library/dataclasses.rst:228 +#, fuzzy msgid "" "For common and simple use cases, no other functionality is required. There " "are, however, some dataclass features that require additional per-field " @@ -277,27 +389,39 @@ msgid "" "replace the default field value with a call to the provided :func:`field` " "function. For example::" msgstr "" +"對於常見和簡單的用例,不需要其他功能。但是,有些資料類功能需要額外的每個欄位" +"資訊。為了滿足這種對附加資訊的需求,您可以通過呼叫提供的 :func:`field` 函式來" +"替換預設欄位值。例如::" #: ../../library/dataclasses.rst:241 +#, fuzzy msgid "" "As shown above, the :const:`MISSING` value is a sentinel object used to " "detect if some parameters are provided by the user. This sentinel is used " "because ``None`` is a valid value for some parameters with a distinct " "meaning. No code should directly use the :const:`MISSING` value." msgstr "" +"如上所示,:const:`MISSING` 值是一個哨兵物件,用於檢測某些參數是否由使用者提" +"供。使用此標記是因為“None”對於某些具有不同含義的參數是有效值。任何程式碼都不" +"應直接使用 :const:`MISSING` 值。" #: ../../library/dataclasses.rst:246 +#, fuzzy msgid "The parameters to :func:`field` are:" -msgstr "" +msgstr ":func:`field` 的參數是:" #: ../../library/dataclasses.rst:248 +#, fuzzy msgid "" "``default``: If provided, this will be the default value for this field. " "This is needed because the :meth:`field` call itself replaces the normal " "position of the default value." msgstr "" +"``default``:如果提供,這將是該欄位的預設值。這是必需的,因為 :meth:`field` " +"呼叫本身會替換預設值的正常位置。" #: ../../library/dataclasses.rst:252 +#, fuzzy msgid "" "``default_factory``: If provided, it must be a zero-argument callable that " "will be called when a default value is needed for this field. Among other " @@ -305,20 +429,30 @@ msgid "" "discussed below. It is an error to specify both ``default`` and " "``default_factory``." msgstr "" +"``default_factory``:如果提供,它必須是一個零參數可呼叫函式,當此欄位需要預設" +"值時將被呼叫。除其他用途外,這可用於指定具有可變預設值的欄位,如下所述。同時" +"指定 ``default`` 和 ``default_factory`` 是錯誤的。" #: ../../library/dataclasses.rst:258 +#, fuzzy msgid "" "``init``: If true (the default), this field is included as a parameter to " "the generated :meth:`__init__` method." msgstr "" +"``init``:如果為 true(預設值),則此欄位將作為生成的 __init__` 方法的參數包" +"含在內。" #: ../../library/dataclasses.rst:261 +#, fuzzy msgid "" "``repr``: If true (the default), this field is included in the string " "returned by the generated :meth:`__repr__` method." msgstr "" +"``repr``:如果為真(預設值),則此欄位包含在生成的 :meth:`__repr__` 方法回傳" +"的字串中。" #: ../../library/dataclasses.rst:264 +#, fuzzy msgid "" "``hash``: This can be a bool or ``None``. If true, this field is included " "in the generated :meth:`__hash__` method. If ``None`` (the default), use " @@ -326,8 +460,13 @@ msgid "" "field should be considered in the hash if it's used for comparisons. " "Setting this value to anything other than ``None`` is discouraged." msgstr "" +"``hash``:這可以是 bool 或 ``None``。如果為真,則此欄位包含在生成的 :meth:" +"`__hash__` 方法中。如果“無”(預設值),則使用“比較”的值:這通常是預期的行為。" +"如果一個欄位用於比較,則應在雜湊中考慮該欄位。不鼓勵將此值設定為“無”以外的任" +"何值。" #: ../../library/dataclasses.rst:271 +#, fuzzy msgid "" "One possible reason to set ``hash=False`` but ``compare=True`` would be if a " "field is expensive to compute a hash value for, that field is needed for " @@ -335,14 +474,21 @@ msgid "" "hash value. Even if a field is excluded from the hash, it will still be " "used for comparisons." msgstr "" +"設定 ``hash=False`` 但 ``compare=True`` 的一個可能原因是,如果一個欄位計算雜" +"湊值的成本很高,則需要該欄位進行相等性測試,並且還有其他欄位有助於型別的雜湊" +"值。即使一個欄位被排除在雜湊之外,它仍然會被用於比較。" #: ../../library/dataclasses.rst:277 +#, fuzzy msgid "" "``compare``: If true (the default), this field is included in the generated " "equality and comparison methods (:meth:`__eq__`, :meth:`__gt__`, et al.)." msgstr "" +"``compare``:如果為真(預設值),則此欄位包含在生成的相等和比較方法中(:meth:" +"`__eq__`、:meth:`__gt__` 等)。" #: ../../library/dataclasses.rst:281 +#, fuzzy msgid "" "``metadata``: This can be a mapping or None. None is treated as an empty " "dict. This value is wrapped in :func:`~types.MappingProxyType` to make it " @@ -351,14 +497,22 @@ msgid "" "Multiple third-parties can each have their own key, to use as a namespace in " "the metadata." msgstr "" +"``元資料``:這可以是映射或無。 None 被視為空字典。此值包含在 :func:`~types." +"MappingProxyType` 中以使其成為只讀的,並暴露在 :class:`Field` 對像上。它根本" +"不被資料類使用,而是作為第三方擴充機制提供的。多個第三方可以各自擁有自己的密" +"鑰,用作元資料中的命名空間。" #: ../../library/dataclasses.rst:289 +#, fuzzy msgid "" "``kw_only``: If true, this field will be marked as keyword-only. This is " "used when the generated :meth:`__init__` method's parameters are computed." msgstr "" +"``kw_only``:如果為真,該欄位將被標記為僅限關鍵字。這在計算生成的 :meth:" +"`__init__` 方法的參數時使用。" #: ../../library/dataclasses.rst:295 +#, fuzzy msgid "" "If the default value of a field is specified by a call to :func:`field()`, " "then the class attribute for this field will be replaced by the specified " @@ -368,91 +522,124 @@ msgid "" "fields, just as if the default value itself were specified. For example, " "after::" msgstr "" +"如果欄位的預設值是通過呼叫 :func:`field()` 指定的,那麼該欄位的類屬性將被指定" +"的``default`` 值替換。如果沒有提供``default``,那麼類屬性將被刪除。目的是在 :" +"func:`dataclass` 裝飾器運行後,類屬性將全部包含欄位的預設值,就像預設值本身已" +"指定一樣。例如,在::" #: ../../library/dataclasses.rst:311 +#, fuzzy msgid "" "The class attribute ``C.z`` will be ``10``, the class attribute ``C.t`` will " "be ``20``, and the class attributes ``C.x`` and ``C.y`` will not be set." -msgstr "" +msgstr "類屬性“C.z”將為“10”,類屬性“C.t”將為“20”,類屬性“C.x”和“C.y”將不會放。" #: ../../library/dataclasses.rst:317 +#, fuzzy msgid "" ":class:`Field` objects describe each defined field. These objects are " "created internally, and are returned by the :func:`fields` module-level " "method (see below). Users should never instantiate a :class:`Field` object " "directly. Its documented attributes are:" msgstr "" +":class:`Field` 物件描述每個定義的欄位。這些對像在內部建立,並由 :func:" +"`fields` 模組級方法回傳(見下文)。使用者不應該直接實例化 Field 物件。它記錄" +"的屬性是:" #: ../../library/dataclasses.rst:322 +#, fuzzy msgid "``name``: The name of the field." -msgstr "" +msgstr "``name``:欄位的名稱。" #: ../../library/dataclasses.rst:324 +#, fuzzy msgid "``type``: The type of the field." -msgstr "" +msgstr "``type``:欄位的型別。" #: ../../library/dataclasses.rst:326 +#, fuzzy msgid "" "``default``, ``default_factory``, ``init``, ``repr``, ``hash``, ``compare``, " "``metadata``, and ``kw_only`` have the identical meaning and values as they " "do in the :func:`field` function." msgstr "" +"``default``、``default_factory``、``init``、``repr``、``hash``、``compare``、" +"``metadata`` 和 ``kw_only`` 有與它們在 :func:`field` 函式中的含義和值相同。" #: ../../library/dataclasses.rst:330 +#, fuzzy msgid "" "Other attributes may exist, but they are private and must not be inspected " "or relied on." -msgstr "" +msgstr "可能存在其他屬性,但它們是私有的,不得檢查或依賴。" #: ../../library/dataclasses.rst:335 +#, fuzzy msgid "" "Returns a tuple of :class:`Field` objects that define the fields for this " "dataclass. Accepts either a dataclass, or an instance of a dataclass. " "Raises :exc:`TypeError` if not passed a dataclass or instance of one. Does " "not return pseudo-fields which are ``ClassVar`` or ``InitVar``." msgstr "" +"回傳定義此資料類欄位的 :class:`Field` 物件的元組。接受資料類或資料類的實例。" +"如果未傳遞資料類或其中一個實例,則引發 :exc:`TypeError`。不回傳 ``ClassVar`` " +"或 ``InitVar`` 的偽欄位。" #: ../../library/dataclasses.rst:342 +#, fuzzy msgid "" "Converts the dataclass ``obj`` to a dict (by using the factory function " "``dict_factory``). Each dataclass is converted to a dict of its fields, as " "``name: value`` pairs. dataclasses, dicts, lists, and tuples are recursed " "into. Other objects are copied with :func:`copy.deepcopy`." msgstr "" +"將資料類“obj”轉換為字典(通過使用工廠函式“dict_factory”)。每個資料類都被轉換" +"為其欄位的字典,作為“名稱:值”對。資料類、字典、列表和元組被遞迴到。其他物件" +"使用 :func:`copy.deepcopy` 複製。" #: ../../library/dataclasses.rst:348 +#, fuzzy msgid "Example of using :func:`asdict` on nested dataclasses::" -msgstr "" +msgstr "在嵌套資料類上使用 :func:`asdict` 的示例::" #: ../../library/dataclasses.rst:365 ../../library/dataclasses.rst:385 +#, fuzzy msgid "To create a shallow copy, the following workaround may be used::" -msgstr "" +msgstr "要建立淺拷貝,可以使用以下解決方法:" #: ../../library/dataclasses.rst:369 +#, fuzzy msgid "" ":func:`asdict` raises :exc:`TypeError` if ``obj`` is not a dataclass " "instance." -msgstr "" +msgstr ":func:`asdict` 如果 ``obj`` 不是資料類實例,則引發 :exc:`TypeError`。" #: ../../library/dataclasses.rst:374 +#, fuzzy msgid "" "Converts the dataclass ``obj`` to a tuple (by using the factory function " "``tuple_factory``). Each dataclass is converted to a tuple of its field " "values. dataclasses, dicts, lists, and tuples are recursed into. Other " "objects are copied with :func:`copy.deepcopy`." msgstr "" +"將資料類“obj”轉換為元組(通過使用工廠函式“tuple_factory”)。每個資料類都被轉" +"換為其欄位值的元組。資料類、字典、列表和元組被遞迴到。其他物件使用 :func:" +"`copy.deepcopy` 複製。" #: ../../library/dataclasses.rst:380 +#, fuzzy msgid "Continuing from the previous example::" -msgstr "" +msgstr "從前面的例子繼續:" #: ../../library/dataclasses.rst:389 +#, fuzzy msgid "" ":func:`astuple` raises :exc:`TypeError` if ``obj`` is not a dataclass " "instance." -msgstr "" +msgstr ":func:`astuple` 如果 ``obj`` 不是資料類實例,則引發 :exc:`TypeError`。" #: ../../library/dataclasses.rst:394 +#, fuzzy msgid "" "Creates a new dataclass with name ``cls_name``, fields as defined in " "``fields``, base classes as given in ``bases``, and initialized with a " @@ -463,48 +650,73 @@ msgid "" "``unsafe_hash``, ``frozen``, ``match_args``, ``kw_only``, ``slots``, and " "``weakref_slot`` have the same meaning as they do in :func:`dataclass`." msgstr "" +"建立一個名為“cls_name”的新資料類,欄位在“fields”中定義,基底類別在“bases”中給" +"出,並使用“namespace”中給出的命名空間進行初始化。 ``fields`` 是一個疊代器,其" +"元素分別是 ``name``、``(name, type)`` 或 ``(name, type, Field)``。如果只提供 " +"``name``,則 ``typing.Any`` 用於 ``type``。 ``init``、``repr``、``eq``、" +"``order``、``unsafe_hash``、``frozen``、``match_args``、``kw_only`` 的值, " +"``slots`` 和 ``weakref_slot`` 與它們在 :func:`dataclass` 中的含義相同。" #: ../../library/dataclasses.rst:404 +#, fuzzy msgid "" "This function is not strictly required, because any Python mechanism for " "creating a new class with ``__annotations__`` can then apply the :func:" "`dataclass` function to convert that class to a dataclass. This function is " "provided as a convenience. For example::" msgstr "" +"這個函式不是嚴格要求的,因為任何使用 ``__annotations__`` 建立新類的 Python 機" +"制都可以應用 :func:`dataclass` 函式將該類轉換為資料類。提供此功能是為了方便。" +"例如::" #: ../../library/dataclasses.rst:416 +#, fuzzy msgid "Is equivalent to::" -msgstr "" +msgstr "相當於::" #: ../../library/dataclasses.rst:429 +#, fuzzy msgid "" "Creates a new object of the same type as ``obj``, replacing fields with " "values from ``changes``. If ``obj`` is not a Data Class, raises :exc:" "`TypeError`. If values in ``changes`` do not specify fields, raises :exc:" "`TypeError`." msgstr "" +"建立一個與 ``obj`` 型別相同的新物件,用 ``changes`` 中的值替換欄位。如果 " +"``obj`` 不是資料類,則引發 :exc:`TypeError`。如果 ``changes`` 中的值未指定欄" +"位,則引發:exc:`TypeError`。" #: ../../library/dataclasses.rst:434 +#, fuzzy msgid "" "The newly returned object is created by calling the :meth:`__init__` method " "of the dataclass. This ensures that :meth:`__post_init__`, if present, is " "also called." msgstr "" +"新回傳的對像是通過呼叫資料類的 :meth:`__init__` 方法建立的。這確保 :meth:" +"`__post_init__`(如果存在)也被呼叫。" #: ../../library/dataclasses.rst:438 +#, fuzzy msgid "" "Init-only variables without default values, if any exist, must be specified " "on the call to :func:`replace` so that they can be passed to :meth:" "`__init__` and :meth:`__post_init__`." msgstr "" +"沒有預設值的僅初始化變數(如果存在)必須在呼叫 :func:`replace` 時指定,以便它" +"們可以傳遞給 :meth:`__init__` 和 :meth:`__post_init__`。" #: ../../library/dataclasses.rst:442 +#, fuzzy msgid "" "It is an error for ``changes`` to contain any fields that are defined as " "having ``init=False``. A :exc:`ValueError` will be raised in this case." msgstr "" +"``changes`` 包含任何定義為具有 ``init=False`` 的欄位是錯誤的。在這種情況下將" +"引發 :exc:`ValueError`。" #: ../../library/dataclasses.rst:446 +#, fuzzy msgid "" "Be forewarned about how ``init=False`` fields work during a call to :func:" "`replace`. They are not copied from the source object, but rather are " @@ -514,25 +726,36 @@ msgid "" "perhaps a custom ``replace()`` (or similarly named) method which handles " "instance copying." msgstr "" +"預先警告 ``init=False`` 欄位在呼叫 :func:`replace` 期間是如何工作的。它們不是" +"從源物件複製的,而是在 :meth:`__post_init__` 中初始化的,如果它們被初始化的" +"話。預計 ``init=False`` 欄位將很少被明智地使用。如果使用它們,使用替代的類構" +"造函式可能是明智的,或者可能是處理實例複製的自定義“replace()”(或類似命名的)" +"方法。" #: ../../library/dataclasses.rst:457 +#, fuzzy msgid "" "Return ``True`` if its parameter is a dataclass or an instance of one, " "otherwise return ``False``." -msgstr "" +msgstr "如果它的參數是一個資料類或一個實例,則回傳“True”,否則回傳“False”。" #: ../../library/dataclasses.rst:460 +#, fuzzy msgid "" "If you need to know if a class is an instance of a dataclass (and not a " "dataclass itself), then add a further check for ``not isinstance(obj, " "type)``::" msgstr "" +"如果你需要知道一個類是否是資料類的實例(而不是資料類本身),那麼新增一個進一" +"步的檢查``not isinstance(obj, type)``::" #: ../../library/dataclasses.rst:469 +#, fuzzy msgid "A sentinel value signifying a missing default or default_factory." -msgstr "" +msgstr "表示缺少預設值或 default_factory 的標記值。" #: ../../library/dataclasses.rst:473 +#, fuzzy msgid "" "A sentinel value used as a type annotation. Any fields after a pseudo-field " "with the type of :const:`KW_ONLY` are marked as keyword-only fields. Note " @@ -542,31 +765,42 @@ msgid "" "meth:`__init__` parameters that must be specified as keywords when the class " "is instantiated." msgstr "" +"用作型別註釋的標記值。型別為 KW_ONLY 的偽欄位之後的任何欄位都被標記為僅關鍵字" +"欄位。請注意,KW_ONLY 型別的偽欄位將被完全忽略。這包括此類欄位的名稱。按照慣" +"例,名稱 _ 用於 :const: `KW_ONLY` 欄位。僅關鍵字欄位表示 :meth:`__init__` 參" +"數,在實例化類時必須將其指定為關鍵字。" #: ../../library/dataclasses.rst:482 +#, fuzzy msgid "" "In this example, the fields ``y`` and ``z`` will be marked as keyword-only " "fields::" -msgstr "" +msgstr "在此示例中,欄位 ``y`` 和 ``z`` 將被標記為僅關鍵字欄位::" #: ../../library/dataclasses.rst:493 +#, fuzzy msgid "" "In a single dataclass, it is an error to specify more than one field whose " "type is :const:`KW_ONLY`." -msgstr "" +msgstr "在單個資料類中,指定多個型別為 KW_ONLY 的欄位是錯誤的。" #: ../../library/dataclasses.rst:500 +#, fuzzy msgid "" "Raised when an implicitly defined :meth:`__setattr__` or :meth:`__delattr__` " "is called on a dataclass which was defined with ``frozen=True``. It is a " "subclass of :exc:`AttributeError`." msgstr "" +"當在使用 frozen=True 定義的資料類上呼叫隱式定義的 :meth:`__setattr__` 或 :" +"meth:`__delattr__` 時引發。它是 :exc:`AttributeError` 的子類別。" #: ../../library/dataclasses.rst:505 +#, fuzzy msgid "Post-init processing" -msgstr "" +msgstr "初始化後處理" #: ../../library/dataclasses.rst:507 +#, fuzzy msgid "" "The generated :meth:`__init__` code will call a method named :meth:" "`__post_init__`, if :meth:`__post_init__` is defined on the class. It will " @@ -575,40 +809,60 @@ msgid "" "order they were defined in the class. If no :meth:`__init__` method is " "generated, then :meth:`__post_init__` will not automatically be called." msgstr "" +"生成的 :meth:`__init__` 程式碼將呼叫一個名為 :meth:`__post_init__` 的方法,如" +"果 :meth:`__post_init__` 是在類上定義的。它通常被稱為 ``self." +"__post_init__()``。但是,如果定義了任何 ``InitVar`` 欄位,它們也將按照它們在" +"類中定義的順序傳遞給 :meth:`__post_init__` 。如果沒有生成 :meth:`__init__` 方" +"法,那麼 :meth:`__post_init__` 將不會被自動呼叫。" #: ../../library/dataclasses.rst:515 +#, fuzzy msgid "" "Among other uses, this allows for initializing field values that depend on " "one or more other fields. For example::" msgstr "" +"在其他用途\\u200b\\u200b中,這允許初始化依賴於一個或多個其他欄位的欄位值。例" +"如::" #: ../../library/dataclasses.rst:527 +#, fuzzy msgid "" "The :meth:`__init__` method generated by :func:`dataclass` does not call " "base class :meth:`__init__` methods. If the base class has an :meth:" "`__init__` method that has to be called, it is common to call this method in " "a :meth:`__post_init__` method::" msgstr "" +":func:`dataclass` 生成的:meth:`__init__` 方法不呼叫基底類別:meth:`__init__` " +"方法。如果基底類別有一個必須呼叫的 :meth:`__init__` 方法,通常在 :meth:" +"`__post_init__` 方法中呼叫此方法::" #: ../../library/dataclasses.rst:544 +#, fuzzy msgid "" "Note, however, that in general the dataclass-generated :meth:`__init__` " "methods don't need to be called, since the derived dataclass will take care " "of initializing all fields of any base class that is a dataclass itself." msgstr "" +"但是請注意,通常不需要呼叫資料類生成的 :meth:`__init__` 方法,因為派生資料類" +"將負責初始化作為資料類本身的任何基底類別的所有欄位。" #: ../../library/dataclasses.rst:548 +#, fuzzy msgid "" "See the section below on init-only variables for ways to pass parameters to :" "meth:`__post_init__`. Also see the warning about how :func:`replace` " "handles ``init=False`` fields." msgstr "" +"請參閱下面有關僅初始化變數的部分,了解將參數傳遞給 :meth:`__post_init__` 的方" +"法。另請參閱有關 :func:`replace` 如何處理 ``init=False`` 欄位的警告。" #: ../../library/dataclasses.rst:553 +#, fuzzy msgid "Class variables" -msgstr "" +msgstr "類變數" #: ../../library/dataclasses.rst:555 +#, fuzzy msgid "" "One of the few places where :func:`dataclass` actually inspects the type of " "a field is to determine if a field is a class variable as defined in :pep:" @@ -617,12 +871,18 @@ msgid "" "as a field and is ignored by the dataclass mechanisms. Such ``ClassVar`` " "pseudo-fields are not returned by the module-level :func:`fields` function." msgstr "" +":func:`dataclass` 實際檢查欄位型別的少數地方之一是確定欄位是否是 :pep:`526` " +"中定義的類變數。它通過檢查欄位的型別是否為“typing.ClassVar”來做到這一點。如果" +"一個欄位是一個“ClassVar”,它就被排除在考慮之外,並被資料類機制忽略。模組級 :" +"func:`fields` 函式不會回傳此類 ``ClassVar`` 偽欄位。" #: ../../library/dataclasses.rst:564 +#, fuzzy msgid "Init-only variables" -msgstr "" +msgstr "僅初始化變數" #: ../../library/dataclasses.rst:566 +#, fuzzy msgid "" "Another place where :func:`dataclass` inspects a type annotation is to " "determine if a field is an init-only variable. It does this by seeing if " @@ -633,24 +893,36 @@ msgid "" "generated :meth:`__init__` method, and are passed to the optional :meth:" "`__post_init__` method. They are not otherwise used by dataclasses." msgstr "" +":func:`dataclass` 檢查型別註解的另一個地方是確定欄位是否是僅初始化變數。它通" +"過查看欄位的型別是否為“dataclasses.InitVar”型別來執行此操作。如果一個欄位是一" +"個“InitVar”,它被認為是一個偽欄位,稱為 init-only 欄位。由於它不是真正的欄" +"位,因此它不會由模組級 fields 函式回傳。 Init-only 欄位作為參數新增到生成的 :" +"meth:`__init__` 方法,並傳遞給可選的 :meth:`__post_init__` 方法。它們不被資料" +"類使用。" #: ../../library/dataclasses.rst:576 +#, fuzzy msgid "" "For example, suppose a field will be initialized from a database, if a value " "is not provided when creating the class::" -msgstr "" +msgstr "例如,假設一個欄位將從資料庫中初始化,如果在建立類時沒有提供值::" #: ../../library/dataclasses.rst:591 +#, fuzzy msgid "" "In this case, :func:`fields` will return :class:`Field` objects for ``i`` " "and ``j``, but not for ``database``." msgstr "" +"在這種情況下,:func:`fields` 將為 ``i`` 和 ``j`` 回傳 :class:`Field` 物件,但" +"不會為 ``database`` 回傳。" #: ../../library/dataclasses.rst:595 +#, fuzzy msgid "Frozen instances" -msgstr "" +msgstr "凍結實例" #: ../../library/dataclasses.rst:597 +#, fuzzy msgid "" "It is not possible to create truly immutable Python objects. However, by " "passing ``frozen=True`` to the :meth:`dataclass` decorator you can emulate " @@ -658,19 +930,28 @@ msgid "" "meth:`__delattr__` methods to the class. These methods will raise a :exc:" "`FrozenInstanceError` when invoked." msgstr "" +"不可能建立真正不可變的 Python 物件。但是,通過將 ``frozen=True`` 傳遞給 :" +"meth:`dataclass` 裝飾器,您可以模擬不變性。在這種情況下,資料類將向類新增 :" +"meth:`__setattr__` 和 :meth:`__delattr__` 方法。這些方法在呼叫時會引發:exc:" +"`FrozenInstanceError`。" #: ../../library/dataclasses.rst:603 +#, fuzzy msgid "" "There is a tiny performance penalty when using ``frozen=True``: :meth:" "`__init__` cannot use simple assignment to initialize fields, and must use :" "meth:`object.__setattr__`." msgstr "" +"使用 ``frozen=True`` 時有一個微小的性能損失::meth:`__init__` 不能使用簡單賦" +"值來初始化欄位,必須使用 :meth:`object.__setattr__`。" #: ../../library/dataclasses.rst:608 +#, fuzzy msgid "Inheritance" -msgstr "" +msgstr "遺產" #: ../../library/dataclasses.rst:610 +#, fuzzy msgid "" "When the dataclass is being created by the :meth:`dataclass` decorator, it " "looks through all of the class's base classes in reverse MRO (that is, " @@ -681,96 +962,131 @@ msgid "" "ordered mapping of fields. Because the fields are in insertion order, " "derived classes override base classes. An example::" msgstr "" +"當 :meth:`dataclass` 裝飾器建立資料類時,它會以反向 MRO(即從 :class:" +"`object` 開始)查看該類的所有基底類別,並且對於它找到的每個資料類,將該基底類" +"別中的欄位新增到欄位的有序映射中。新增所有基底類別欄位後,它會將自己的欄位新" +"增到有序映射中。所有生成的方法都將使用這種組合的、計算的有序欄位映射。因為欄" +"位是按插入順序排列的,所以派生類會覆蓋基底類別。一個例子::" #: ../../library/dataclasses.rst:630 +#, fuzzy msgid "" "The final list of fields is, in order, ``x``, ``y``, ``z``. The final type " "of ``x`` is ``int``, as specified in class ``C``." msgstr "" +"最終的欄位列表按順序為“x”、“y”、“z”。 ``x`` 的最終型別是 ``int``,如類 ``C`` " +"中指定的那樣。" #: ../../library/dataclasses.rst:633 +#, fuzzy msgid "The generated :meth:`__init__` method for ``C`` will look like::" -msgstr "" +msgstr "為 ``C`` 生成的 :meth:`__init__` 方法將如下所示:" #: ../../library/dataclasses.rst:638 +#, fuzzy msgid "Re-ordering of keyword-only parameters in :meth:`__init__`" -msgstr "" +msgstr ":meth:`__init__` 中僅關鍵字參數的重新排序" #: ../../library/dataclasses.rst:640 +#, fuzzy msgid "" "After the parameters needed for :meth:`__init__` are computed, any keyword-" "only parameters are moved to come after all regular (non-keyword-only) " "parameters. This is a requirement of how keyword-only parameters are " "implemented in Python: they must come after non-keyword-only parameters." msgstr "" +"在計算 :meth:`__init__` 所需的參數後,任何僅關鍵字參數都將移動到所有常規(非" +"僅關鍵字)參數之後。這是如何在 Python 中實作僅關鍵字參數的要求:它們必須位於" +"非僅關鍵字參數之後。" #: ../../library/dataclasses.rst:646 +#, fuzzy msgid "" "In this example, ``Base.y``, ``Base.w``, and ``D.t`` are keyword-only " "fields, and ``Base.x`` and ``D.z`` are regular fields::" msgstr "" +"在此示例中,``Base.y``、``Base.w`` 和``D.t`` 是僅限關鍵字的欄位,``Base.x`` " +"和``D.z`` 是常規欄位: :" #: ../../library/dataclasses.rst:661 +#, fuzzy msgid "The generated :meth:`__init__` method for ``D`` will look like::" -msgstr "" +msgstr "為 ``D`` 生成的 :meth:`__init__` 方法將如下所示:" #: ../../library/dataclasses.rst:665 +#, fuzzy msgid "" "Note that the parameters have been re-ordered from how they appear in the " "list of fields: parameters derived from regular fields are followed by " "parameters derived from keyword-only fields." msgstr "" +"請注意,參數已根據它們在欄位列表中的顯示方式重新排序:從常規欄位派生的參數後" +"跟從僅關鍵字欄位派生的參數。" #: ../../library/dataclasses.rst:669 +#, fuzzy msgid "" "The relative ordering of keyword-only parameters is maintained in the re-" "ordered :meth:`__init__` parameter list." -msgstr "" +msgstr "僅關鍵字參數的相對順序在重新排序的 :meth:`__init__` 參數列表中維護。" #: ../../library/dataclasses.rst:674 +#, fuzzy msgid "Default factory functions" -msgstr "" +msgstr "預設工廠函式" #: ../../library/dataclasses.rst:676 +#, fuzzy msgid "" "If a :func:`field` specifies a ``default_factory``, it is called with zero " "arguments when a default value for the field is needed. For example, to " "create a new instance of a list, use::" msgstr "" +"如果 :func:`field` 指定了 ``default_factory``,當需要該欄位的預設值時,它會以" +"零參數呼叫。例如,要建立列表的新實例,請使用:" #: ../../library/dataclasses.rst:682 +#, fuzzy msgid "" "If a field is excluded from :meth:`__init__` (using ``init=False``) and the " "field also specifies ``default_factory``, then the default factory function " "will always be called from the generated :meth:`__init__` function. This " "happens because there is no other way to give the field an initial value." msgstr "" +"如果一個欄位從 :meth:`__init__` 中排除(使用 ``init=False``)並且該欄位還指定" +"了 ``default_factory``,那麼預設工廠函式將始終從生成的 :meth:`__init__ 中呼叫" +"`功能。發生這種情況是因為沒有其他方法可以為該欄位賦予初始值。" #: ../../library/dataclasses.rst:689 +#, fuzzy msgid "Mutable default values" -msgstr "" +msgstr "可變預設值" #: ../../library/dataclasses.rst:691 +#, fuzzy msgid "" "Python stores default member variable values in class attributes. Consider " "this example, not using dataclasses::" -msgstr "" +msgstr "Python 將預設成員變數值存儲在類屬性中。考慮這個例子,不使用資料類::" #: ../../library/dataclasses.rst:706 +#, fuzzy msgid "" "Note that the two instances of class ``C`` share the same class variable " "``x``, as expected." -msgstr "" +msgstr "請注意,類“C”的兩個實例共享同一個類變數“x”,正如預期的那樣。" #: ../../library/dataclasses.rst:709 +#, fuzzy msgid "Using dataclasses, *if* this code was valid::" -msgstr "" +msgstr "使用資料類,*如果*此程式碼有效::" #: ../../library/dataclasses.rst:717 msgid "it would generate code similar to::" -msgstr "" +msgstr "它會生成類似的程式碼::" #: ../../library/dataclasses.rst:728 +#, fuzzy msgid "" "This has the same issue as the original example using class ``C``. That is, " "two instances of class ``D`` that do not specify a value for ``x`` when " @@ -782,45 +1098,66 @@ msgid "" "is that if a value is unhashable, it is mutable. This is a partial " "solution, but it does protect against many common errors." msgstr "" +"這與使用類“C”的原始示例存在相同的問題。也就是說,類“D”的兩個實例在建立類實例" +"時沒有為“x”指定值,它們將共享“x”的同一個副本。因為資料類只是使用普通的 " +"Python 類建立,所以它們也有這種行為。資料類沒有通用的方法來檢測這種情況。相" +"反,如果 :func:`dataclass` 裝飾器檢測到不可散列的預設參數,它將引發 :exc:" +"`TypeError`。假設是如果一個值是不可散列的,那麼它就是可變的。這是一個部分解決" +"方案,但它確實可以防止許多常見錯誤。" #: ../../library/dataclasses.rst:739 +#, fuzzy msgid "" "Using default factory functions is a way to create new instances of mutable " "types as default values for fields::" -msgstr "" +msgstr "使用預設工廠函式是一種建立可變型別的新實例作為欄位預設值的方法:" #: ../../library/dataclasses.rst:748 +#, fuzzy msgid "" "Instead of looking for and disallowing objects of type ``list``, ``dict``, " "or ``set``, unhashable objects are now not allowed as default values. " "Unhashability is used to approximate mutability." msgstr "" +"不再查找和禁止型別為“list”、“dict”或“set”的物件,現在不允許使用不可散列的對像" +"作為預設值。不可散列性用於近似可變性。" #: ../../library/dataclasses.rst:755 +#, fuzzy msgid "Descriptor-typed fields" -msgstr "" +msgstr "描述器型別的欄位" #: ../../library/dataclasses.rst:757 +#, fuzzy msgid "" "Fields that are assigned :ref:`descriptor objects ` as their " "default value have the following special behaviors:" msgstr "" +"指定為 :ref:`descriptor objects ` 作為預設值的欄位具有以下特殊行" +"為:" #: ../../library/dataclasses.rst:760 +#, fuzzy msgid "" "The value for the field passed to the dataclass's ``__init__`` method is " "passed to the descriptor's ``__set__`` method rather than overwriting the " "descriptor object." msgstr "" +"傳遞給資料類的“__init__”方法的欄位值被傳遞給描述器的“__set__”方法,而不是覆蓋" +"描述器物件。" #: ../../library/dataclasses.rst:763 +#, fuzzy msgid "" "Similarly, when getting or setting the field, the descriptor's ``__get__`` " "or ``__set__`` method is called rather than returning or overwriting the " "descriptor object." msgstr "" +"同樣,在獲取或設定欄位時,將呼叫描述器的“__get__”或“__set__”方法,而不是回傳" +"或覆蓋描述器物件。" #: ../../library/dataclasses.rst:766 +#, fuzzy msgid "" "To determine whether a field contains a default value, ``dataclasses`` will " "call the descriptor's ``__get__`` method using its class access form (i.e. " @@ -829,10 +1166,17 @@ msgid "" "hand, if the descriptor raises :exc:`AttributeError` in this situation, no " "default value will be provided for the field." msgstr "" +"為了確定一個欄位是否包含預設值,``dataclasses`` 將使用其類訪問形式呼叫描述器" +"的``__get__`` 方法(即``descriptor.__get__(obj=None, type=cls)``。如果在這種" +"情況下,描述器回傳一個值,它將用作欄位的預設值。另一方面,如果描述器在這種情" +"況下引發 :exc:`AttributeError`,則不會為該欄位提供預設值。" #: ../../library/dataclasses.rst:801 +#, fuzzy msgid "" "Note that if a field is annotated with a descriptor type, but is not " "assigned a descriptor object as its default value, the field will act like a " "normal field." msgstr "" +"請注意,如果一個欄位用描述器型別註釋,但未分配描述器對像作為其預設值,則該欄" +"位將像普通欄位一樣工作。"