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