Skip to content

Day 27: Python Data Classes

Python Data Classes: Simplifying Data Storage

Python data classes are a modern feature that streamline the creation of classes specifically designed for storing data. Essentially, a class serves as a blueprint for generating objects, and data classes are a specialized type of class tailored for data storage.

For instance, if you're developing a program to record people's names and ages, data classes can be incredibly useful. You could establish a class named 'Person' with two fields: one for the name and another for the age. Consequently, you can generate multiple instances of the 'Person' class, each with a unique name and age.

Implementing Data Classes in Python

from dataclasses import dataclass 

@dataclass 
class Person: 
    name: str 
    age: int 

p1 = Person("John", 30)
print(p1)

In the above code snippet, we import the 'dataclass' decorator from the 'dataclasses' module. We then define a 'Person' class with 'name' and 'age' as its fields. The '@dataclass' decorator above the class definition indicates that 'Person' is a data class. Finally, we create an instance of the 'Person' class and print it.

Learn More

Want to learn more about Python for Machine Learning? Check out the full course HERE.


Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.