Dataclasses

A simple way to define classes with many attributes

Created: August 19th 2019
Updated: December 3th 2019
Connect with us on
image
image

Dataclasses are a simple way to define classes with many attributes.

Definitions

Some terms to help you navigate the post

Dataclasses

Dataclasses is a python standard library module for creating dataclasses. Dataclasses are just syntactically simpler class definitions, typically this is used when you have a class that is used to store many attributes to save you from having to type out __init__() and __repr__() methods for your classes.

Consider the example below:

from dataclasses import dataclass # import dataclass method from module

@dataclass # Decorate class to let python know this is a dataclass
class user:
    name: str
    age: int
    birthday: str
    family_names: list[str]
    address: str
    job_title: str
    country_of_birth: str

And the examples equivalent code without using dataclasses:

class user:
    def __init__(self, name, age, birthday, family_names, address, job_title, country_of_birth):
        self.name = name
        self.age = age
        self.birthday = birthday
        self.family_names = family_names
        self.address = address
        self.job_title = job_title
        self.country_of_birth = country_of_birth

    def __repr__(self):
        return f"user(name='{self.name}', age={self.age}, birthday = '{self.birthday}',family_names = '{self.family_names}', address='{self.address}', job_title='{self.job_title}', country_of_birth='{self.country_of_birth}' )"

13 lines vs 8, not to mention the fact that if any of the variables change, or the class is subclassed the __repr__ method will need to be recreated from scratch (including the hardcoded single quotes around strings to indicate their types).

Usage

In this repo you can see the demo code and actually run it by running python <filename>.py or python3 <filename>.py.

Real World Applications

This function is a great time saver in spinning up quick classes, especially for some quick testing.

equivalent_class_demo.py

"""This file is the exact same class defined in dataclass_demo.py
As you can see this class is much longer since you have to implement
both the __init__ and __repr__ methods yourself.
"""
class User:
    def __init__(self, name, age):
        """This class is equivalent to the user dataclass
        defined in dataclass_demo.py
        Attributes
        ----------
        name: str
        The users' name
        age: int
        The users' age
        Methods
        -------
        __repr__(self)
        prints the instance attributes and type; done by default in a dataclass.
        """
        self.name = name
        self.age = age
        def __repr__(self):
        # Notice the type (user) is hardcoded and would need to be changed in all subclasses
        return f"user(name='{self.name}', age={self.age})"

if __name__ == "__main__":
    john_doe = User("John Doe", 21)
    print(john_doe)

dataclass_demo.py

"""This file is meant to demonstrate the usefullness of dataclasses.
dataclasses allow you to create short, simple and useful classes. By
default decorating a class as a dataclass will automatically create a
__init__ and __repr__ method based on the class attributes.
"""
from dataclasses import dataclass # import dataclass method from module

@dataclass # Decorate class to let python know this is a dataclass
class User:
    """This is a demo class of a basic user.
    Attributes
    ----------
    name: str
    The users' name
    age: int
    The users' age"""
    name: str
    age: int

if __name__ == "__main__":
    john_doe = User("John Doe", 21)
    print(john_doe)

Thank you for your support!

Be sure to share the post if it was helpful!