Python Memory Management(Mutable and Immutable objects).
Python Programming Language is probably the most fluent programing language out there, but have you ever experienced ambiguity in one of the most uncomplicated codes? Something like assigning a value to a variable.
Check out this code:
>>> x =
>>> y = x
>>> print(x)
4
>>> x = 8
>>> print(y)
4
Bad news: you might have thought of it to output(4 and 8), but it is OK to cause the good news is that you are about to find out why your code acted this way.
OOP in Python
“Everything is an object”
understanding the object-oriented nature of Python is the key here.
An object is an instance of a particular class. We can think of a class as a blueprint, a template based on which an object is created. And, anything that could be named in this language such as an integer, string, function and so on is an object of that corresponding name class, more like the name of a class tells the data type of its objects(also known as "instances").
The built-in function type() prints the class of the specified object:
>>> s = "banana
>>> type(s)
<class 'str'>
>>> n = 4
>>> type(n)
<class 'int'>"
Mutable and Immutable Objects
There are two kinds of objects in Python:
e.g: list, dict, set, byte array, user-defined classes.
e.g: int, float, long, complex, string tuple, bool.
the built-in function id() can be used to check the mutability of a function. it returns the unique identity of an object, thus if an object is modified it would retain the same number, but if it was defined as a new object then it is to return a different id.
for instance:
>>> x =
>>> id(x)
2152779415888
>>> x = 4 + 2
>>> id(x)
21527794159524
How about a mutable object:
>>> L = [1, 2, 3
>>> id(L)
4430688016
>>> L += [4]
>>> print(L)
[1, 2, 3, 4]
>>> id(L)
4430688016 # this is the same as before!]
NOTE: note that the '==' operator differs from the 'is' operator, while the '==' operator compares the values or equality of two objects, the python 'is' operator checks whether two variables point to the same object in memory.
Recommended by LinkedIn
Python Memory Management
Memory management in Python involves the management of a private heap. A private heap is a portion of memory that is exclusive to the Python process. All Python objects and data structures are stored in the private heap. The operating system cannot allocate this piece of memory to another process.
Therefore, Each time we create a variable that refers to an object, a new object is created.
Check out this code:
>>> L1 = [1, 2, 3]
>>> L2 = [1, 2, 3]
>>> L1 == L2
True # L1 and L2 have the same value
>>> L1 is L2
False # L1 and L2 do not refer to the same object!
However, through a process called aliasing, we can assign a single object to multiple variables. Example:
>>> L1 = [1, 2, 3]
>>> L2 = L1 # L2 now refers to the same object as L1
>>> L1 == L2
True
>>> L1 is L2
True
>>> L1.append(4)
>>> print(L2)
[1, 2, 3, 4]
Note: we can refer to these two codes in two different ways, the one of reference, which is the latter and the one of mere assignment which is the first example.
Exceptions with Immutable Objects
before we move any further let us consider two import exceptional treatment of some immutable objects.
Python caches small integers, which are integers between -5 and 256. These numbers are used so frequently that it’s better for performance to already have these objects available. So these integers will be assigned at startup. meaning for the int object, marcos called NSMALLPOSINTS and NSMALLNEGINTS are used, Then each time you refer to one, you’ll be referring to an object that already exists.
check this out:
>>> a = 256
>>> b = 256
>>> a is b
True
which is on contrary, when the integer is out of that range;
>>> a = 257
>>> b = 257
>>> a is b
False
2. Empty Immutable Objects
Let’s take a look at empty tuples, which are immutable:
>>> a = (
>>> b = ()
>>> a is b
True # a and b both refer to the same object in memory)
however, the story be different when for non-empty tuples:
>>> a = (1, )
>>> b = (1, )
>>> a == b
True
>>> a is b
False
SUMMARY:
Hoolai...Thank you for sticking through this whole article, that was a lot of information to acquire in one go, I hope you have learned a couple of things. KEEP LEARNING!