In this video, we're going to look at Function ID's in Python
Video Transcription
Hi, my name is Art and I teach Python at Noble Desktop. In this video, I'm going to show you the built-in function ID. It returns a unique ID for the specified object.
Let me show you how it works. Suppose I create the variable X and assign it a value of 7. The point I'm trying to make here is that when you use the equal sign, it will check if there is already an object of 7 in memory. As a matter of fact, no, because we just got started.
But if I do Y equals 7, it will use the same object for memory efficiency. How do I know? I can use the function ID and pass X to it. I'm going to get a number. Then, I'm going to do ID Y, and I'm going to get the same number.
This number is the address of the object 7 in Python memory. Suppose in winter you go to an event and they give you a number. To get your code back, you need to present that number. That's how it works in Python.
Now, the point I'm trying to make here is that you see these numbers match. What it means is that X and Y work like pointers and they point into the same object.
Let me show you something else. Suppose I want to create a copy of seven. If I assign X here and then run this ID on A, you see the number hasn't changed. That's not how you create a copy in Python. The only way to create a copy in Python is to use the method copy.
If I change X plus two, then all the numbers will change except A because it still points to the same object. If I run this ID on Y, you see that they will be totally different objects.
So, the function ID will help you to check if you're working with the same object. It's important because, especially in data science, you don't want to mess up your original dataset.
In Python, variable names work like pointers compared to other languages. Reassignment will not create a copy. Please keep in mind that.
Thank you.