![]() |
VOOZH | about |
The id() function is used to get the unique identity number of an object in memory. This identity remains the same as long as the object exists. If two variables have the same id(), it means they are pointing to the same object in memory.
id(object)
139745147511024 139745147511024
Note: id() values may differ across Python environments and runs.
In the above example:
Example 1: This example compares two different list objects to show that even with similar values, their memory identity is different.
128102271441856 128102273731776
In the above example:
Example 2: This example uses a custom class to show that each object has its own unique identity.
126157094693120 126157093473488
In the above example:
Example 3: This example checks whether two strings with the same text refer to the same object in memory.
139546861085408 139546861085408
In the above example:
Example 4: This example shows how tuple objects with the same values may or may not share memory.
130201030380352 130201030380352
In the above example:
The is keyword in Python is used to check whether two variables point to the same object in memory.
True True
Explanation: