DEV Community

Cover image for How Python Sees Variables
Mcvean Soans
Mcvean Soans

Posted on

3 1

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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (5)

Collapse
 
alainvanhout profile image
Alain Van Hout โ€ข

What you describe is also pretty much how Java approaches variables, which is indeed quite different than pointers in C/C++.

Collapse
 
mctechie profile image
Mcvean Soans โ€ข

Yes it's a very well said point! But I would like to say that very few languages follow this approach of viewing variables (Python included and not Java). This comes from the fact that Python is dynamically-typed and Java is statically-typed.

When we declare a "variable" in Java, we have to declare the datatype too. This helps JVM to sufficiently allocate memory according to the datatype. In Python, the "object" is given emphasis rather than the "variable". The object is stored in the memory first, and then a "tag" is associated to it.

The main difference between both is that the order in which the memory allocation happens. Java stores the data after the variable has already been created in memory, while Python stores the data (or object) first and then assigns a variable (or tag) to it.

I hope my reasoning has some credibility to it ๐Ÿ˜„

Collapse
 
alainvanhout profile image
Alain Van Hout โ€ข

In general that's true enough, but e.g. when you declare a variable and at that time set it to be equal to an existing variable, then no new memory allocation happens (in Java I mean).

To be honest, I'm not sure whether there would be a memory allocation when you set a variable to null when declaring it.

Thread Thread
 
mctechie profile image
Mcvean Soans โ€ข

In Java, null is just a value which a variable can have. It means that the variable refers to nothing, but still space is consumed for the "reference".

Java having two types of memory: stack and heap, primitive types (like int, float), and object references in Java are stored in the stack memory. Objects themselves are stored in the heap memory. Hece, if two int variables contain the same value they will both be stored on the stack.

In Python however there is no separation, and everything is considered to be an object, and every object is stored in the heap memory. If two int variables have the same value, Python optimizes memory on the heap by allowing the variables or "tags" to point to the same object on the heap.

For more info on memory management, you may kindly check out my previous post Python: Memory Management, or a GeeksforGeeks article on Java Memory Management.

Thread Thread
 
alainvanhout profile image
Alain Van Hout โ€ข

Yes, primitives are different, but quite a lot in Java is also 'just an object'. That primitives are different is also apparent in the fact that they can't be null.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

๐Ÿ‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Communityโ€”every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple โ€œthank youโ€ goes a long wayโ€”express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay