What are data classes for?
When coding it is extremely common to find that you have a bunch of data that all is associated with a single thing and it would make sense to store all of that together. For example, a coordinate. A cartesian coordinate in 3D space consists of anx position, a y position and a z position. When working with lots of geometries you may often be working with lots of coordinates. Thousands, or even millions. You could store these in lists, or in tuples, but that would be harder to read.
Building a data class
dataclasses is a built-in Python library introduced in version 3.7, designed to assist users with class-creation. We need the dataclass decorator from the library to do what we need to.
Coordinate and then follow up with a list of class properties. These must follow the same rules as variables and should follow the same typing conventions as well, that is snake_case_naming. When creating a dataclass, however you need to specify the expected types of each property in the class definition. In this case each property will be an integer.
And that’s it! We can now use this class and access properties on it.
= syntax you are used to in functions. For example, if we wanted to add a time property to coordinate and have it default to 0 for now, our new class would look like this.
0. And that’s really all you need to know. Creating and using data classes is simple and rewarding.