Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Demo: Container sizes, classes
0:00 Okay, Well, lists were interesting. Let's try to actually store them in classes. We're gonna put the classes in a list as well.
0:08 But what if we pair them together? This is really cumbersome to have. One list that has ages and one that has
0:15 the name. You've gotta use an index of find the same person. Well, that's yucky. So, let's go and try something better.
0:21 Let's go over here and we're gonna just maybe make our situation as good as possible.
0:25 So we'll have a "PersonDetail" and, oops, I dind't want call it that one..
0:31 "persondetail" we'll do like that and then we'll have down here a class, like this, and it's gonna have not much going on with it.
0:40 It's gonna have an age, which in an int. Maybe the name should come first, name, str, Pycharm has this cool feature that will add these
0:50 fields here. It still wants me to close that out. Then I'll do it again, like that. So we can just create a bunch of people here and let's say "print
1:03 storing them as classes". So we're gonna do another cool thing here, we're gonna say "I want to create a person detail for name and age",
1:23 and we're gonna use this thing called "zip", which I think we used before. But we're gonna use zip to take these two together and pick them off one at
1:30 a time. So "for n, a in zip(name, ages), like that. Names and ages. So we'll go through them all,
1:41 and then we'll pair them up and we'll use them to create a person detail. And then let's just print just for a second to see this is working.
1:48 Let's print out the top 10 and it's only gonna work if we have some kind of representation. I think we need this one for what we're about to do.
2:00 Then we'll return self.name is self.age Something like that. Here we go. Catherine is 99. Gayomali is 32. Freeman is 21 and so on.
2:15 Alright, well, it looks like we were able to create the people, but now the question is how much memory did that use?
2:22 So let's take something like this and just print it out. size_util.
2:29 get.full.size, people. Now, It's super important that we have that recursive thing here right? Because we're holding people,
2:36 but then the people hold all sorts stuff, and we'll just put out the people_size. What do you think? Gonna take
2:43 more or less than just storing them directly in a list? I'm gonna guess more. As in, well, 10 times more or something like that.
2:53 Close. So we're using 15MB and maybe it's not called ages. Person details, Let's go with that. Oh, it should say class. There we go.
3:07 So it lines up with the tab. Yeah, that's a lot more. Having objects is super, super valuable. They can contain data and behaviors,
3:17 and group them together and are great for thinking about problem solving. And they're great for isolation, data protection.
3:25 They're not great for memory, okay? So if that's your most primary concern, maybe it's not, Maybe it is,
3:31 but we are using 10 times as much memory for basically storing the same thing. Again, they are matched up, which is nice,
3:39 but maybe we could do something better. I don't know. It depends how much memory pressure under,
3:44 but understanding this takes a lot more, and that's good to know.