mutable and immutable objects

Objects can be Mutable or immutable.
Mutable objects values can be chagned , whereas immutable object values can't be changed.
Identities remain same for mutable objects but changes for immutable objects.

Mutable and immutable object and id()

For mutable objects by changing the element the id() vlaue will not change , whereas for immutable objects id() value will change. 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
mutable objects are list, dictionary , set

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

We can make elements of an iterator immutable or unchangeable by using frozenset()

any() Iterator in and not in: The Membership operators



plus2net.com



Post your comments , suggestion , error , requirements etc here




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