Well, this seems a silly topic at first, but indeed, if you are not cleared with this question, the rest of your Python learning will go in vain. So, this tutorial would try to provide you a crisp yet informative account of the entire process of running a Python code.
Python - The Basic Idea
In general terms, Python is an interpreted programming language. It means that Python code is actually executed by the Python interpreter. To know how a Python interpreter run python code, you need to explore some associated knowledge as under
Whenever you install the Python software into your machine, there would be two entities that get installed at least; and they are
- An Interpreter; and
- A Support Library
The interpreter is the software that runs your Python script line by line and the Support Library provides all the built-in modules, types, file structures, functions, etc.
Let us dive a bit deeper into the Python Interpreter.
Python Interpreter
As mentioned earlier, Python Interpreter is software that executes your Python scripts. Now this software, that is, the interpreter can be written in any language.
- For example, the default Python Interpreter is CPython and it has been developed in C language.
- There is another implementation of Python Interpreter known as Jython which is written with Java.
- A popular version of .NET-based Python Interpreter is IronPython.
From a programmer's or developer's perspective, a Python Interpreter is a black box kind of thing that takes source code as input and gets the program running as output.
But the real understanding of how a Python Program runs depends on how cleanly you can look through this black box. So let us look at the Interpreter more closely.
Internals of Python Interpreter
A Python Interpreter is composed of
- A Compiler
- A Byte Code; and
- A Python Virtual Machine
It looks something like the following
The above diagram shows the following facts
The Python Source Code that you write, is fed to the Compiler. In turn, Compiler, which is a translator in a generic manner, translates the source code into a Byte Code, which in turn is fed to the Virtual Machine. Virtual Machine iterates over all the byte code level instructions take the assistance of the Modules provided by the Support Libraries and ultimately runs the code.
If you already know C and Java, you might be wondering if the Compiler same as that of the C Compiler, or are the Byte Code and Virtual Machine are the same as that of Java?
So let us discuss these issues
Compiler Within Python Interpreter
As we have gone through C programming, there we have seen that C Compiler takes a source code and converts it into machine-level binary codes. But this is just a specific case.
In general, Compilers is system software that translates a program written in one language to the other. Here, in Python, Compiler translates the codes written in Python to Byte Codes. And while doing it, the Compiler (in the case of default CPython) follows the following steps
- Source Code Parsing to generate Parse Tree
- Generation of Abstract Syntax Tree from the Parse Tree
- Generation of Control Flow Graph from the Abstract Syntax Tree; and
- Generation of Byte Code from the Control Flow Graph
Byte Codes Within Python Interpreter
Python Byte Code is a lower-level, platform-independent and intermediate representation of your source code.
Every single Python statement present in your source code is translated by the Compiler into a group of Byte Code instructions.
Byte Code instructions are fast and more optimized than the original source code statements and it is loaded into the memory till the Virtual Machine runs these instructions. After the instruction is executed, the Byte Code is relinquished from the memory.
But if you have any Support Library Module, and if it is being compiled for the very first time, then the byte codes generated out of the compiler are stored in a .pyc or .pyo file. This ensures that you don't have to recompile the same module once again if it is reused in the future.
Virtual Machine Within Python Interpreter
Virtual Machine within the Python Interpreter provides the runtime and it iterates over the Python Byte Codes generated. It is a stack-based system and the newest byte code instruction is pushed into the Python Virtual Machine and is popped by the VM to be executed.
The Python Virtual Machine can use the already saved .pyc or .pyo files (generated out of the Support Library Modules) to execute Byte Code instructions.
So, this was how a Python program run in a nutshell. But if you wish to have more detailed knowledge of this, you can check out our related tutorials.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.