mutable and immutable objects

Objects in Python can be Mutable or immutable.
For mutable objects, modifications occur in place, and the object's identity (memory address) remains the same. In contrast, operations on immutable objects create a new object with a different identity (memory address), as the original object cannot be altered.

Understanding Objects in Python

In Python, everything is treated as an object, making it a highly object-oriented programming language. Every object in Python has three core attributes:

  • Identity: This represents the memory address where the object is stored, ensuring its uniqueness during the object's lifetime.
  • Type: This defines the category or kind of object, such as an integer, string, list, or custom-defined class.
  • Value: This refers to the data or content stored within the object. For instance, a list like my_list = ['a', 'b', 'c'] holds the values a, b, and c.

These attributes together determine the behavior and characteristics of an object in Python.

Mutable and immutable object and id()

List is a mutable object but tuple is a immutable object.

Check how the identities of list and tuple are changing after adding elements.

Immutable objects are tuple, string, int, frozenset, bytes
Mutable objects are list, dictionary , set ,bytearray

Let us try one string ( immutable )
my_str='plus2net.com'
print(my_str[4])
my_str[4]='5'
Above code will generate error saying TypeError: 'str' object does not support item assignment

Using List ( mutable )
my_list=[1,2,3]
my_list[1]=8
print(my_list) # [1, 8, 3]
Using a tuple ( immutable )
my_tuple=(1,2,3)
my_tuple[1]=8
print(my_tuple)
Above code will generate error saying TypeError: 'tuple' object does not support item assignment

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]
str1='welcome'
print("address of list : ", id(my_list))
print("address of string str1 : ",id(str1))

# 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))

# str1[1]='p' # this will generate error 
# new string object is created now, 
# not modified the old one, address changed
str1='Python' 
print("address of string str1 : ", id(str1))
Output
address of list :  140301581527624
address of string str1 :  140301582741432
address of list :  140301581527624
address of string str1 :  140302155689016
Address (id ) of the string is changed when we assign a different value to it, as string is immutable
Address of list remain same as we add or update elements of it, as list is mutable.

More examples using List

This shows that modifying a list does not change its id.
my_list = [5, 7, 5, 4, 3, 5]
print(id(my_list))  # e.g., 139787163608584
my_list += [9, 8]
print(id(my_list))  # e.g., 139787163608584
print(my_list)      # [5, 7, 5, 4, 3, 5, 9, 8]

Using Tuple

This demonstrates that modifying a tuple results in a new object with a different id.
my_tuple = (1, 2, 3)
print(id(my_tuple))  # e.g., 139787173397704
my_tuple += (4, 5)
print(id(my_tuple))  # e.g., 139787174601112
print(my_tuple)      # (1, 2, 3, 4, 5)
We can make elements of an iterator immutable or unchangeable by using frozenset()
hash() : Hashable and Immutable objects id()
any() Iterator in and not in: The Membership operators


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    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