Variables In Python
Posted on Sun 29 September 2019 in Python
This article is part1 of a series
(part1, part2)
introducing Python variables
through illustrative diagrams.
The beauty of Python language is that, you, as a developer can go a long way without understanding the fundamental mechanism behind variables. However, a solid knowledge of these fundamentals would enable to reason your code much better.
So in this article, I will try to explain these fundamentals through helpful diagrams.
What are Variables
?
Variables are the basic building blocks of any programming language. In Python, everything is an object
and variables allow us
to attach names to these objects. The names can then be used to refer to these objects.
For instance, we can create an int object in Python like so:
>>> 20
20
>>>
Although the integer object(with value 20) is created using the above code, there is no way to access the object after its creation. Essentially this object is useless and will be cleaned up by Python.
Look at the following code:
>>> x = 20
>>> print(x)
20
>>> x + 10
30
The variable x
is a name attached to the integer object(with value 20) and it can be used to access the object. This opens up a world of possible operations we could do on the integer object, which was impossible otherwise.
Understanding Assignments
Assignment makes a name refer to an object.
The following is an example of an assignment statement in Python:
>>> x = 100
Let us understand this statement a little more:
a. The =
is the assignment operator.
b. To the right of it, 100
tells Python that it is an int object. So Python creates an integer object in memory.
This object has a few properties
-
id
: Memory location where Python stores the int object. -
type
: the type is int(eger) -
value
: the value of 100
>>> x = 100
>>>
>>> id(x)
94319675170784
>>>
>>> type(x)
<class 'int'>
>>>
>>> x
100
>>>
c. To the left of it, Python creates a variable x
and attaches it to the int object with value 100.
Essentially, the assignment operator binds
the name on the left(x
) to the int object(100
) on the right.
The following diagram illustrates the code above:
Many names
for one object
My son's official name is Aeben
. However, when playing with him, I lovingly call him Aebu
, Bobo
and so on.
Although there are 3 different names attached to him, they all refer to the same person. I could use any of those names
to call(access) him and he hears me.
Similarly, we can attach multiple names to the same object.
>>> x = 100
>>> y = x
>>> print(x)
100
>>> print(y)
100
x
is referring to the int object with value 100y
is not referring to x. Instead,y
is referring to the int object whichx
is referring to.- Both
x
andy
are equally valid names of the int object. - Neither
x
nory
are the real names of the int object. - We can attach any number of names to the int object.
Independent Reassignment
We can assign n
names to an object. Later, one name among them could be reassigned to another object independently
.
This means that the other n-1
names still refer to the initial object. The binding of the n-1
objects
is not affected by the reassignment of one name to a different object.
>>> x = 100
>>> y = x
>>>
>>> x = 200
>>> x
200
>>> y
100
This is illustrated in the following diagram:
x = 200
creates a new int object with value 200 and reassigns x
to it. Now, x
refers to 200. However, y
is still attached to 100 and
is not affected by the reassignment of x.
Garbage Collection
An object remains in memory as long as it has names referring to it. Each name increases the reference count of the object.
When no names refer to an object, its reference count becomes 0 and Python reclaims the memory of the object. This process is called
Garbage Collection
.
>>> person = "Raymond"
>>> id(person)
140351024963632
>>>
>>>
>>> person = "Guido"
>>> id(person)
140351024963688
In the above code, the name person
first referred to the string
object, "Raymond". Later, the name person
was reassigned to another string object "Guido". Now, the string "Raymond" has no names referring to it. Eventually, Python will reclaim the memory occupied by the string "Raymond".
This is illustrated in the following diagram:
The way Python cleans up memory during this process is transparent to the user and we don't have to worry about it. Python is smart enough to handle that.
Conclusion
In this post, I introduced the concept of variables in Python. Variables are names or labels attached to an object and used for accessing the object. An object can have many names and names can be independently reassigned. When an object has no names attached to it, Python reclaims the object's memory.
Understanding these fundamental mechanisms will enable you to reason your code much better. These mechanisms apply to any object in Python and not just int
or string
. The day I understood these concepts, it changed the way I looked at Python code. I hope you gained valuable insights from this article.
In the next part of this series, I will cover how variables work with mutable and immutable types. I will also emphasize the concepts of mutation and rebinding.
That's it, readers, until next time! Happy coding Python!