dataclasses --- Data Classes¶
This module provides a decorator and functions for automatically
adding generated special methods such as __init__() and
__repr__() to user-defined classes. It was originally described
in PEP 557.
The member variables to use in these generated methods are defined using PEP 526 type annotations. For example, this code:
from dataclasses import dataclass
@dataclass
class InventoryItem:
"""Class for keeping track of an item in inventory."""
name: str
unit_price: float
quantity_on_hand: int = 0
def total_cost(self) -> float:
return self.unit_price * self.quantity_on_hand
will add, among other things, a __init__() that looks like:
def __init__(self, name: str, unit_price: float, quantity_on_hand: int = 0):
self.name = name
self.unit_price = unit_price
self.quantity_on_hand = quantity_on_hand
Note that this method is automatically added to the class: it is not
directly specified in the InventoryItem definition shown above.
在 3.7 版被加入.
模組內容¶
- @dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)¶
This function is a decorator that is used to add generated special methods to classes, as described below.
The
@dataclassdecorator examines the class to findfields. Afieldis defined as a class variable that has a type annotation. With two exceptions described below, nothing in@dataclassexamines the type specified in the variable annotation.The order of the fields in all of the generated methods is the order in which they appear in the class definition.
如下所述,
@dataclass裝飾器會向類別新增各種 "dunder" 方法。如果類別中已存在任何新增的方法,則行為會取決於參數,如下方文件所述。裝飾器會回傳呼叫它的同一個類別;不會建立新類別。If
@dataclassis 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@dataclassare equivalent:@dataclass class C: ... @dataclass() class C: ... @dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False) class C: ...
@dataclass的參數是:init:如果為真(預設值),將生成一個
__init__()方法。如果該類別已經定義了
__init__(),則此參數會被忽略。repr: If true (the default), a
__repr__()method will be generated. The generated repr string will have the class name and the name and repr of each field, in the order they are defined in the class. Fields that are marked as being excluded from the repr are not included. For example:InventoryItem(name='widget', unit_price=3.0, quantity_on_hand=10).如果該類別已經定義了
__repr__(),則此參數會被忽略。eq: If true (the default), an
__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.如果該類別已經定義了
__eq__(),則此參數會被忽略。order: If true (the default is
False),__lt__(),__le__(),__gt__(), and__ge__()methods will be generated. These compare the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type. If order is true and eq is false, aValueErroris raised.如果該類別已經定義了
__lt__()、