2. What is Immutable?
Once we created an object then we cannot change that
object.
Immutable means unchangeable.
All fundamental data types (int, float, complex, bool,
str) is immutable.
Uses for internal reason(memory utilization).
x = 10
x = 20
x 10
20
Only variable’s reference changed and the
previous value is left unchanged
3. Representation of variable’s reference
a 10
20
1723196320
1723196480
b
Id is built in func to get the address of an identifier.
Reuse of object for b
4. Advantage or disadvantage ?
Some may think that if we keep on changing value of x then we have to
make new object for each ‘x’, so there may be a chance of memory wastage,
performance may down.
But don’t think like that because it is really an advantage feature of Python.
Because if a object is already available then it uses the existing object
suppose we have thousands of variable with same object
x1 = 10
x2 = 10
x3 = 10
xn = 10
10
x2
x1
x3
x5x4
x6
x7
xn
Only one object is shared with n number of variables. No need to create
separate object. This makes memory utilization and better performance
5. Example
Hyd
v1
v2
v4
v3
Agra
Now v3 changes the city to Agra, then only the link
will change from “Hyd” to “Agra” the object will not
be deleted as it is still shared by v1, v2, and v4
49316480
49317984
6. ‘is’ operator
Is operator tests if two variables point the same object, not
if two variables have the same value.
Lists are mutable i.e., changeable
Different address though x and y have same value. But lists are
mutable i.e., both x and y points to different object.
But if you assign (y = x) then both points to the same object.
Returns True if both points to same object
7. Use of ‘is’ operator instead of id to check
variable references
Only one True object is created with 3 references pointing
towards the single object
8. What if I take x = 257?
It should be True
Different address, means 2 different object for
same x = 257
257x
y 257
58040688
58041344
9. Cont...
Internally, reusing of same object is available from 0
to 256 only(most precisely -5 to 256), because this range is
most commonly used by programs.
In programs we generally use x = 10 or x = 20 etc.
There are very rare case when we use more than 256
values.
More than 256 is very rare case to use so there is no
need to use the same object. If do so there may be a
chance of memory wastage.
10. Cont...
What happens internally is at the time of python
interpreter starts than before execution of program,
interpreter creates 0 to 256 integer objects. i.e., objects
with content 0 to 256 created at the time of interpreter
starts.
a = 10 interpreter checks that: Is already object for
content 10 is available or not?
Obviously object with content 10 is already created at the beginning .
Then ‘a’ just points to already created object. Interpreter doesn’t
create the new object from 0 to 256.
If a = 500 will create the new object as object with
content 500 will not be found.
11. Why the range is 0 to 256 only?
Ques: Why Python interpreter doesn’t make more objects i.e.,
more than 256 lets say rages from -1 crore to 1 crore?
Ans: As we have discussed that 0 to 256 objects created at the
time of interpreter starts as 0 to 256 is most commonly used
range. Lets suppose if python creates object from -1 crore to 1
crore i.e., 2 crore objects at time of interpreter starts than
python will take more and more time to start. May be one day
after . So python start up will become very costly.
we mostly use 2 or 3 objects, so for the sake of 3 or 4 objects,
creating 2 crore objects at the beginning is stupidness and
instead of advantage many problem may occur.
12. Immutability for bool
In Boolean case, it always reuse the existing object
It never create the objects at run time, because only two
objects(True & False) are required to create
These two objects created at the time of interpreter starts.
Same address i.e., same object is shared
13. Immutability with str
It always create the object at run time.
It doesn’t create object at time of interpreter starts because there
may be infinite strings literals a user can choose. Then for how
many and for which literal interpreter makes object at beginning,
this prediction is impossible.
But still Python gave reuse of same object facility to str type also
because most of the time we use same str literals.
True shows same object is shared
15. Immutability with float & complex
Surprisingly reusability of same object is not with
float and complex numbers.
16. Cont...
If we talk about int only 0 to 256 values are in range of
reusability the same object which is fixed.
In case of bool only two value are there to hold the concept of
reusability.
But in terms of float or complex, can you tell how many values
are there between in 0 and 1 only
0.1
0.01
0.11
0.101
infinite
Then how python will make this much object at the beginning.
At language level, float and complex assume to be immutable.
0.1 + 0.2j #complex also have floating values
0.11 + 0.101j
0.10 + 0.111j
0.01 + 0.9j ........infinite
17. Things to remember
All fundamental data types are immutable.
Immutable means unchangeable.
‘is’ operator test if both variable points to same object or
not.
Reusing the same object lies in following ranges:
Int 0 to 256
Bool always
Str always
Float Never
Complex Never
In terms of str python create object at the run time not at
the beginning as str value are infinite.
Reusing of same object is not available. Every time a
new object is created