id

Full name
id
Library
Built-in
Syntax

id(object)

Description

The id function returns the "identity" of an object, that is, a constant numeric value that uniquely identifies the object for as long as it is defined.

In Python, anything is an object, so we can get the identity from -even- the id function.

The identity of an object can vary from one execution of the code to another. Some objects always receive the same identity (integers between -5 and 256 inclusive, for example).

Parameters
  • object: object whose identity you want to obtain.
Result

The id function returns an integer.

Examples

We can obtain the identity of the number 18 with the following code:

id(18)

140710254160192

This value will remain the same even after restarting the kernel.

We can obtain the identity of the id function with the following code:

id(id)

2520727620160

If two variables refer to the same object, their identity will be the same:

a = [1, 2]
b = a

dir(a) == dir(b)

True

Working with NumPy arrays, a view of an array receives a different identity than the one received by the array:

import numpy as np

a = np.array([1, 2, 3])
a

array ([1, 2, 3])

b = a[: 1]
b

array([1])

id(a) == id(b)

False

Submitted by admin on Wed, 01/13/2021 - 14:56