Python inner working:

When beginning with any programming language, the customary first step is to print "Hello, World!" to the console. Python is no exception:

print("Hello Python!")
# OUTPUT: Hello Python!

Executing this code snippet will undoubtedly produce the expected result: "Hello Python!"

Now, let's delve deeper and create a function named Coffee:

def Coffee(n):
    print(n)

Coffee(5)
#OUTPUT: 5

Next, let's explore the process of importing this Coffee function into another file and observe the changes. Upon running the script, you'll notice the addition of a new folder in your directory named __pycache__, housing a file named hello_python.cpython-312.pyc.

This is where we begin our exploration into the inner workings of Python.

Behind the scenes:

Execution Process of Python Scripts

1. Compilation to Byte Code: Python source code is not traditionally compiled into machine code. Instead, it is translated into Byte Code during the interpretation process. This Byte Code is a low-level representation that is platform-independent.

2. Byte Code and Platform Independence: Byte Code serves as an intermediary step, allowing Python programs to run on any system with a Python Virtual Machine (PVM). This platform independence is crucial for the portability of Python code across different operating systems.

3. Benefits of Byte Code: The execution of Byte Code is faster than running raw scripts because many checks and syntax validations are performed at this stage. The platform independence also contributes to the efficiency of Python programs.

4. .pyc Files in the pycache Folder: When a Python script is executed, a compiled version of the code, known as a "frozen binary," is stored in a .pyc file within the pycache folder. This folder serves as a cache for compiled files, preventing the need for recompilation upon subsequent executions.

5. Purpose of pycache Folder: The pycache folder is created and managed by Python. It acts as a storage space for compiled files, enhancing the performance of Python scripts by avoiding repeated compilation.

6. File Naming Convention (e.g., hello_python.cpython-312.pyc): The .pyc file inside pycache includes essential information such as source code changes and the Python version used. The file name, like "hello_python.cpython-312.pyc," indicates the associated Python version (in this case, version 3.12).

Python Virtual Machine (PVM):

7. Python Virtual Machine (PVM): The Python Virtual Machine (PVM) plays a central role in executing Python code. It is responsible for interpreting the Byte Code and translating it into machine code that can be executed by the underlying hardware. The PVM acts as a runtime engine, managing the execution of Python programs.

8. Runtime Engine and Python Interpreter: The PVM can be referred to as the runtime engine of Python. It is essentially the component responsible for executing the Byte Code generated from the Python source code. This execution process is commonly known as interpretation, and thus, the PVM is often referred to as the Python Interpreter.

9. Byte Code Iteration Loop: During the execution of Python programs, the PVM uses a loop to iterate over the generated Byte Code. This loop interprets each instruction in the Byte Code, facilitating the dynamic execution of Python scripts.

10. Byte Code Is Not Machine Code - Python-Specific Interpretation: It's important to emphasize that Byte Code is not machine code. Instead, it is a Python-specific intermediate code that the PVM understands and executes. This Python-specific interpretation allows for the platform independence of Python code.

11. Python Implementations: While CPython is the standard and most widely used implementation of Python, there are alternative implementations, each with its unique features. Some notable implementations include:

  • Jython: Runs on the Java Virtual Machine (JVM).

  • IronPython: Designed for integration with the .NET Framework.

  • Stackless Python: Focuses on concurrency and tasklet-based programming.

  • PyPy: A just-in-time compiler that aims to improve the execution speed of Python programs.

12. CPython - Standard Implementation: CPython is the default and standard implementation of Python. It is written in C and is the reference implementation, providing the official interpreter for the Python language. When people refer to Python as a language, they often implicitly mean CPython.

THIS ARTICLE IS A SUMMARY OF VIDEO BELOW by: Hitesh Choudhary