Get startedGet started for free

Dataclasses

1. Dataclasses

Another common way to represent complex data would be using a python dataclass. I like to think of dataclass as a more powerful namedtuple.

2. Why use dataclasses

In addition to being usable like a namedtuple, dataclasses offer many other benefits. You can set default values for particular fields to ensure that each time you use a dataclass those fields are preset. Dataclasses also provide a nice default representation for print, log and other outputs. If you need to convert your dataclass to a dictionary or a tuple dataclasses have functions that can perform that application for you. You can also make custom properties that do more than just store a value. It's also possible to make frozen instances of a dataclass that doesn't allow any edits to the properties after the dataclass has been created. These are just a few benefits of using a dataclass, and the python documentation can supply you with even more.

3. Looking at our first dataclass

Lets take a look at how to make a dataclass. You first import dataclass from the dataclasses module. Next you use the @ symbol and the word dataclass to create a decorator for the class you are about to make. A decorator is a wrapper around something code in python that adds extra behaviors. Now we define our class name and field names with their types and any default values. For example we're using the name as a string and the quantity as an integer with a default value of zero.

4. Easy tuple or a dictionary conversion

The dataclasses module also has functions like asdict() and astuple() that can automatically convert your dataclasses into useful dictionaries and tuples. For example, I can import asdict and astuple from the dataclasses module, and using the same dataclass we created previously I can make a ginger molasses cookie. Then I can use those two functions to convert it to a dict and a tuple.

5. Custom properties

While it's more common to define methods on standard python classes, we often will want to define our own custom properties on dataclasses that perform some function to our dataclasses property's values and return the result of that operation. Python has a built in decorator called @ property that makes that work as any other property would. Suppose we expand our cookie dataclass to have a new property called cost, a decimal. In that case, we can add a new custom property by using the @property decorator on the value_of_goods method, which always gets itself as an argument, and then we use self-dot-cost multiplied by self-dot-quantity to get the value of our cookie.

6. Using custom properties

We can use the hybrid property just like any other property on our dataclass. For example, I'm creating a new peanut cookie and getting the value_of_goods property. Notice methods decorated with the @property decorator do not need me to add parentheses onto them to call them appropriately.

7. Frozen instances

A frozen instance of a dataclass can not be edited one it has been created. To make a dataclass that creates frozen instances, we pass the frozen equals True argument to our dataclass decorator. Then once we make an instance of our dataclass like the c cookie we made here it will be frozen and we can't update it. For example, if we try to update the quantity to 15, it will give us an error saying we can't assign to field quantity.

8. Let's practice!

This is just a glimpse of the full power of dataclasses, let's go put what we've learned here to use.