id() : The identity of an object

id(object)
Returns unique identity ( integer or long integer ) of the python object.
identity is assigned when object is created.
It is the address of the object's memory
id changes each time the program is run except some objects for which id is fixed.

Using integer

my_int=34
print(id(my_int)) # 10969856 
Using list
my_list=[1,2,3]
print(id(my_list)) #139787163392648
Below output will change each time we run the program but watch how the id remain same for different objects.
a=23
b=a
print(id(a))    #1173520149488
print(id(b))    #1173520149488

str1='plus2net'
str2='plus2net'
print(id(str1)) #1173522439088
print(id(str2)) #1173522439088

Fixed id

These ids will remain same even after re-run of the program.
my_int=-5
print(id(my_int)) # 10968608
my_int=256
print(id(my_int)) # 10976960
my_int=257
Difference between value and id using is and == operator

Mutable and immutable object and id()

For mutable objects, by changing the element the ID will not change , whereas for immutable objects id will change.

More on Mutable & Immutable objects

List is a mutable object but tuple is a immutable object. Check how the identities are changing after adding elements.

Using tuple
my_tuple=(5,7,5,4,3,5) 
print(id(my_tuple)) # 139787173397704
my_tuple +=(9,8)
print(id(my_tuple)) # 139787174601112
print(my_tuple)     #(5, 7, 5, 4, 3, 5, 9, 8)
Using List
my_list=[5,7,5,4,3,5]
print(id(my_list))  # 139787163608584
my_list +=[9,8]
print(id(my_list))  # 139787163608584
print(my_list)      # [5, 7, 5, 4, 3, 5, 9, 8]
Two non-overlapping lifetime objects may have the same identity or id() vlaue.

Comparing list( Mutable ) with string ( immutable )

When we assign a different value to a string variable we are not modifying the string object, we are creating a new string object.
But when we change any element of a list we are not creating a different object, we are only modifying the same object.
my_list=[1,2,3]
str='welcome'
print("address of list : ", id(my_list))
print("address of string str : ",id(str))

# change the values 
# modified the list object, not a new object , address is same.
my_list[1]=7 
print("address of list : ", id(my_list))

# str[1]='p' # this will generate error 
# new string object is created now, 
# not modified the old one, address changed
str='Python' 
print("address of string str : ", id(str))
Output
address of list :  140301581527624
address of string str :  140301582741432
address of list :  140301581527624
address of string str :  140302155689016
The id value of list is not changing, but it is changing for string.

Key Points About id() Function

  • Uniqueness: The identity value is guaranteed to be unique among simultaneously existing objects. Once an object is destroyed, its identity may be reused for a newly created object.
  • Implementation Dependent: The actual value returned by id() is implementation-dependent. While in CPython it's the object's memory address, other Python implementations might use different methods.
  • Use in Comparisons: The id() function is useful in comparing whether two variables point to the same object in memory, which is a stricter comparison than equality of value.
In summary, the id() function provides a way to obtain the unique identifier of an object in Python, which is closely tied to the object's location in memory in CPython. This function is especially useful for understanding object identity and memory management within Python applications.
variables hash() : Hashable and Immutable objects Mutable & Immutable object Iterator any() is() & not is() Operators




Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer