Learn about the use of Python's built-in function 'ID' to effectively manage memory and ensure data accuracy, especially in the field of data science. This tutorial video by Art, a Python instructor at Noble Desktop, explains how the ID function can help in identifying whether you're working with the same object or not.
Key Insights
- Python's built-in function 'ID' is used to return a unique ID for a specified object, which represents that object's address in Python memory.
- When you use the equals sign (=) to assign a value to a variable, Python first checks if there is an existing object with the same value already in memory. If not, it creates a new object.
- If you assign the same value to another variable, Python uses the same object for memory efficiency, confirmed by the ID function returning the same number for both variables.
- The variables in Python work as pointers, pointing to the same or different objects in memory.
- Reassigning a variable will not create a copy of the object in Python. The only way to create a copy is to use the method 'copy'.
- Using the 'ID' function is especially crucial in data science to prevent accidental alterations to the original data set.
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.