Python Memory Management and Tips Transcripts
Chapter: Memory and classes
Lecture: Concept: Properties, a memory-oriented perspective
0:00 so we saw that many times classes can contain computed fields.
0:05 They've got base data, or primary data, in this case a and b,
0:09 but then there might be some other fields we want to provide to our users that
0:13 could be computed, or are derived from, that base
0:17 data. Here, like c is a over B, and d is a times b. Somewhat contrived,
0:21 but you can see some of this could always like,
0:24 c and d could always be recreated from a and b,
0:26 but it's handy. It's easy to just make these all fields so people don't have
0:30 to call functions, they can just say a "thing.d" and it's all good.
0:35 But if we switch this over to properties, from the outside,
0:38 the consumption model feels identical. But from the memory side,
0:41 we're now using half as much memory,
0:43 or maybe more, depending on what kind of objects we were storing there.
0:47 So this is really nice and really easy.
0:50 It also provides slots or spaces in the code to do things like cacheing like only
0:56 computed on demand but then don't recompute it and all kinds of interesting stuff
1:00 along those lines.