Python 3 reference · Built-in Functions

Python id() Function

Return an object's identity: a unique integer for the object while it exists. In CPython it is the memory address.

Python 3 Since 3.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

id(object)

Parameters

ParameterDescription
object Any object.

Return value

An int, unique among objects alive at the same time.

Examples

Same object or just equal?Try it yourself
a = [1, 2]
b = a          # the same list
c = [1, 2]     # an equal but different list
print(id(a) == id(b), id(a) == id(c))
print(a is b, a is c, a == c)

Output

True False
True False True
Functions receive the same objectTry it yourself
def add_item(items):
    items.append("new")
    return id(items)

shopping = []
print(add_item(shopping) == id(shopping), shopping)

Output

True ['new']

Good to know

Use is to compare identity rather than comparing id() values. Two objects with non-overlapping lifetimes may get the same id().

Version history

VersionChange
Python 3.0Added

Was this page helpful?