Python Memory Management and Tips Transcripts
Chapter: Python variables and memory
Lecture: Does Python have pointers?
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Alright. Time for some Python code. In a big question, does Python have pointers?
0:06
Well, let's look at a function. The same, SamePerson function but written in Python. So here we have a function: def
0:13
same_person, and we're passing a p1 and a p2, and we're using type annotations to indicate this is a person class.
0:19
So, p1 is a person, p2 is a person indicating also the function returns a bool, true or false, on whether they're actually the same
0:27
person. But notice p1.id == p2.id That's not that arrow thing, right? We don't have to treat it differently. And if you think about Python,
0:38
you've probably never seen the star in the context of meaning this is a reference or a pointer to a thing you've never allocated memory,
0:46
you probably never cleaned it up. There's a del key word, but that means something totally different that doesn't really directly
0:52
affect memory. Are there pointers here? I don't see any pointers. Here's the thing. Let's try to print out p1 and p2 and see what we get.
1:01
Well, the interpreter would just say "__main__" we have a person class object. So person is the class and we've created one of them.
1:09
So object at this memory location. Hmm, at this memory location sounds a little bit like, well what we have the pointers,
1:17
doesn't it? You could also use this function, the cool function in Python a built-in called "id" and say,
1:23
where does this thing, basically, where does it live? Hey, and if those numbers are the same, they're sharing the location. If they're not,
1:31
it's a not shared thing. And we can talk about that. We will talk about that as we go. But if you go out here and actually look in memory,
1:40
we're gonna have two things out on the heap dynamically allocated, And these are going to be pointing to it. Well, p1 and p2
1:47
are pointing to it. Those id of them actually correspond to the address. This is the same situation as we had in C++ pointers.
1:55
The language is hiding it from us. We don't have to worry about it, right? That's cool. But as you think about, you know,
2:02
what is the lifetime of p1 and p2? Who was in charge of it? All those same questions I asked, Come up. Who owns these objects?
2:10
Well, in Python, the answer is better because you don't have to worry about
2:14
it. Like I said, you probably have never really thought about cleaning up memory
2:17
by, like, going free or delete or whatever on some thing you've created.
2:21
Because you can't really do that. But somebody has to, right? If these get created. So the question Who owns it it's kind of, it's interesting,
2:28
It's kind of the community of all the things in the program, all the things that share that piece of data. Once they all stop paying attention to it,
2:37
it goes away. It goes away for one and other reason. There's a couple ways in which it could go away and we'll talk about it.
2:43
But the runtime itself kind of owns of these objects. You don't have to worry about that.
2:48
How long were they stick around? Until everyone is done with them, maybe a little bit longer, depending on how they're linked together.
2:56
But generally speaking, just until everyone is done with it and the runtime also knows
3:02
who's paying attention to it, so you don't have to worry about the time. It is really nice. So in a sense we have pointers in Python,
3:08
yes, but we don't have the syntax of pointers, lovely. Nor do we have all the stuff for in the memory management and the accounting of
3:16
who owns what, when, and when it should be cleaned up. All those things are gone, which is beautiful,
3:20
but we also need to understand how and when Python does those things on our behalf right? Does Python have pointers?
3:27
I'm going to say yes, Python has pointers, but you don't syntactically have to worry about it.