Dataclasses in Python

The dataclass() decorator will add various “dunder” methods to the class

The parameters to dataclass() are:

  • init: If true (the default), a __init__() method will be generated. If the class already defines __init__(), this parameter is ignored.


the repr() function is used to get a printable string representation of an object

  • repr: If true (the default), a __repr__() method will be generated. The generated repr string will have the class name and the name and repr of each field, in the order they are defined in the class.

repr can be passed to eval() whereas str can not.

Although repr() and str() both return a similar-looking string, the style of the content within the string is different.

Evaluates to False

Using the dataclass with a function

from dataclasses import dataclass

Previous article

Google Cloud Platform

Next article

Python ctypes