Python Memory Management and Tips Transcripts
Chapter: Course conclusion and review
Lecture: Classes

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


Talk Python's Mastodon Michael Kennedy's Mastodon