Python Memory Management and Tips Transcripts
Chapter: Course conclusion and review
0:00 We saw a lot of times when we create classes were gonna compute some information based
0:05 on the data provided to it.
0:07 Like in this example here we have an A and a B,
0:09 but we also want people to be able to access C and D,
0:12 which is the ratio and the multiplier of those two things.
0:17 So to make that super easy for them, we're just going to say "self.a
0:20 equals A, and B equals B",
0:22 and then we're going to compute those here so later they could create a thing
0:24 and say "thing.c or thing.d" and get that information right away.
0:29 Well, we've got to store
0:31 all of those four things in memory,
0:33 but if we found some other way to provide C and D in the same syntax for
0:39 the consumer, we would save potentially half the memory.
0:43 And guess what? That's properties.
0:45 I'm sure you know about properties,
0:46 but it's easy to forget their relevance in the context of memory,
0:51 right? So here we're only storing A and B and not those other two variables
0:54 and the cost for having the property is a one time thing,
0:58 right? Defines the code on the type object.
1:00 It's not an instance for every time we make one of these,
1:03 so it's a huge efficiency gain.
1:05 Also, if you were going to create one of these and we might say "use
1:08 property C but not property D" it also could be faster because we're not doing
1:12 that computation for D every time we create one,
1:15 even in the cases where we're not using it.
1:18 Another thing that we compare together with properties,
1:20 actually, is this idea of slots.
1:23 Python objects in general are dynamic objects that can grow, and things can be
1:28 added to them, and new fields can be created or fields can even be taken
1:31 away, all sorts of funky stuff.
1:33 Most of the time though, people just treat them as regular old objects
1:38 doing traditional object-oriented programming where that is not the thing that happens.
1:43 If that's the case, we'll see that we can use something called "slots", and with
1:48 slots, we just name the variables, name the fields,
1:51 and it turns out that this is much,
1:54 much more efficient in terms of memory.
1:55 Remember, we don't create that one-off dictionary
1:58 every time we create the class,
2:00 we just have a space for memory where the variables go, so
2:03 that's faster. Down here, we create a regular thing, we can say "r.b"
2:07 which gives us 2
2:08 in this case. We can also add on "r.e", right?
2:12 This that dynamic stuff. Not sure it's a great idea,
2:15 but technically it works. If you adopt slots,
2:18 you get the same regular behavior,
2:20 but you can't add on, you can do anything dynamic.
2:23 So like, "s.e" is actually an error, "AttributeError: 'SlotThing'
2:27 object has no attribute 'e'".
2:29 So you give up this dynamic nature,
2:32 But usually, like I said,
2:33 you're not using it, so this is the really good trade off in terms of
2:37 making your code much, much faster.