Python id() Function

Python’s built-in 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

ArgumentobjectA Python object for which the identity should be returned.
Return ValueintAn integer number associated to the object that is static and unique.
Python id() Function - Explanation
Input : id('42')
Output : 3033433182136

Input : id(42)
Output : 140725259659376

Input : id(43)
Output : 140725259659408

Input : id(3.41)
Output : 3033419857088

Input : id([1, 2, 3])
Output : 3033433284680

Input : id([1, 2, 3])
Output : 3033433132552

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).

Link: https://nostarch.com/pythononeliners

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.

Summary

Python’s built-in 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.

Join the free webinar now!