Python is a programming language oriented on work under developers’ productivity and code readability. Its syntax minimized but still has a huge set of functions in standard library. In this article, we would talk about how it works, what is interpreter, compiler, byte-code and other nuances of this language.
Python interpreter
Interpreter is a special program, which is executing others. When you write a code on Python, interpreter reads it and follow written instructions. So it is like a some special layer of program logic between program code and computer hardware.
Depending on what version on Python is used, interpreter could be realized like C program, set of Java classes or some other view.
Dynamic compilation
After you run the script, Python firstly compile its source to byte-code for virtual machine. Compilation is a simple stage of translation. Byte-code is a low-level platform-independent representation of program source code.
Python translates each instruction written in a script’s source into a group of instructions called byte-code. It is done in order to rise speed of execution because byte-code is much faster. As a result, it creates a special file with extension “.pyc”.
Next time when you would start the program, interpreter skip the compilation step and put on execution created compiled file. But if you have changed the source code the program would compile it again because Python supervise after appearance of any changes.
If Python will not be able to write a file with byte-code. For example, because of write data rights’ absence, program would be executed anyway. Then the byte-code would be created in memory before program starts and deleted after its closing.
Python virtual machine (PVM)
After the step of compilation byte-code sent to special virtual machine which is executing specified in code instructions. Virtual machine is a mechanism of execution. It’s always present in Python system and is an important constituent of part called Python Interpreter.
PVM is just a part of system which have been installed along with interpreter and compiler. All this process works independently and is fully clear for programmer.
Performance
If to compare Python with such languages as C and C++, you’ll notice some important differences. Mostly they are:
Those two moments are the reason of a little bit lower execution of programs in comparison with C/C++. Usage of interpreter needs more time for code execution than machine instructions of microprocessor.
Python interpreter has one advantage over other ones as it does not analyze source code every time because it is already made by compiler. Thereby, Python’s performance is something between compiling and traditional languages’ rate.
Frozen binaries
Sometimes there’s an necessity to create ones own executable files from Python code. Mostly it is necessary for packing and distribution of programs.
Frozen binaries are uniting in one file byte-code, PVM and supporting files, necessary for program execution. In result, you receive the only one executable file, for example, “.exe” for Windows.
Now there are three main instruments for frozen binaries’ creation:
No matter which one you would choose, you’ll have to install it separately.
Remember that frozen binaries are big in size, because they contain PVM. It’s not extremely big size comparing to other modern programs these days.