The id()
function in Python returns the identity of an object. This integer id actually represents the address of the object where it is stored in the memory. This id remains constant during the lifetime of the object. This means if you rerun the program the id value is not guaranteed to be the same.
name = 'John' print('id of name is: ', id(name)) # Output # id of name is: 139697701582896
Syntax of id()
The syntax of the id()
function is as follows:
id(object)
Parameters of id()
The id()
function accepts only one parameter:
- object – The object whose identity has to be returned
id() Return Values
The id()
function returns an integer value which represents the address of the object in the memory.
Python id() for Literal Values
A literal is raw data assigned to a variable or constant in our program. All literals in Python are objects. This means they definitely have a unique identity. Let’s see if they do have an Id associated.
Example :
# Id of integers print(id(10)) # Id of float numbers print(id(10.23)) # Id of string values print(id('Hello'))
Output:
9788896 139769290373392 139769289934960
Note: The value of Id can differ on your computer.
Python id() for Variables
Similar to literals, variables are also objects in Python. These variables also have a unique Id. The length of the Id depends on the type of data stored in the variable.
Example 1: When the values stored in the variables are different
If values(data) stored in variables are not the same, the Id of each variable will be unique. See the example below:
a = 10 print('Id of a: ', id(a)) b = 20 print('Id of b: ', id(b))
Output:
Id of a: 9788896 Id of b: 9789216
Example 2: When the values stored in variables are exactly the same
If two or more variables in Python contain exactly the same value(data), it is guaranteed that their Id would be exactly the same. Python does this to minimize the memory space used by our program. This is known as caching in Python.
a = 10 print('Id of a: ', id(a)) b = 10 print('Id of b: ', id(b))
Output:
Id of a: 9788896 Id of b: 9788896
You can try out other values also. The variables having the same values will have the same Id value.
Python id() for Immutable Objects
In the previous example, we saw that the id()
function returns the same Id if two variables contain the same value. But, now the question arises will it work for other data types also?
Well, In simple words, Python caching is performed only for immutable objects. This means if two or more immutable objects(like strings, tuples, floats, numbers) have the same values(data), It is guaranteed that their Id would definitely be the same.
Example:
# Id of strings str1 = 'abc' str2 = 'abc' print('Id of str1: ', id(str1)) print('Id of str2: ', id(str2)) print(id(str1)==id(str2)) # Id of tuples tuple1 = ('a', 'b', 'c', 'd') tuple2 = ('a', 'b', 'c', 'd') print('nId of tuple1: ', id(tuple1)) print('Id of tuple2: ', id(tuple2)) print(id(tuple1)==id(tuple2))
Output:
Id of str1: 139738371420848 Id of str2: 139738371420848 True Id of tuple1: 139738371156304 Id of tuple2: 139738371156304 True
You can easily observe from the above output that the strings and tuples having the same data have the same Id value.
Python id() for Mutable Objects
In the last example, we saw that if two or more immutable objects contain the same data, their Id will be exactly the same. Does the same rule apply to mutable objects also? Well, the answer is no.
Mutable objects(lists, sets, dictionaries, etc) always have a unique Id irrespective of the data they contain. This means even if two mutable objects have exactly the same data, their Id would never be the same.
Example:
a = [1, 2, 3, 4] b = [1, 2, 3, 4] print('Id of a: ', id(a)) print('Id of b: ', id(b)) print(id(a) == id(b))
Output:
Id of a: 140021326610112 Id of b: 140021325973952 False
Python id() for Custom Objects
Similar to mutable objects, the Id of custom(user-defined) objects is always unique even if they contain exactly the same data.
Example:
class Student: def __init__(self, name, age): self.name = name self.age = age student1 = Student('John', 23) student2 = Student('John', 23) print('Id of student1: ', id(student1)) print('Id of student2: ', id(student2)) print(id(student1) == id(student2))
Output:
Id of student1: 140324975477952 Id of student2: 140324974844080 False