How Python Works Under the Hood: A Deep Dive
Understand what happens when you run a Python script!
This edition of the newsletter contains a short write-up about the inner workings of Python, how your Python code is compiled and the understanding of different python distributions. Youβll also gain insights about the compilation process, from parsing to bytecode generation and storage.
Inner Working of Python
A python source code is a .py file. When you run a python script, it first gets compiled into a bytecode.
The bytecode generated is essentially is a .pyc file which is typically located in the pycache folder.
The next time you run the script, if there is no change in the source code, it skips the compilation step and directly executes the bytecode from the .pyc file, making execution faster.
After the compilation, the bytecode is taken by the python interpreter and the instructions are executed at runtime.
Now there are a lot of python distributions which have a combination of built-in compiler and interpreter both in them. CPython is the default and most widely used implementation of python programming language. Itβs written in C, and it compiles Python code to bytecode and then interprets that bytecode to execute the program.
Below is the list of some popular implementations :
CPython
PyPy
Jython
Iron Python
Micro Python
What Exactly Happens During Pythonβs Compilation Process?
The compilation of a python source code by the built-in compilers in any python distribution mostly involves three processes :
Parsing (includes Lexical Analysis)
Compilation
Storage
Hereβs what happens in case of CPython implementation :
Parsing: The Python source code (.py file) is broken down into tokens, which are the smallest units of meaning like keywords, identifiers, operators, and literals. It is parsed into an Abstract Syntax Tree (AST), which represents the structure of the code.
Compilation: The AST is converted into bytecode, a lower-level, platform-independent representation of the code.
Storage: The bytecode is saved in a .pyc file (or in memory) for future execution. This bytecode is what CPythonβs interpreter then runs to execute the program.
I regularly share technical content on my social platforms. If that aligns with your interests, feel free to follow me on LinkedIn, YouTube, and Instagram. Also, if you have any doubts, donβt hesitate to reach out! π