Python: Mutable & Immutable objects

Python: Mutable & Immutable objects

Introduction

Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. Python is a higher level programming language that is easy to read, easy to use and easy to maintain.

What is the id of a variable?

id function in Python returns the unique identity of the specified object. The id is assigned when a memory object is created. The id function always returns an integer value as the unique id of the object that remains constant throughout the program execution time and cannot be modified or changed. Variables pointing to the same memory object will have the same id.

No alt text provided for this image


What is a type() function in Python??

The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three arguments type(object, bases, dict) is passed, it returns a new type object.

Example type() with object parameter

No alt text provided for this image

Output-

No alt text provided for this image

Example - type() with 3 parameters

No alt text provided for this image

Output -

No alt text provided for this image

Mutable objects -

Mutable data types in Python are those whose value can be changed in place (means without making a separate copy of it) after they have been created.While doing any changes to the mutable objects the memory at which those objects are stored remains the same.

Example - List, Dictionary, Set

Immutable objects - Immutable data types are those, whose values cannot be modified once they are created. While doing any changes to the immutable objects, the memory at which these objects were stored during initialization, gets updated.?

Example - int, str, bool, float, tuple, etc.

Why does it matter and how differently does Python treat mutable and immutable objects?

Mutable and Immutable objects are handled differently in Python. Mutable objects are easier to change, whereas Immutable objects are quicker to access and require more memory to change because it involves the creation of a copy.

The use of mutable objects are recommended when there is a need to change the size or content of an object.

However, there is an exception in immutable objects as well. We know from the example earlier that tuples in Python are immutable, but the tuple consists of a sequence of names with unchangeable bindings to objects.

Consider the below example -

No alt text provided for this image

This tuple consists of both a list and a string. Strings are immutable so we are unable to change it’s value, however the contents of a the list can be changed. The tuple itself isn’t mutable, but contain items that are. The same can be done with Frozen Sets as well.As a general rule, Generally Primitive-like types are most likely immutable and Customized Container-like types are most likely mutable.

How arguments are passed to functions and what does that imply for mutable and immutable objects?

How are these objects behaving when they are passed to functions. There are usually two ways : Passing by reference meaning that the called functions parameter will be the same as the caller passed argument. And the second one, pass by value meaning the called function parameter will be a copy of the caller passed argument.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了