dataclass
dataclasses
Neat way to declare classes, which automatically add special methods such as __init__() and __repr__().
Example
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_handWill automatically add the below __init__():
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_handReference
Full documentation can be found at https://docs.python.org/3/library/dataclasses.html
Last updated