Function ID in Python

Free Video Tutorial and Guide

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

photo of Noble Desktop

Noble Desktop

At Noble Desktop you can learn how to code websites, build iOS apps, make graphics, and more. From our front-end coding bootcamp to Photoshop classes, we offer a variety of comprehensive day, evening, and weekend training classes as well as certificate programs in web design, web development, HTML email, and more. Courses are available in-person (at our training facility in Soho), live online, and customized corporate or private training. Our course materials and workbooks are used by colleges and schools worldwide. Learn a skill today and start using it tomorrow.

More articles by Noble Desktop

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram