Python id(): How to Use id() Function In Python Example

Python id() is an inbuilt function that is used to get the identity of an object. Two objects with the non-overlapping lifetimes may have the same id() value. In CPython implementation, it is an address of the object in memory.

Syntax:

id (object)  

Parameters

  • object : It is an object of which id is to be returned.

Return

  • It returns a unique integer number.

Python id() Function Example

Example 1:

# Python id() function example  
# Calling function  
val = id("Javatpoint") # string object  
val2 = id(1200) # integer object  
val3 = id([25,336,95,236,92,3225]) # List object  
# Displaying result  
print(val)  
print(val2)  
print(val3)  

Output:

139963782059696
139963805666864
139963781994504

Example 2:

# Python id() function example  
class Student:  
    def __init__(self, id, name):  
        self.id = id  
        self.name = name  
          
student = Student(101,"Mohan")          
print(student.id)  
print(student.name)  
# Calling function  
val = id(student) # student class object  
# Displaying result  
print("Object id:",val)  

Output:

101
Mohan
Object id: 140157155861392

#python #python id

Python id(): How to Use id() Function In Python Example
1.85 GEEK