Last modified: Nov 19, 2026

Python id Function and Object Identity

Every object in Python has an identity. That identity is a unique number tied to the object's place in memory. The built-in id function lets you see that number.

This article explains what object identity means. It also shows how the id function works, when to use it, and the common mistakes beginners make.

What Is Object Identity?

In Python, everything is an object. Numbers, strings, lists, functions, and even classes are objects. Each object gets a unique identity when it is created.

The identity stays the same for the whole life of the object. It never changes, even if the object's value changes. Once the object is destroyed, the identity may be reused by a new object.

Think of identity as the object's "address" in memory. The id function returns this address as an integer.

How the id Function Works

The id function takes one argument and returns an integer. That integer is guaranteed to be unique while the object exists.


# Get the identity of a simple object
number = 42
print(id(number))

# Two variables pointing to the same object
same_number = number
print(id(same_number))

9789248
9789248

Both variables point to the same object. So they share the same identity. This is a key idea to remember.

Identity vs Equality

Identity and equality are not the same thing. Identity asks "are these the same object?" Equality asks "do these objects have the same value?"

Python uses the is operator to check identity. It uses == to check equality. The is operator is basically a shortcut for comparing id values.


a = [1, 2, 3]
b = [1, 2, 3]

# Same value, different objects
print(a == b)   # True
print(a is b)   # False
print(id(a) == id(b))  # False

True
False
False

The two lists hold equal values. But they are separate objects in memory. So their identities differ.

This distinction matters a lot. Many beginners confuse is with == and get surprising results.

Small Integer Caching

Python caches small integers from -5 to 256. These cached objects are reused. So two variables holding the same small integer often share identity.


x = 100
y = 100
print(x is y)   # True, cached

p = 1000
q = 1000
print(p is q)   # Often False, not cached

True
False

This behavior is an implementation detail. Do not rely on it in your code. Always use == when you want to compare values.

The same idea applies to other data structures. When you combine iterables, like in this guide on how to use the zip function to combine iterables, you create a new object with a new identity.

Identity of Mutable and Immutable Objects

Immutable objects like strings and tuples cannot be changed. If you "modify" them, Python creates a new object with a new identity.

Mutable objects like lists and dictionaries can change in place. Their identity stays the same even after you change their contents.


# Immutable: new object on change
s = "hello"
print(id(s))
s = s + " world"
print(id(s))  # Different id

# Mutable: same object on change
items = [1, 2]
first_id = id(items)
items.append(3)
print(first_id == id(items))  # True

140234567890
140234567999
True

This is why mutable objects can cause bugs when shared. Two names may point to the same list, and one change affects both.

Using id to Understand References

The id function is a great debugging tool. It shows whether two names refer to the same object.

You can also use it while learning about functions. When you pass an object to a function, Python passes a reference. Inside the function, the identity is the same.


def show_id(data):
    print(id(data))

my_list = [10, 20, 30]
print(id(my_list))
show_id(my_list)

140234567001
140234567001

The identity inside the function matches the identity outside. This confirms that no copy was made.

Understanding references helps with many topics. For example, it explains how arguments are passed and why default arguments can be tricky. The same concept shows up in the discussion of Python *args and **kwargs explained, where references are passed around freely.

Identity and Iteration

Loops and iterators create objects too. Each iterator has its own identity. Generators are a special kind of iterator, and they hold state between calls.

When you work with generators, identity helps you see that the generator object itself is a single object. It is not a list of results. You can learn more in the guide on Python generators and yield explained.


def counter():
    yield 1
    yield 2

gen = counter()
print(id(gen))
print(id(gen))  # Same object both times

140234567222
140234567222

The generator keeps its identity across calls. This is because it is a stateful object, not a fresh result each time.

Common Mistakes with id and is

The most common mistake is using is instead of ==. This works by accident for small integers and some strings. But it fails for larger values and most objects.

Another mistake is expecting id to be stable across runs. The actual number depends on memory layout. It can change every time you run the program.

Never store or compare id values across separate runs. Only compare identities within the same running program.

Also avoid using id as a permanent key. If the object is garbage collected, a new object can reuse the same identity.

When to Use id

The id function is best for learning, debugging, and understanding memory. It is rarely needed in production code.

Good uses include checking if two variables share an object, exploring caching behavior, and teaching how references work.

If you are learning functional tools, identity can help you reason about results. For example, map and filter return new objects, so their identities differ from the input. See Python map function explained for a clear example of this behavior.

Conclusion

Object identity is a core Python concept. The id function gives you a direct view of it. It returns a unique integer for each live object.

Remember the difference between identity and equality. Use is for identity and == for value comparison. Do not rely on small integer caching or fixed id numbers.

With these rules in mind, you can read Python code with more confidence. You will also debug reference bugs much faster.