Python Memory Management and Tips Transcripts
Chapter: Python variables and memory
Lecture: The id() function
0:00 Let's real quickly look at the documentation from Python's docs around this id built-in
0:08 function, and it says "id is a function that takes a single object". And if you look at the description,
0:13 it says "it will return the identity of the object. This is gonna be a number which is guaranteed to be unique and constant during the objects
0:21 Lifetime, however, two objects may with non-overlapping lifetimes may have the same id value". That's more likely than just random places in memory.
0:31 As you'll see, there's patterns that have a tendency to reuse memory rather than allocate new memory. Nonetheless, this while the objects are around,
0:40 what this number come back means this is the unique identity of the object. And if you get two objects and you ask from them and they're
0:47 the same, they're literally the same object in the runtime. If those numbers are different, even if they would test for equality like two strings
0:55 might be equal equal to each other but they're not the same location of memory,
0:59 potentially, then the id would come back to be different. And, little special detail here at the end, if you happen to be in CPython,
1:08 The CPython implementation detail is that this is the memory address, the address of the object in memory.
1:14 So this literally is the base 10 version of "Where's This Thing in Memory?"
1:20 Id is simple, but we're gonna be using it a couple of times throughout this course to ask questions like "are these actually the same thing?"
1:26 or "where do they live in memory?" and stuff like that. So, here's the deal.