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 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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn 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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.