How Python Sees Variables
Mcvean Soans
Posted on November 26, 2020
In programming languages like C and Java, the concept of a variable is related to a memory location. Hence, often a variable is defined as a named memory location. Let us visualize this with an example, say we have the equation
int a = 1;
This may be viewed as a memory box:
Say we now store another value using the same variable
a = 2;
This may be viewed as an updated memory box:
Let us now store the value of this variable into another
int b = a;
This creates another memory box as follows:
This is basically how other programming languages visualize variables. We can simply say that the "memory location" is emphasized which contains a "value". However, Python views variables as "tags" which are referenced (or tied) to some "value" (or object).
Let us visualize the above examples once again, say we have the equation
a = 1
This may be viewed as the value or object having priority, to which a tag is assigned:
Say we now store another value using the same variable
a = 2
In this case, the tag is simply changed to the new value, and the previous value (now unreferenced) is removed by the garbage collector:
Let us now store the value of this variable into another
b = a
In this case, another tag is created that references to the same object:
Hence, only one memory is referenced by two names.
So to conclude, other languages have variables, while Python has 'tags' to represent the values (or objects). This method allows Python to utilize memory efficiently, and this visualization helps in understanding variables in Python.
Posted on November 26, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.