🔵 🔵 🔵


Primary

၊၊||၊|။

dataclasses ⚬⃕ᵖʸ|Documentation|1st|20251021004058-00-⌔

dataclasses — Data Classes — Python 3 documentation

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:

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.

Added in version 3.7.

Printed 2026-06-28.

(echo:: @ )

Link to original

Secondary

• • •