The inner working of python
Having recently tuned into a video by Hitesh Sir, I'm set to distill the essence of his insights in this article. Brace yourself for a condensed version of the crucial takeaways from the video. The only prerequisite for diving into this summary is the ability to write a simple "Hello World" program in Python. So, if you've got that covered, let's explore the depths of Python together
Let's start with an example
print_chai.py
def chai(n):
print(n)
chai.py
from print_chai import chai
print("Chai Type")
chai("Masala Chai")
After running this code the following question may arise.
What is this __pycache__ ?
What is the puropse of the file print_chai.cpython-311.pyc ?
Why are they named like that ?
Can i run python without it ?
Will python reproduce it if I make changes in the code ?
The Theory
When we run python chai.py
the following things happen.
The Python Virtual Machine
Code loop to iterate bytecode
Runtime Engine
Also known as Python Interpreter
The __pycache__ directory is where the generated byte code is stored.
Have you ever wondered about the significance of bytecode when our ultimate aim is simply to execute a script?
Bytecode serves as a low-level and platform-independent code, often referred to as "Frozen Binaries."
One noteworthy aspect is that bytecode remains mostly hidden from the developer's direct view. It comes into play specifically for imported files. If there are no imports the the *.py is directly fetched into the Python Virtual Machine (PVM).
The bytecode, essentially a set of instructions, is fetched into the PVM, which subsequently executes it. This intermediate step holds key advantages.
Notably, bytecode contributes to enhanced execution speed. Since much of the syntax checks and parsing are already performed during the bytecode generation process, the execution within the PVM becomes more efficient.
It's crucial to understand that Bytecode is specific to Python and isn't the same as machine code. Think of Bytecode as Python's own special language translation. It's like a set of instructions in Python's unique way of speaking. Importantly, Bytecode won't function in a Java Virtual Machine, just in case you were curious. Each programming language has its own set of rules, and Python's Bytecode is designed to work specifically with Python, not with other languages like Java.
Did you know that Python comes in various flavors? Some of the most notable ones include CPython, Jython, IronPython, Stackless, and PyPy. Each of these variations has its own unique features and capabilities. For instance, CPython is the default and most widely used implementation, while Jython allows Python code to run on the Java Virtual Machine. IronPython is tailored for integration with .NET, Stackless focuses on concurrency, and PyPy aims to enhance performance through a Just-In-Time (JIT) compiler.
Subscribe to my newsletter
Read articles from Rushabh Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by