Mutable and Immutable Objects in Python

Python traces its origin back to the early 1990s, when Guido Van Rossum, a worker at Centrum Wiskunde & Informatica (CWI), a Dutch research center, had the idea of ​​developing a new language based on a previous project, the Python language. “ABC” programming, which he himself had developed together with his colleagues.

In this article we will talk a little about the Is() and Type() functions and we will learn about mutable and immutable objects.

What is Python?

Python is a widely used, object-oriented, interpreted, high-level programming language with dynamic semantics, used for general-purpose programming.

Python is an easy language to read and write due to its high similarity with human language. In addition, it is an open source multi platform language and, therefore, free, which allows you to develop software without limits.

Function id() and type() in Python

id():

The id() function es an inbuilt function in Python, returns the identity of object. It is the address of object in memory, which will be unique and constant throughout the lifetime of object.

Syntax

id(object)

Example:

type():

The type() function es an inbuilt function in Python, returns the type of the specified object.

If a single argument (object) is passed to type() built-in, it returns type of the given object, but If three arguments (name, bases and dict) are passed, it returns a new type object.

Parameters:
name:
name of class, which later corresponds to the __name__ attribute of the class.
bases : tuple of classes from which the current class derives. Later corresponds
to the __bases__ attribute.
dict : a dictionary that holds the namespaces for the class. Later corresponds
to the __dict__ attribute.
Return type:
returns a new type class or essentially a metaclass.

Syntax

type(object)
type(name, bases, dict)

Example

Mutable and Immutable objects

There are two types of objects in python 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 summaries the difference, mutable objects can change their state or contents and immutable objects can’t change their state or content.

Mutable objects

They are those objects that can change their content without changing your id after their creation, and among them we have: lists, byte arrays, sets, and dictionaries.

Lists

Byte Arrays

Sets

Dictionaries

Immutable objects

They are those objects that can’t change their content without changing your id after their creation, and among them we have numeric data types, strings, bytes, frozen sets, and tuples.

Numeric data types

In those we can include boolean, floats, complex numbers, fractions, and decimals

String and Bytes

Tuples

Conclusions

Both mutable and immutable objects have different uses and their own uses, none is much better than another, but on different occasions one may be more appropriate than another.

Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy.

Use of mutable objects is recommended when there is a need to change the size or content of the object.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store