id(object) function takes a Python object as an input and returns the identity of an object that is a static, unique integer. The identity is static, it never changes throughout the program’s execution, and unique, no other object has the same identity. It is implemented in
cPython by returning the address of the object in memory.
>>> x = [1, 2, 3] >>> id(x) 2205705788488 >>> id(x) == id([1, 2, 3]) False
Python id() Video
Python id() Syntax and Examples
|Argument||A Python object for which the identity should be returned.|
|Return Value||An integer number associated to the object that is static and unique.|
id([1, 2, 3])
id([1, 2, 3])
You can see in the last two examples that if you create two identical list objects, the id is still different because they are different objects in memory.
Python id() to Check Same Object Reference
A frequent use of the id() function is to check whether two or more variables refer to the same object in memory.
a = 5 b = a c = a print(id(a)) print(id(b)) print(id(c))
The output shows that all three names refer to the same object in memory:
140725259658192 140725259658192 140725259658192
You can also see this visualized in this interactive Python memory simulator:
Python id() Example List Objects
If you create multiple list objects with the same elements, they should all have the same unique id, right?
for i in range(10): print(id([1, 2, 3]))
Exercise: Do we print 10 times the same output?
But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
The output of the code snippet is surprising because it shows that the id must only be unique during the lifetime of the object!
2805153590280 2805153590280 2805153590664 2805153590536 2805153590280 2805153590664 2805153590536 2805153590280 2805153590664 2805153590536
For instance, the first two instances have the same identifiers! The reason is that the lifetime of the object is limited—a list is never used after the creation.
id(object) function takes a Python object as an input and returns the identity of an object that is a static, unique integer.
- The identity is static, i.e., it never changes throughout the program’s execution, and
- unique, i.e., no other object has the same identity.
Want to keep improving your Python skills? Check out our free Python cheat sheets:
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.