Stumbling upon a Python code snippet from a GitHub repo I did come to realize that in Python variables don’t actually store values they are assigned too, variables actually store the location to the value. Unlike in C++/C which actually creates a space of a fixed size and assigns it to a variable created which we usually call a bucket/room while explaining variables to a beginner “what variables are?” in programming.
Thought python variables are a bit different they work like keys which points to a particular room in the hotel(memory space) so whenever we make an assignment to available we are not creating rooms rather creating keys to that room which is freed/overwritten by the python’s garbage collector automatically. (More on the topic of garbage collector later). So the point is whenever we do something like
>>>a = 10
>>>b = 10
>>>id(a)
94268504788576
>>>id(b)
94268504788576
We are optimizing here and creating two same keys which points to the same room in the hotel (memory) thus they would have the same id but this kind of optimization works only with the range of integers from -5 to 256 if you exceed the range, variables would point to two different storages thus will have different id().
Just don’t get confused why we did not use “==” instead used id(), == checks whether the value pointed by the variables are same or not inside the object and id() checks if it uses the same object or not because every object has a unique identity which can be checked by the id() function.
Coming back to the code snippet from the GitHub repo given below and applying the same knowledge of integers to strings.
>>>a = "wtf"
>>>b = "wtf"
>>>id(a),id(b)
(139942771029080, 139942771029080)
>>>a = "wtf!"
>>>b = "wtf!"
>>>id(a),id(b)
(139942771029192, 139942771029136)
>>>a = "hello world this is a string"
>>>a = "hello world this is a string"
>>>id(a),id(b)
(139942770977328, 139942770977408)
The same kind of optimization happens here too when the strings are small they refer to the same object in the memory rather creating a new one thus saving memory, this kind is called interning. But when the object becomes bigger or contains ASCII letters, digits or underscore they are not interned.
This shows abstraction at it’s best and python is very good at it, it does all the heavy lifting job for you of allocating/deallocating memory for you and lets you focus on the other parts of the program. Until you really want to know what’s happening and I assume you do that’s why you are reading this blog 😛
Though this explanation was already available on the repo. I wanted to know more about how memory management happens internally so I stumbled about a talk of “ Memory Managment in Python – The basics” by nnja. So yeah people with nicks like ninja are great with Python and Fortnite, hahaha! (I could not resist myself from posting this joke and just to clear things out ninja is one of the top Fortnite players)
Thus if you see technically Python does not have variable but ‘names’ which refers to other objects or names and Python likes to keep a count of all the references called as reference count of all the object’s references. So if the reference count of an object decreases to zero this means no reference is made to that object which as seen by the Garbage collector of python as free space thus the object is deleted and space is free to use.
We as a programmer can increase or decrease the reference count of an object as a python object stores three things:
* Type: int,string,float
* Reference Count
* Value
Seeing the first code snippet of the blog where two names a and b in which both names points to the same object with the value 10 with the reference count of 1 and type as int.
That’s all for this part, will cover the same topics in a bit detail in the next part. Still confused in some of the things so keeping this plain and simple for future me to look back when I am lost and can’t even notice the simple things 😛 and for someone who just wants to know this briefly.
Top comments (1)
Thanks :)