1

This is what it says in the document:

This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.

Accordingly, I can use dataclass in every class, but should I really use ?

I really don't understand when should I use it, I'm curious about your ideas.

ruohola
  • 21,987
  • 6
  • 62
  • 97

1 Answers1

1

To me, dataclasses are best for simple objects (sometimes called value objects) that have no logic to them, just data. For example:

@dataclass
class StockItem:
    sku: str
    name: str
    quantity: int

This then benefits from not having to implement init, which is nice because it would be trivial. It also means, with repr, you can print the object to the shell and copy-paste it to get another identical instance of the object, which is handy.

PirateNinjas
  • 1,908
  • 1
  • 16
  • 21