30.6. dataclasses — Data Classes¶
Source code: Lib/dataclasses.py
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:
@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 版新加入.
30.6.1. Module-level decorators, classes, and functions¶
-
@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)¶ This function is a decorator that is used to add generated special methods to classes, as described below.
The
dataclass()decorator examines the class to findfields. Afieldis defined as class variable that has a type annotation. With two exceptions described below, nothing indataclass()examines 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.
The
dataclass()decorator will add various 「dunder」 methods to the class, described below. If any of the added methods already exist on the class, the behavior depends on the parameter, as documented below. The decorator returns the same class that is called on; no new class is created.If
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 ofdataclass()are equivalent:@dataclass class C: ... @dataclass() class C: ... @dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False) class C: ...
The parameters to
dataclass()are:init: If true (the default), a__init__()method will be generated.If the class already defines
__init__(), this parameter is ignored.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).If the class already defines
__repr__(), this parameter is ignored.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.If the class already defines
__eq__(), this parameter is ignored.order: If true (the default isFalse),__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. Iforderis true andeqis false, aValueErroris raised.If the class already defines any of
__lt__(),__le__(),__gt__(), or__ge__(), thenTypeErroris raised.unsafe_hash: IfFalse(the default), a__hash__()method is generated according to howeqandfrozenare set.__hash__()is used by built-inhash(), and when objects are added to hashed collections such as dictionaries and sets. Having a__hash__()implies that instances of the class are immutable. Mutability is a complicated property that depends on the programmer’s intent, the existence and behavior of__eq__(), and the values of theeqandfrozenflags in thedataclass()decorator.By default,
dataclass()will not implicitly add a__hash__()method unless it is safe to do so. Neither will it add or change an existing explicitly defined__hash__()method. Setting the class attribute__hash__ = Nonehas a specific meaning to Python, as described in the__hash__()documentation.If
__hash__()is not explicit defined, or if it is set toNone, thendataclass()may add an implicit__hash__()method. Although not recommended, you can forcedataclass()to create a__hash__()method withunsafe_hash=True. This might be the case if your class is logically immutable but can nonetheless be mutated. This is a specialized use case and should be considered carefully.Here are the rules governing implicit creation of a
__hash__()method. Note that you cannot both have an explicit__hash__()method in your dataclass and setunsafe_hash=True; this will result in aTypeError.If
eqandfrozenare both true, by defaultdataclass()will generate a__hash__()method for you. Ifeqis true andfrozenis false,__hash__()will be set toNone, marking it unhashable (which it is, since it is mutable). Ifeqis false,__hash__()will be left untouched meaning the__hash__()method of the superclass will be used (if the superclass isobject, this means it will fall back to id-based hashing).frozen: If true (the default is False), assigning to fields will generate an exception. This emulates read-only frozen instances. If__setattr__()or__delattr__()is defined in the class, thenTypeErroris raised. See the discussion below.
fields may optionally specify a default value, using normal Python syntax:@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
In this example, both
aandbwill be included in the added__init__()method, which will be defined as:def __init__(self, a: int, b: int = 0):
TypeErrorwill be raised if a field without a default value follows a field with a default value. This is true either when this occurs in a single class, or as a result of class inheritance.
-
dataclasses.field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None)¶ For common and simple use cases, no other functionality is required. There are, however, some dataclass features that require additional per-field information. To satisfy this need for additional information, you can replace the default field value with a call to the provided
field()function. For example:@dataclass class C: mylist: List[int] = field(default_factory=list) c = C() c.mylist += [1, 2, 3]
As shown above, the
MISSINGvalue is a sentinel object used to detect if thedefaultanddefault_factoryparameters are provided. This sentinel is used becauseNoneis a valid value fordefault. No code should directly use theMISSINGvalue.The parameters to
field()are:default: If provided, this will be the default value for this field. This is needed because thefield()call itself replaces the normal position of the default value.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 purposes, this can be used to specify fields with mutable default values, as discussed below. It is an error to specify bothdefaultanddefault_factory.init: If true (the default), this field is included as a parameter to the generated__init__()method.repr: If true (the default), this field is included in the string returned by the generated__repr__()method.compare: If true (the default), this field is included in the generated equality and comparison methods (
