Skip to main content

Inner Working of Python

Updated 4 min readMarkdown
Part of seriesChai aur Python
Inner Working of Python

What is Python?

Well, it was a snake until I wasn't into the tech world. After being exposed to actual code I heard of it as a programming language. Everyone said its the most beginner-friendly language they have ever seen and everyone seemed crazy about it.

I looked at a python code and it made me stare at it for a long time as it was just too easy. It amazed me and I wanted to learn the language.

Why do I love the language?

It was introduced to me in school as I took Computer Science as the optional subject. After getting into the high school (or as they call it, a college), I learnt many languages, worked on MERN stack mainly. But, recently I had time to kill, I remembered the old lost friend of mine. PYTHON, and no, I am not saying my friend is a snake (although he is). Python seems so easy to me, and also triggers that nostalgia. Also, nowadays learning Python is kind of a necessity.

I started brushing on it up from Hitesh Sir's Chai aur Python series on YouTube. I have to admit, Sir... if you are reading this, thanks a lot for this series. This article is the assignment given by you in the third video and a way to say Thank You! :)

Python inner working

Python: Under-The-Hood

It might seem that a lot goes under the hood. But, honestly it isn't that difficult to understand. All the steps that are involved are listed below:

  • .py file --> Byte Code: Your Python script is converted to Byte code, which is a low-level code (not machine code), hence is Platform Independent, do not confuse it with the Byte Code in Java as well, it is similar but not the same. A .pyc file is generated which in most cases is kept hidden from the user (the developer). This file is nothing but, compiled Python (or frozen binaries), and no, Python isn't a compiled language, it's just a tech jargon that we use. However, in some cases this file is visible, for example when you are importing a file, a folder appears magically. Well, it's not magic. That __pycache__ contains the byte code and it is visible to you. CAUTION: I opened this file and cried for half an hour as I couldn't understand anything, turns out, I never needed to.
  • PVM Iterates: This Byte Code is fed to the Python Virtual Machine (PVM) that comes with Python itself when you install it. Inside the PVM, a loop runs over this byte code and executes it line by line and that is why we say Python is an Interpreted Language.

Miscellaneous Questions

Why to use Byte Code at all?

Because it runs faster and is platform independent, its just an optimization. However, you can also feed the raw .py file to the PVM.

How the .pyc file is named?

<source>.<cpython>-<your-python-version>.pyc 

source

Let us say I do have a file hello_chai.py:

print("Chai aur Python")

def chai_peelo(n):
    print(n) 

And I import the chai_peelo function in a file named chai.py

from hello_chai import chai_peelo

chai_peelo("ginger tea")

What is the source? hello_chai.

cpython

Python does have many versions: cpython, jpython, PyPy, stackless, etc. The standard one being cpython.

.pyc

The file extension, tells that it is compiled Python or also known as Frozen Binaries.

python version

It is simply a 3 - digit number representing the version of Python you are working on. For me, it was Python 3.14, however, the versions keep changing as they update the language.

Sample name

Let us take an example and try to name the .pyc file ourself. We write a file named evening_chai.py

print("chai ho jaaye?")

def matthi_nahi_loge(reply):
    print(reply)

And now we import this function in a file matthi_eater.py

from evening_chai import matthi_nahi_loge

matthi_nahi_loge("yes")

Now, let us try to name its .pyc file.

  1. Source: evening_chai
  2. Standard Python: cpython
  3. Version: Although it changes, but, let's take Python 3.14 for this example (314 will be displayed).
  4. File extension: .pyc

Name: evening_chai.cpython-314.pyc

If changes are made in the source file, does the byte code regenerate?

No, the Frozen Binaries won't regenerate completely. Python uses the diffing algorithm inside to differentiate and find out what all was changed. Only the part that was changed, gets updated in the binaries.

A Big Thanks to you Hitesh Sir or I should say, MasterJi. I hope this assignment reaches you and you mark me well 😅

Written by Divye Kapoor with ❤️

No comments yet. Be the first to comment.

Chai aur Python

Part 1 of 1

This series contains everything I learnt while watching Chai aur Python by Hitesh Sir on YouTube