Python id() function

Python has many datatypes. Some of them are strings, integers, lists, sets, and dictionaries. When using a function, some of them require certain arguments within the parenthesis. These arguments are usually objects of the above data-types. Here we will see Python id() function.

id() function in Python

Python’s id() is an in-built function. The syntax for the function is:

>>id(object)

This function requires an object as a parameter to execute successfully. It returns the identity of the object (In other languages, it is termed as memory location address). This ID is “guaranteed to be unique among the simultaneously existing objects”. This means that among all the variables of the same lifetime, this Id of one object cannot be equal to any other object. Although for different variables of different lifetime, this Id can be the same.

d="hello"
id(d)
t="hello"
id(t)

output:


59639616
59639616

In the above code, it can be noted that the data or information stored in both variables are the same. So, the Id generated for both the variables is also equal.

However, the above statement contradicts itself when the object data-type is not a string.

id(2056)
id(2056)

output:


59370656
59370992

The integer generates different Id whenever executed in the above code even though both the integral values are equal. Applying the same thing to two similar lists(with the same elements), the same result occurs.

The function usually has applications internally in Python.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *