Python Data Class

Python Data Class

Part of what classes are used for in Python is to represent data. It is by using the __init__ function that we can accept data into an object as it is created, and assign those data values to the object using the self keyword. You might notice that sometimes this code setup has a lot of boilerplate to it. In other words, you have to write the same structure of code to simply store simple values on an object. It can seem verbose sometimes. This is where Python Data Classes come in. Beginning in Python version 3.7, you can use a new syntax to create classes for holding data in your application and that is what we will examine in this tutorial.


Data Class Definition

Recall the usual way we define a class in Python using __init__ and super().

Python Crash Course by Eric Matthes, costs 23.99
Serious Python by Julien Danjou, costs 25.43
Python Crash Course by Eric Matthes, costs 23.99
title=Serious Python,author=Julien Danjou,price=25.43

Now let’s see it using the Data Class approach. To use dataclasses, you must import them using from dataclasses import dataclass. We then need to decorate the class with the @dataclass decorator. The code below has less boilerplate, yet we still get the output we expect.

Python Crash Course by Eric Matthes, costs 23.99
Serious Python by Julien Danjou, costs 25.43
Python Crash Course by Eric Matthes, costs 23.99
title=Serious Python,author=Julien Danjou,price=25.43

Post Initialization

If you decide to use Data Classes, you have access to a __post_init__ method that can perform additional object initialization since the data class automatically handled the init function like magic. The data class decorator provides a special function named __post_init__ that you can override and that is called after the built-in init function has finished. Here is an example of that in action.

Python Crash Course by Eric Matthes, 544 pages
Serious Python by Julien Danjou, 225 pages

Default Values

Part of using classes in Python is making use of the ability to set default values on objects at run time. It seems this might be missing with data classes since the data class handles the __init__ method automagically for you. You can still make use of default values, but the syntax is a bit different. Here is an example.

Book(title='Empty Book', author='Your Imagination', pages=0, price=0.0)
Book(title='Python Crash Course', author='Eric Matthes', pages=544, price=23.99)
Book(title='Serious Python', author='Julien Danjou', pages=225, price=25.43)

Immutable Data Class

The last thing we’ll look at in this data class tutorial is how to make objects that are immutable. This is done by simply passing frozen=True to the @dataclass decorator.

Python Crash Course
Julien Danjou
Book(title='Python Crash Course', author='Eric Matthes', pages=544, price=23.99)
False
Traceback (most recent call last):
  File "C:/python/OOP/dataclass.py", line 33, in 
    book1.title = 'Python for Kids'
  File "", line 3, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'title'