Python Behind the Scenes

Python Behind the Scenes

ยท

2 min read

I have been using Python since 2017, but only recently started learning about the internals and the core fundamentals of Python.

Here is my attempt to break it into smaller chunks for any beginner in the field who is trying to understand it from a prespective of another learner.

To understand this further, I hope you have Python installed in you machine and can run a basic hello world program , I would recommend you to run it in VScode :)

Make a file display.py and add the following lines of code:

and make another file in the same folder called learn.py and run it !

notice a strange folder pycache on your left hand side and open it to see another file called display.cpython-310.pyc

If you try to open this file, you might be able make sense of it. So what exactly are these folders in between our simple hello world programs.

The diagram above shows the simple mechanism of running a python program. Notice the Byte code , this byte code is a low-level code(intermediate) but not a Machine code.

The Byte code is a platform independent code and runs faster.

The .pyc file we saw earlier is nothing but a Compiled Bytecode(frozen Binaries)

This usually is visible for imported files otherwise the pycache folder remains hidden and works in the background.

P.s Frozen Binaries : Well it basically means to bundle the bytecode, cirtual machine codes files, and other files into a single bundle. For example convert to a .exe file by making it to a single executable binary file.

Yes ! The source code compiled to a bytecode .

__pycache __ folder is a system folder for containing the compiled and optimised bytecode corresponding to our program files and there updation.

The files name display.cpython-310.pyc can be broken as:

I hope the diagram make it clear !

Python Virtual Machine

It is a the runtime engine that helps execute Python programs and converts bytecode line by line to machine code, hence it is also called the Python Interpreter.

There is alot of things to learn and write about , and Python is a beautiful language and I hope I could make few things clear to you all.

Give some love, like to Hitesh Sir !

Happy coding :)

Happy Learning !!

ย