Python Data Classes – Introduction With Example

python data classes

Why we use class in python? Probably to store data and perform certain operations on it. In the real world, we create objects mainly to store data and not so much of methods (behavior). In the previous section, we learned about the named tuple and how it can be used to store data with readability in mind. This time we will learn about python data classes which have been introduced in python 3.7, this can also be used as an alternative for storing data as named tuple.

What is a Data Class in Python?

The data class is a decorator function implemented in python 3.7.

A Data Class is basically a class that contains only attributes (methods also, if necessary).

Its primary purpose is to decorate the python class and include some functionality that we would otherwise need to override.

You should have basic knowledge about python decorator to understand this concept better.

Why Do We Need Data classes in Python?

Let me introduce you to the problem. Then we will look at the solution that data classes offer.

We are going to create a Student class that will store the student information.


class Student():
    def __init__(self, name, clss, stu_id): = name
        self.cls = clss = stu_id

For the sake of simplicity, we are taking only three attributes.

Let’s create the instance of the class and print it.

student1 = Student('HTD', 10, 17)

>>> print(student1)

<__main__.Student object at 0x000001D02D2F2940>

We instantiated the class with values but when we print the object, the memory location of the object is returned.

There is no information of what type the object is it, or what fields are defined for it.

For classes to give their correct representation, we need to override the __repr__ Method.


    def __repr__(self):
        return f"Student(name={},clss={self.clss},stu_id={})"

After adding this function to the class, when we print the class we get:

student1 = Student('HTD', 10, 17)

>>> print(student1)


This time we get the necessary information regarding the object.

Let’s look at one more problem.

In the real world, many times there is a need to compare the data like comparing two integer or string values. Can we do the same with objects? Let’s take a look at this.

student1 = Student('HTD', 10, 17)
student2 = Student('HTD', 10, 17)

>>> print(student1 == student2)


The above example shows how two objects with same values are different. They are different with respect to the memory reference but their values are the same.

This problem can also be fixed by overriding the __eq__ Method (Magic Method).

    def __eq__(self, other):
        return ( == and self.clss == other.clss and ==

student1 = Student('HTD', 10, 17)
student2 = Student('HTD', 10, 17)

>>> print(student1 == student2)


Now this work fine.

But there is one problem, we have to write these codes for all the classes that we will create and this will be more time consuming if the attributes increase in near future.

Here, dataclass plays an important role to reduce our repetitive work.

Import Data Class

As the python data classes has been introduced in python 3.7, it doesn’t come in the main module. We will have to import it in order to use it.

from dataclasses import dataclass

Python Data Class

Let’s define the class with the help of dataclass decorator @dataclass.

from dataclasses import dataclass

class Student():
    name: str
    clss: int
    stu_id: int

Did you notice something.

  1. @dataclass
  2. variable: datatype

The first on is the decorator syntax, it would not have surprised you much as the second one. Python is a dynamically typed language then why we are using datatypes.

This is called the Type Hints. As the name suggests, it gives hint of the type to the variable.

Let’s now try to print the class and see what’s different in it.

student1 = Student('HTD', 10, 17)

>>> print(student1)

Student(name='HTD', clss=10, stu_id=17)

By using a single @dataclass at the top of the class and the class attributes with type hints, we achieved this, which in earlier case we had to override the __repr__ Method.

This also implements the equality check feature.


student1 = Student('HTD', 10, 17)
student2 = Student('HTD', 10, 17)
student2 = Student('Arpit', 10, 17)

>>> print(student1 == student2)


>>> print(student1 == student3)


The @dataclass decorator decorates the class and makes our work easy, as we don’t have to write these codes by ourselves.

In the Next Article we will take a look at the dataclasses parameters.

Hope You like it!

Also learn about other python tutorial articles like python list comprehensionInner FunctionAnonymous/Lambda FunctionCustom Exception etc.

Learn more about Python Data Classes from Official Documentation.

About Author
5 3 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Scroll to Top