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.
|Numbers(int, float, Booleans, complex number)||List|
|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]
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.
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.
- CPython: is a slandered or reference to python implementation which is written in c.
- Jython– written in Java and can import java class and compiles to Java bytecode and can be run on JVM
- IronPython- it is written in c# and targets for.Net or mono clr.
- 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
- Variable names
- Function names
- 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.