- Data classes were introduced with Python 3.7, and they’re a class that typically contain mainly data, although there aren’t really any restrictions. They provide a convenient way to define classes to store data and allow value retrieval via attribute lookup.
- The class import statement is::
from dataclasses import dataclass
- The decorator for the class is::
@dataclass
- No constructor is defined, so it uses a default constructor and knows where to put the values for the properties positionally.
- The property attribute declaration is done with data types, and they look like class attributes but are being treated by the “default” constructor like instance property attributes::
from dataclasses import dataclass
@dataclass
class DataClass:
name: str
value: int
- The code below runs in the REPL using value retrieval via attribute lookup::
test = DataClass("hello world", 1234)
test.name #allow value retrieval via attribute lookup as stated above
test.value #allow value retrieval via attribute lookup as stated above

- Data classes are seen to provide and do provide conveniences for classes not provided before Python 3.7.


- A data class comes with basic functionality already implemented. For instance, you can instantiate, print, and compare data class instances straight out of the box.

- Behind the scenes, dataclasses implement a repr dunder method (Representation) and an eq dunder method for simple comparison, and what is meant is that the eq dunder method checks for attribute value equality.

- As you can see, this means extra work needs to be done to allow regular classes to give you what data classes do by default. In the next section, you’ll look at other alternatives, some of which you may have already encountered.