Looking inside the Python code!!
Topic: Looking inside the Python code!!
Tags: How does Python work internally| Is python really an interpretable
language?| Is python compile able language?|What is PVM(Python virtual machine)|Inside
CPython
According to Google:
Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible, and you lose the compile-time type checking of the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs.
Python is a high-level (very high level) language. Some of the known
facts of high-level language and Python are as follows:
1] It is much easier to program in a high-level language. Programs
written in a high-level language take less time to write, they are shorter and
easier to read, and they are more likely to be correct.
2] High-level languages are portable, meaning that they can run on
different kinds of computers with few or no modifications. Low-level programs
can run on only one kind of computer and have to be rewritten to run on another.
Due to these advantages, almost all programs are written in high-level languages. Low-level languages are used only for a few specialized applications.
Two kinds of programs process high-level languages into low-level languages: interpreters and compilers. An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations. A compiler reads the program and translates it completely before the program starts running. In this context, the high-level program is called the source code, and the translated program is called the object code or the executable. Once a program is compiled, you can execute it repeatedly without further translation. Python is considered an interpreted language because Python programs are executed by an Interpreter
So, is it completely an interpreted language? We will see this in a while.
If you want detailed information about how to write Python code, coding techniques, and things related to it, then I would suggest this is not the content for you. This content focuses more on the actual working of python internally.
Well, you can always code N number of times on any of the coding
platforms like hackerearth, hackerrank, etc. where you can practice and learn.
For more information just go to https://www.python.org/about/gettingstarted/
https://docs.python.org/3/tutorial/index.html
From where all the basics can be covered
https://www.tutorialspoint.com/python/index.htm
Basics like
1] Applications of Python
2] Basic syntax
3] Variables, operators, decision making, and loops
4] Strings, tuple, list, dictionary, classes, and objects
Some of my codes to refer to https://github.com/stupidcroc/Basic-Python-codes
(I will try to add here more codes in the future)
Now, coming back to the question is what is inside python? Is it completely an interpreted language?
How is python code executed inside?
Python doesn’t
convert its code into machine code, something that hardware can understand. It
actually converts it into something called byte code. So, within python,
compilation happens, but it’s just not into a machine language. It is into byte
code and this byte code can’t be understood by the CPU. So, we need actually an
interpreter called the python virtual machine. The python virtual machine
executes the byte codes.
Python doesn’t
convert the code written by the user directly into the machine code, unlike the compiler. It converts it into byte code. So, inside the compilation happens but
not into machine language. This byte code cannot be understood by the CPU
(hardware). So, we need an interpreter called, as the python virtual machine
(PVM) which will execute the byte code.
So, Python is both
a compiled and interpreted language. Actually, that is also one reason it is
slow as compared to the traditional C or C++ language.
Let's understand what is CPython first.
CPython is an
implementation of Python programming language. It is basically a mammoth of a program written in C. It can be defined as both an interpreter as well as a compiler as it compiles the Python code written by the user into bytecode
before interpreting it. It compiles a Python code into intermediate bytecode
which is interpreted by the CPython virtual machine. It also acts as a compiler
as it transforms Python code to bytecode before it is interpreted. CPython is
best if you are writing an open-source code Python code and wish to reach a
larger audience. CPython also offers the advantage of speeding up code. CPython
also offers the highest level of compatibility with Python packages as well as
C extension modules. Similarly, we have Jython, PyPy, IronPython.
The reason why
Python is termed as an interpreted language, is that the compiler, in Python does
relatively less work than an interpreter or in a compiled language like C or
Rust.
However, the programming language in itself has no say as to where it is compiled or
interpreted. Rather, it is the property of the implementation that decides
this. The Python interpreter is generally installed as /usr/local/bin/python if
it happens to be available in the system.
The compilation
process, to bytecode, is entirely implicit. This means that you never invoke the
compiler. Instead, you simply run a .py file. The lack of an explicit compile
step is why the Python executable is known as the Python interpreter. The first
step of an interpreter is to read a Python code or instruction. It then checks
the syntax of each line and verifies if the instruction is well-formatted. The
interpreter can display the errors of each line one by one.
An important
feature of Python is it's interactive prompt. A Python statement can be typed
and immediately executed. Most of the compiled languages may not have this
interactivity. Python is compiled to bytecode and this bytecode is immediately
executed without an explicit compile step.
This implicit compilation process involves the following steps which are responsible to convert from source code to byte code:
- Tokenize the source code
- Parse the stream of tokens into an Abstract Syntax Tree
- Transform AST into a Control Flow Graph
- Emit Bytecode based on the Control Flow Graph
- The compiler receives the source code.
- The compiler checks the syntax of each line in the source code.
- If the compiler encounters an error, it halts the translation process with an error message (Syntax error).
- Else if the instruction is well-formatted, then it translates the source code to Bytecode.
- Bytecode is sent to the Python Virtual Machine (PVM).
- Bytecode along with the inputs and Library Modules is given as the input to the PVM.
- PVM executes the Bytecode, if any error occurs, it displays an error message (Runtime error).
- Otherwise, if there is no error in execution, it results in the output.
We will look into the implementation of the compiler for CPython in detail in upcoming blogs followed by the PVM execution. Later will understand how does a compiler and an interpreter works and how Python has a combination of both of them.
Links for
reference:
https://devguide.python.org/compiler/
https://tech.blog.aknin.name/2010/04/02/pythons-innards-introduction/
https://stackoverflow.com/questions/441824/java-virtual-machine-vs-python-interpreter-parlance
Comments
Post a Comment