3.7 - Seeing Data Classes in Practice
00:00 The previous lesson covered Python’s 3.5 and 3.6. This lesson is about Python 3.7 and you’ll see data classes in practice. Welcome to Python 3.7 in 2018. Here a great new introduction: data classes, which I don’t know, this is one of my favorite features.
00:20 They provide the functionality for creating and working with user-defined data classes. These types of classes are mainly used to store data as attributes or fields and can provide a lot more functionality.
00:32
Continuing building on the functionality of asyncio
, async
and await
become keywords. We mentioned before the OrderedDict
.
00:41
Well, in Python 3.7 dictionary order is actually guaranteed for the standard dict
. And to improve debugging across your programs, the breakpoint()
function was added.
00:54 Alright, once again, I’ll explain something by showing you the contrast. So let me start with a simple class intent on keeping some associated data together.
01:09 My initializer, which takes a few arguments.
01:15 Then I store the name and store the height away for later use. This is a pretty common pattern and it’s kind of repetitive. For each attribute, you need an argument and a line of code to store it away.
01:30 Let’s instantiate this and there’s fido and of course you can get at its attribute directly. Data classes got invented because all that boilerplate code is annoying.
01:43 You declare a data class by wrapping it in a decorator.
01:49 So first you have to import it, then wrap the class.
01:56 And instead of mucking with the initializer, you directly declare class attributes.
02:02 You also declare what type they are, giving you more information about the attribute than in the case above.
02:09 And you instantiate this like anything else.
02:16 And like before you can get at its attributes. I’m not gonna get into the details here, but you can also provide default values, but they’re attached to the declaration of the attribute.
02:26
And since this is just a class, you can still add methods and do other class-like things. You can override the .__init__()
method if you want, but it’s best if you don’t as it’s kind of taking care of all this initialization stuff.
02:38
If you want to add other things afterwards, there’s a .__post_init__()
method you can use that gets called once .__init__()
is done.
02:46 Data classes don’t solve all the problems. And there’s plenty of code out there like Pydantic or ORM class libraries like Django and SQLAlchemy that aren’t quite compatible.
02:56 But if you just need a class to store some things, this saves you some of that boilerplate. I was slow to adopt these, but I think part of it is because I spent so much time in the Django world, there were just too many edge cases.
03:07 But I’m finding I’m using them more and more in my classes now. I’ve gotten used to it. My discovery of that post-init hook was also one of the things, was also one of the things, because I found about half the time I wrote a data class, I ended up having to undo the data class part and change the initializer anyways.
03:23 But with that post-init hook, you can get away with a lot of the other things without having to muck with it. I saw pretty early on, again, when I started at Real Python, this is one of the tutorials that Geir Arne had written because he was very, very excited about it.
03:36 Of course, he’s coming from the data science world and he was very interested in using them and many, many of his pieces on Real Python included them. So that’s how I got really introduced to them and, and saw them in use and said, oh yeah, this is a very helpful tool for managing sort of more complex objects without having to like completely build out classes for everything.
04:00 I would take this a level further. I think I’m almost at the point where I think data class should become a key word. Yeah. Because these are becoming so common that having to import a library to do the thing that you’re always doing, this is starting to smell like the bracket object thing, right?
04:15 Hey, we’ve got this better thing, but you’ve gotta jump through hoops to get to get access to it. Don’t forget to import. That’s right. Yeah. Yeah.
04:24
And as you might’ve guessed, there’s a course in tutorial on data classes. If you want to learn more, to see the breakpoint()
function in action or to learn more about debugging, this course and tutorial might be good for you.
04:35 And finally, Python 3.7 is when Real Python started writing content specific to each release. So this article runs down everything in 3.7 with examples along the way.
04:47 Next up, Python 3.8 and a shift in how Python is managed due to the walrus operator.
Become a Member to join the conversation.
molecule61 on Sept. 6, 2025
No mention of namedtuples as a precursor to dataclasses, or the third-party attrs module, that is dataclass’s more direct precursor?