Behind the scene

A deep dive behind the scenes of variables

Python is a highly object-oriented programing language, In fact, every item in python is considered an object of a specific class in python.

Python variables are references or pointers to an object.

For example,

n=300

This assignment creates an integer object with the value 300 and assigns the variable n to point to that object.

Now, if we consider,

m=n

Here python will not create another object but just point to the same object by creating a new pointer alone.

Now, if we change the value of m,

m=400

Now, python creates an integer object 400, and m is made as a reference to it.

Suppose we are changing,

m="foo"

Now, a string object is created with the value foo, and m is made as a reference to that object.

Here, if you notice there is no reference for the integer object with the value 300 and no way we can access it.

When the number of references to an object drops to zero, it is no longer accessible. At that point, its lifetime is over. Python will eventually notice that it is inaccessible and reclaim the allocated memory. This process is garbage collection.

Still, not clear about the above topic? You can watch this tutorial for a clear idea of Memory Allocation in python | Episode-3 | Pythonista_Geek | Shravanthi | Basics of python

For an example implementation, Memory Allocation with example in python | Episode-4 |Pythonista_Geek | Shravanthi |Basics of python

Conclusion

I hope, this article gave a clear idea about the internal process of variables in python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.