Python Memory Management and Tips Transcripts
Chapter: Memory and classes
Lecture: Concept: Class dictionaries
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We're gonna talk about this idea, this concept called "slots",
0:03
which lets us much more efficiently store data associated with a class at the cost of
0:09
some dynamic flexibility. So, standard Python classes we saw look like this. Here
0:15
we've got a thing, we call it a dynamic thing, because we can do the
0:18
dynamic stuff to it, like later on
0:20
add features to it. If we create three of them: d1, d2, d3,
0:24
and they have numbers: 1,2,3,4, 4,5,6,7, 7,8,9,10
0:29
you can see they each have a newly allocated and managed dictionary, and each one of
0:35
those is gonna point over here to,
0:38
you know, it's gonna have its own details,
0:40
Right? So the first one is gonna have "field a", which currently
0:43
has a value of 1, the "field b" has the value 2 and so on.
0:46
But the last one, the "field a" is value 7 and so on.
0:50
And we saw that we can even dynamically add to these objects.
0:53
I don't think that's a super good idea,
0:54
but you can do it. And these dictionaries are there to support that
0:59
amongst other types of things. This is how things work
1:02
unless you take specific actions. This doesn't look super efficient,
1:07
does it, right? Especially the repeating of the keys a,
1:10
b, c and d over and over.
1:12
If I've got a list of 100,000 dynamic things,
1:16
I've got a, b, c, and d as keys stored in this dictionary,
1:19
repeated 100,000 times.
1:22
Maybe we can do something with that to make our code more efficient in terms of memory usage.