Python Memory Management and Tips Transcripts
Chapter: Memory and classes
Lecture: Delayed fields with properties

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's think about some of these things that we're working with here.
0:02 So, like full_name, we're storing "Sarah space Sampson" in memory for this p object,
0:10 and we're also storing Sarah, and we're storing
0:12 Sampson. Wouldn't it be nice if we didn't have to store all that extra info?
0:16 Same thing for age in years and yearly income and so on. So we can go
0:21 to our person object here, and we can change it a little bit.
0:25 Let's make a not so naive, call this "PersonEfficient" like that,
0:32 and it's going to take the same inbound data,
0:34 but instead of doing all of this,
0:36 every one of these is gonna become a property.
0:39 You're probably familiar with the property, but they are functions that you put on the class
0:43 but they actually, from the consumption side, look like fields.
0:46 So the important thing is this bit of code of nicely, just saying "p.full_name"
0:52 will not change, but how we store this stuff will.
0:55 So let's just get cracking on here.
0:58 Say "self.full_name" and in PyCharm we type "prop" and hit tab,
1:02 and it'll write much of this for you.
1:04 And then we want to go down here and just say "return" now we gotta
1:08 say "self.first and
1:10 self.last", and so on. And then what's this
1:14 next one we want? Age in years? Alright,
1:16 here we go. Prop, tab,
1:18 boom! Take all this. Instead of storing it, we're going to return it and
1:25 this will be a "self.birthdate". Perfect.
1:29 We could even go to these and tell them that this is a string,
1:33 this is an int, so that when we work with it,
1:36 it's gonna be a little bit nicer.
1:38 What do we need, yearly income? Again, prop,
1:41 tab, paste, and we come down here
1:43 and we just return that, and we need a self.
1:47 Final one, years to retire. And there we have it.
1:58 Let's do a little cleanup. So these things were only going to do that computation
2:03 when somebody asked for the years to retire or when they ask age in years or if
2:07 they ask for the full name.
2:08 If you don't ask for that,
2:10 we don't even run the code to create it.
2:13 We also don't have to worry about the memory.
2:15 Now, on the flip side,
2:16 if this is something you would create one and then ask for this 100 times you
2:22 might create some kind of, like, cached memory of,
2:24 like, have I computed age in years?
2:26 yes or no. If no, first compute it and then return it if it was expensive.
2:31 So there's always room for some kind of balance.
2:34 But let's try, use this over here and see how that comes out. Do exactly the
2:40 same thing, but this time I'm gonna say person equals this thing, which
2:45 We gotta import. Again, this is really weird,
2:48 you would not normally do that.
2:49 But in this case, for a little demo, we could just,
2:52 you know, swap out the type,
2:54 and have exactly the same code run.
2:57 You think it's gonna be better?
2:58 Think it's gonna be faster? Let's find out.
3:01 So we did it up here,
3:03 and it was, that number was 427 milliseconds and 32 megabytes!
3:09 Look at that! It's much faster.
3:13 basically three times faster, and it used a third less memory.
3:17 Wow wow wow. So that is pretty awesome.
3:20 And as you can see right here,
3:22 the programming model is still the same.
3:24 Hey, Sarah Sampson, let's review your retirement. Your 46 years old.
3:27 You currently make this much a year and all of these are the properties here,
3:30 Okay? So, super, super cool and easy change.
3:34 You're probably familiar with properties, but I just want to make sure that you linked together
3:38 this idea of the property, which is this computer thing often used for validation.
3:43 If you're going to set something,
3:44 you'll check that the variable gets set correctly, or you could use it for, like I
3:48 talked about, cacheing only computed on demand,
3:51 but then store it, and then so on. So there's a lot of functionality behaviors that are interesting
3:55 here. But there's also big memory implications,
3:59 because now we're only storing this data,
4:01 and all of this is just on demand.