The Wayback Machine - http://web.archive.org/web/20221002200900/https://www.geeksforgeeks.org/mutable-vs-immutable-objects-in-python/amp/

Mutable vs Immutable Objects in Python

Every variable in python holds an instance of an object. There are two types of objects in python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object.

To summarise the difference, mutable objects can change their state or contents and immutable objects can’t change their state or content.

Conclusion

  1. Mutable and immutable objects are handled differently in python. Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy.
    Whereas mutable objects are easy to change.
  2. Use of mutable objects is recommended when there is a need to change the size or content of the object.
  3. Exception : However, there is an exception in immutability as well. We know that tuple in python is immutable. But the tuple consists of a sequence of names with unchangeable bindings to objects.
    Consider a tuple
     tup = ([3, 4, 5], 'myname') 

    The tuple consists of a string and a list. Strings are immutable so we can’t change its value. But the contents of the list can change. The tuple itself isn’t mutable but contain items that are mutable.

As a rule of thumb, Generally Primitive-like types are probably immutable and Customized Container-like types are mostly mutable.


Article Tags :