In Python, id(obj) returns an integer that is unique to that object while it exists. Two names refer to the same object exactly when their ids are equal, which is what the is operator checks: a is b means id(a) == id(b).
The actual number is an implementation detail (in CPython it is the memory address), it changes from run to run, and an id can be reused after an object is deleted. So compare ids, but never store or print them as meaningful data.
id() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass any object.
python
identity = id(obj)
same = id(a) == id(b) # same as: a is b
Call
What it does
id(a) == id(b)
Same object? Equivalent to a is b.
a is b
The idiomatic identity test.
a == b
Equal values, possibly different objects.
x is None
The standard way to test for None.
The examples compare ids instead of printing them, because the numbers differ on every run.
Library declarations
python
id(object, /)
Parameters
Parameter
Description
object
Required. Any object.
Return Value
Return Value
Description
int
An integer identity, constant and unique during the object's lifetime.
Python Examples
Example 1 — Same Object or Just Equal?
Compare an alias and an equal copy with ids, is, and ==.
python
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(id(a) == id(b), a is b)
print(id(a) == id(c), a is c)
print(a == c)
Output
True True
False False
True
How It Works
An alias.b = a creates a second name, not a new list, so the ids match.
Same as is.id(a) == id(b) and a is b always agree.
Equal but separate.c is a different list with the same items.
Value comparison.== compares contents, so a == c is True.
Example 2 — Mutation Keeps the id, Rebinding Changes It
Track a list's identity through append() and +, and a tuple through +=.
In place.append() changes the existing list, so its identity stays the same.
New object.items + [4] builds a new list and the name now points to it.
Immutable +=. Tuples cannot change, so += creates a new tuple with a new identity.
Names vs objects. Assignment moves a name to an object; it never copies.
Example 3 — Shallow Copies, Deep Copies, and is None
Use identity to see what a shallow copy shares, and test for None the standard way.
python
import copy
original = {"tags": ["a"]}
shallow = copy.copy(original)
deep = copy.deepcopy(original)
print(shallow is original, shallow["tags"] is original["tags"])
print(deep["tags"] is original["tags"])
value = None
print(value is None, id(value) == id(None))
Output
False True
False
True True
How It Works
New outer dict.copy.copy() makes a different dict object.
Shared inner list. The shallow copy reuses the same "tags" list.
Fully separate.deepcopy() copies the inner list too, so the identities differ.
One None. There is exactly one None object, which is why is None is the right test.
Frequently Asked Questions
An integer that uniquely identifies an object while it exists. In CPython it is the memory address, but that is an implementation detail.
is checks identity (same object, same id). == checks whether the values are equal.
Objects are placed in memory differently each time. Only compare ids within one run.
Not at the same time. An id can be reused after the first object is garbage collected.
CPython caches some small ints and strings. Do not rely on it; use == for values.