How Python Sees Variables

mctechie

Mcvean Soans

Posted on November 26, 2020

How Python Sees Variables

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;
Enter fullscreen mode Exit fullscreen mode

This may be viewed as a memory box:
Variable a
Say we now store another value using the same variable

a = 2;
Enter fullscreen mode Exit fullscreen mode

This may be viewed as an updated memory box:
Updated Variable a
Let us now store the value of this variable into another

int b = a;
Enter fullscreen mode Exit fullscreen mode

This creates another memory box as follows:
New Variable
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
Enter fullscreen mode Exit fullscreen mode

This may be viewed as the value or object having priority, to which a tag is assigned:
Variable a
Say we now store another value using the same variable

a = 2
Enter fullscreen mode Exit fullscreen mode

In this case, the tag is simply changed to the new value, and the previous value (now unreferenced) is removed by the garbage collector:
Updated Variable a
Let us now store the value of this variable into another

b = a
Enter fullscreen mode Exit fullscreen mode

In this case, another tag is created that references to the same object:
New Variable
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.

💖 💪 🙅 🚩
mctechie
Mcvean Soans

Posted on November 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How Python Sees Variables
python How Python Sees Variables

November 26, 2020