id() function in Python
In Python, the id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location.
Return: a unique integer for a given object
10731304 10731304 10731304
As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.
Python id() function Examples
Python id() for Inbuilt DataTypes
In this example, we are printing the id of multiple datatypes like strings and lists.
140252505691448 140252505691448 True 140252505691840 140252505739928 False
Python id() for custom object
In this example, we are creating the Python class and we are creating two Python class objects and checking their ids.
Python id() with Sets
In this example, we are using the id function on sets in Python.
139755710961408 139755710961184 139755709277664
Python id() with Tuples
In this example, we are using the id function on tuples in Python.
140102982048128 140102982084288 140102982048128
Please Login to comment...