Python id() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

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
CallWhat it does
id(a) == id(b)Same object? Equivalent to a is b.
a is bThe idiomatic identity test.
a == bEqual values, possibly different objects.
x is NoneThe 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

ParameterDescription
objectRequired. Any object.

Return Value

Return ValueDescription
intAn 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)

How It Works

  1. An alias. b = a creates a second name, not a new list, so the ids match.
  2. Same as is. id(a) == id(b) and a is b always agree.
  3. Equal but separate. c is a different list with the same items.
  4. 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 +=.

python
items = [1, 2]
before = id(items)
items.append(3)
print(id(items) == before)

items = items + [4]
print(id(items) == before)

point = (1, 2)
start = id(point)
point += (3,)
print(id(point) == start, point)

How It Works

  1. In place. append() changes the existing list, so its identity stays the same.
  2. New object. items + [4] builds a new list and the name now points to it.
  3. Immutable +=. Tuples cannot change, so += creates a new tuple with a new identity.
  4. 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))

How It Works

  1. New outer dict. copy.copy() makes a different dict object.
  2. Shared inner list. The shallow copy reuses the same "tags" list.
  3. Fully separate. deepcopy() copies the inner list too, so the identities differ.
  4. 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.

Next: input()

Read a line of text typed by the user.

input() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful