Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

A Short Intro to Data Classes

Today, Day 8, we take a slight detour from special methods, although you’ll see that this topic is still related to special methods!

00:00 This week we’re focusing on special methods, but today we’re going to take a slight detour. We’re going to be talking about data classes. Now, data classes.

00:10 I wanted to fit it in somewhere in the course and it seemed appropriate to fit it in here. And I’ll tell you why in a bit. So it is linked to special methods in a certain way.

00:19 Data classes are a relatively new addition to Python, although I say relatively new. They’ve been around for a few years now. So, so they’re quite well established and you’ll see them quite a bit in Python.

00:31 And the purpose of data classes is to provide a data structure where you can decide what properties you want and what data types they have. For example, let’s say you want to create a people and therefore you create a data structure called person.

00:51 And all you want to do is store every person’s first name, last name, and age. And the first name and the last name will be strings and the age will be an integer.

01:03 So you want to create a data structure that can accept those three bits of information. Preferably, you want to be able to use the attributes, first name, last name, and age.

01:13 So you can refer to the fields. And you also would make sure that the first name and last name are always strings. And the age is always an integer. That’s what a data class is designed for and that’s its primary use.

01:26 How do you create data structures in Python? Well, everything is an object in Python, right? You’ve heard me say that before. Therefore, everything has a class.

01:35 So a data class is a class to create this type of structure that gives you control on what items you have and what data types they have.

01:46 Now, a data class, however, is not a lesser version of a class. A data class is a full-fledged class, so it can do whatever

01:56 any normal class can do. So, so what’s the difference? The difference is the data class gives you some shortcuts. And this is where the special methods come in, because often when you define a data class, you don’t need to worry about some of the special methods because it provides default values for you.

02:16 And if you’re happy with the default values that the data class gives you, and you’ll have a look at this in today’s material, then you don’t need to define the various __init__, __str__, __repr__, and many others.

02:31 Now, a data class is a fully fleshed class, so there’s nothing stopping you from adding methods as well, functionality to it. So some people use data classes as a shortcut to create a normal class that they need with data attributes and methods, but without having to define some of the dunder methods.

02:52 Anyway, you’ve got a whole day today to focus on data classes, and as always, we’ll discuss more in our live Q&A sessions on the forum as needed.

You must own this product to join the conversation.