search

What is the use of the "id" function in Python?

The "id" function in Python is used to return a unique identifier for an object. The identifier is an integer that is guaranteed to be unique and constant for this object during its lifetime. The id is also system-dependent, meaning that objects that have the same id on one system might have different ids on another.

The "id" function can be useful for debugging and for testing the identity of objects in comparison operations.

Here's an example of how to use the "id" function:

>>> x = 10
>>> id(x)
140714200792768
>>> y = 10
>>> id(y)
140714200792768
>>> z = 20
>>> id(z)
140714200793008

As you can see, **x** and **y** have the same id even though they refer to different objects, because they have the same value. On the other hand, **z** has a different id.

Related Questions You Might Be Interested