Python variables are not "boxes"

Python variables are not "boxes"

Get a solid understanding of variables and their assignment in Python.

·

3 min read

We all have heard the usual “variables as boxes” metaphor that is used to teach variables to newbies. In this blog, we will see why considering Python variables as “labels” attached to objects makes more sense than the box analogy.

We will first understand how objects/values are created before the assignment of a variable in Python. Then we will look at a code example which will conclude that variables are more like sticky notes to the data objects rather than boxes that store the data.

Hang tight, as this might challenge your existing knowledge about variables from other languages. But by the end, you will have a solid understanding of this most fundamental concept in Python or any other object-oriented programming language.

Variable assignment in Python

Python variables are fundamentally different than C/C++ variables where the box metaphor works fine.

In C/C++, a variable name is the one that owns the piece of memory, and different values can be assigned to the variable. So even if you alter the value, the memory address of the variable will not change, and hence the variable can be considered a box.

Whereas everything in Python is an object. Although we first declare the variable name in Python and then write its value, the assignment under the hood is happening in reverse direction.

The righthand side object must exist and should be valid before a name can be assigned or bound to it.

For example, in the code below, the variable “name” is assigned to the string object “Seve Jobs”, not the other way around. Example-2 will prove this.

In the following interpreter session, we try to multiply two strings which results in a “TypeError”. What’s interesting to note in the next step is that the interpreter throws an error upon evaluating the “full_name” variable as if it was never created.

This means that the exception was thrown while the righthand side of the assignment was being evaluated and as a result, the variable “full_name” never got assigned to this object.

The above code session proves that objects/values on the righthand side are evaluated before a variable name is assigned to it.

Variables as "labels"

Since now we understand that objects/values are created before the assignment of a variable in Python, it will be easier to understand that variables are mere labels to the data objects. Let’s prove this in the next example.

In example-3, to check if variables “names” and “born_in_1955” hold references to the same list and do not store copies of the list object, we make changes to the “names” variable and then observe the effects on the other variable “born_in_1955”.

It is apparent from the above interpreter session that changing the list for one variable also changes the list content for the other variable. This means that the two variables do not store copies of the same list but are attached and point to the same object in memory.

The image below gives a good visual feel of the “variables as labels” metaphor.

Image Credit: Fluent Python by Luciano Ramalho

I hope you now have a good grasp of Python variables and their assignment. The following note from the book Fluent Python summarizes this entire blog very well.

"To understand an assignment in Python, read the righthand side first: that’s where the object is created or retrieved. After that, the variable on the left is bound to the object, like a label stuck to it. Just forget about the boxes."

Luciano Ramalho, Fluent Python

P.S. This is my first blog, so please give suggestions about the writing and explanation. Also, feel free to ask any queries!