Python Inner Working

Ankush LadaniAnkush Ladani
2 min read

Is Python interpreted language or compiled language?

๐Ÿ’ก
Python is an interpreted language. And it executes .py or script line by line.

Behind the scenes when we write code in python and execute it

So basically below are the process or steps happen when we execute any .py file :-

  1. First our whole python code is Compiled into Byte code.

    ๐Ÿ’ก
    DON'T THINK THAT BYTE CODE IS MACHINE CODE. MACHINE CODE IS THE CODE WHICH IS DIRECTLY FEED TO ANY HARDWARE. BYTE CODE IS PLATFORM INDEPENDENT CODE.
    ๐Ÿ’ก
    Byte code is sometimes called low level code also. When any big company wants to make their execution faster in cloud , they simply convert scripts into Byte Code and that's it Done. And they simply feed this Byte Code to Python Virtual Machine.๐Ÿš€
    ๐Ÿ’ก
    One thing to know here is that Byte Code executed faster than simple python scripts as Syntax error checking , Attribute error checking , Name error checking , etc. has been done by interpreter.
    ๐Ÿ’ก
    And Byte Code is stored in .pyc ( Compiled Python ) file. It's naming convention is based on file name from which we import any method and python version which we are using. For Ex. - settings.cpython-312 , where settings is the filename and version is 3.12. Cpython is the standard implementation of python and mostly used version. Other than Cpython we have IronPython , Jython , PyPy , Stackless Python , MicroPython , etc.
  2. For converting Byte Code into machine understandable language or machine code , we have Python Virtual Machine ( PVM ) which is provided by python community and every company uses it and do their work.

    ๐Ÿ’ก
    PVM is nothing but just a code loop which iterates again and again.
  3. And one interesting folder is created whenever we import some method from any other module or file. Its name is __pycache__. This folder consist of .pyc files and if you do change in existing code .pyc file will be created again and these changes are merged with existing one.

I am attaching one video here from one of the best tutorial I have found on Youtube.๐Ÿ˜„๐Ÿ˜…

So that's it for this article and if you find it useful , don't forget to like this article and follow me on LinkedIn.

0
Subscribe to my newsletter

Read articles from Ankush Ladani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankush Ladani
Ankush Ladani