Python id() Function

Returns a unique id for the specified object. This is useful to determine whether two values are in fact the same object.

Syntax

Python
id(object)

Parameters

ParameterDescription
object Required. The object for which to return the id

Example

Python
a = 5
b = 5
print(f'Is object a the same as object b: {id(a) == id(b)}')

class Person:
    pass

aPerson = Person()
anotherPerson = Person()

# Assign aPerson to bPerson
bPerson = aPerson

print(f'Is aPerson the same object as bPerson: {id(aPerson) == id(bPerson)}')
print(f'Is aPerson the same object as anotherPerson: {id(aPerson) == id(anotherPerson)}')

Output

Is object a the same as object b: True
Is aPerson the same object as bPerson: True
Is aPerson the same object as anotherPerson: False