Object Mutability In Python

Object Mutability- By Sagar Jaybhay

what is an object?

It is instance and which is having its state and type. So when we change the data inside that object it is called modifying the internal state of the object.

In this when we change data inside the object memory address of that object is not changed. And it called object is muted means an internal state of change is called mutable.

Immutable: the object whose internal state cannot be changed is called immutable.

Immutable Mutable
Numbers(int, float, Booleans, complex number) List
Strings Sets
Tuples Dictionaries
Frozen Sets  
User-Defined classes but only If you restrict to not to modify  

Tuples are immutable.

T= (1, 2, 3)

In this tuple you cannot add remove insert or replace.

List are Mutable

L= [1, 2, 3] 

Functions:

When you write a function and if you pass parameters which are immutable it’s good because if modification is done in the function it a completely new variable. But if you pass mutable object then if your function performs some operations on that and if the values change the address remains same. What effect is that you are performing an operation on base variables and whatever modification you did is applicable to base variable?

Shared References and Mutability:

Shared reference means 2 variables pointing to the same reference.

In below example, we create 2 different variables of different type but the value assign is the same so if you see below out even if 2 different variables the memory address is the same this called shared references. Below example is for the immutable object in python. The python program manager will create shared references for the immutable object. But have no guaranty is that program manager always does this, sometimes it does and sometimes not.

For mutable object Python program manager not create shared references.

Equality of Variables:

is the operator is the identity operator which compares the memory address of 2 different objects. If you want to check internal state or data of 2 different objects we can use == sign or equality operator. For negation is not operator is used and for object state != is used.

None

The None object can assign to variables to indicate that they are not set. Is also called empty value or null like in other programming languages like c#, java. The None object is a real object and can be managed by the Python memory manager. So python memory manager always uses shared references when to assign the value None to the variable. None is an object, not a value. It is useful to indicate the value of the variable is nothing.

Python:

  1. CPython: is a slandered or reference to python implementation which is written in c.
  2. Jython– written in Java and can import java class and compiles to Java bytecode and can be run on JVM
  3. IronPython- it is written in c# and targets for.Net or mono clr.
  4. PyPy- it is written in RPythonis a statically typed subset of python which is written in c and used for interpreters.

Python Interning int

It means reusing objects in demand. At startup, Python preloads or caches a global list of integers in the range [-5,256]. When the interpreter finds the integers in that range it will create a cached version of that object. It refers to the singleton pattern for optimization strategy.

When we create a=10 it cache that but when we create a=500 then not because it is outside of that range.

Python Interning String

When python code is compiled identifiers are interns what it means

  1. Variable names
  2. Function names
  3. Class names

Means the variable whose identifier matches with declaration identifier criteria that will intern.

Interning is mainly for optimization in terms of memory & speed. Not all string automatically intern, but you can force strings to be interned by using sys.intern() method.

See in below image when we write a function and function get compiled constant means before the call to function it will get evaluated.

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...

1 Response

  1. Wonderful goods from you, man. I have understand
    your stuff previous to and you’re just extremely magnificent.
    I actually like what you’ve acquired here, really like what
    you are saying and the way in which you say it. You make it entertaining and you still care for to keep it wise.
    I can not wait to read far more from you. This is really
    a great website.

Leave a Reply

Your email address will not be published. Required fields are marked *