C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python id() FunctionPython id() function returns an identity of an object. This is an integer which is guaranteed to be unique. This function takes an argument an object and returns a unique integer number which represents identity. Two objects with non-overlapping lifetimes may have the same id() value. Signatureid (object) Parametersobject : It is an object of which id is to be returned. ReturnIt returns a unique integer number. Let's see some examples of id() function to understand it's functionality. Python id() Function Example 1# Python id() function example # Calling function val = id("TheDeveloperBlog") # 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 Python id() Function 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 id() Function Example 3# Python id() function example l1 = [1,2,3,4] l2 = [1,2,3,4] l3 = [3,5,6,7] # Calling function id1 = id(l1) id2 = id(l2) id3 = id(l3) # Displaying result print((l1==l2),(l1==l3)) # Objects with the same values can have different ids print((id1==id2),(id1==id3)) # l1 and l2 returns True, while id1 and id2 returns False Output: True False False False
Next TopicPython Set
|