Dataclass

noun

say it: “DAY-tuh-klas” /ˈdeɪtəˌklæs/

A Python class made mainly to hold data: the @dataclass decorator writes __init__, printing and comparing for you from the fields you list.

What is a dataclass?

A dataclass is a class mainly for holding data. The @dataclass decorator writes __init__, printing and comparing for you from the fields you list.

from dataclasses import dataclass

@dataclass
class Product:
    name: str
    price: float

lamp = Product("Lamp", 25.0)
print(lamp)  # Product(name='Lamp', price=25.0)

In a sentence

  • “Printing a dataclass shows its fields, such as Product(name='Lamp', price=25.0).”
  • “Product is a dataclass with a name and a price.”

Level: Freelancer · More Python terms at this level

Updated