Object Identity


Object Identity

In Python, every object or variable is uniquely identified. The built-in id() function is used to identify the different objects and variables.

Objects in Python

"The Objects are Python's speculation for data. Every data in a Python program is described by objects or by connections between objects."
All the objects have an identity, a value, and a type. The function python id() returns the "identity" of the object. The object identity is an integer that is confirmed to be unique and consistent for this object throughout its lifetime.

Object Identity in python

Once the object's id() has been created in python, it will never change throughout the lifetimes, and you might think of it as the object's address in memory. The object's identity is used to uniquely identify where this object is stored in the computer memory. "The function of id() returns an integer representing its [the object's] identity."
Therefore, the id() function shows us the permanent address of the object in memory.

Some Parameters are:-

Object: It is an object in python whose id is to be returned.

Return

It always returns a different and unique integer number.

  • The two objects with non-overlapping lifetimes may have the same value of id().
  • Python has the id() value of ordinarily used data types like string, integers, tuples, etc.
  • It might be possible that you find the multiple variables referring to the same object and hold the exact value of id() if their values are the same.

Example No.1:

x = ('Knowledge2life 1', 'Knowledge2life 2', 'Knowledge2life 3') y = id(x) print(y)

OUTPUT:

47938335258432 (Random)

Example No.2:

x = ('website', 'website 2 ') y = id(x) print(y)

OUTPUT:

47917836558144 (Random)