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