I had some time to read a python book I bought a few years ago, O ‘Reilly’s Python Learning Guide.
Unfortunately, I read very little of it, which is why I’m worried about being too familiar with Python on my resume. Although the learning value of Java is greater, but learn usually do not use, forget quickly. Plus, I felt more comfortable doing problems in Python (crap, you’re not familiar with anything else), so I decided to finish the book and write down details that I didn’t know or didn’t know before.
What is a Python interpreter
1. Interpreted and compiled languages
Speaking of interpreters, you’ve heard of interpreted and compiled languages before. First of all, the source code for either of them is unreadable to a computer, which only understands binary instructions. So, in order for the program to be executed, the natural first step is to convert the source code into binary instructions, or machine code.
So, when do you switch?
- Some languages require that the code must be converted in advance, which is a compiled language, with conversion tools called the compiler, such as C language, C++.
- Some languages can be converted at the same time, and transferred where it is used, which is an interpreted language, using conversion tools called interpreters, such as Python and javascript.
Java is special in that it has both compilation and interpretation. However, compilation is not directly compiled into machine code, but into bytecode, which is then taken out to the virtual machine for execution. This is designed to be efficient across platforms, but won’t be discussed here.
Ok, that’s the setup.
2. Python interpreter
The Python interpreter is a program that makes other programs run. Python code that you write must run inside the interpreter, which is already installed when you install Python.
So, now Iprint(hello world)
, and the console can see the output of the text.
3. The running process of python programs
The above line of code is simple, but python runs it in two steps:
- Compiling source code into “bytecode”
- Forward bytecode to virtual Machine
Bytecode compilation can be interpreted as translation. Print (Hello world) is translated into bytecode, which is saved in a.pyc file, which is the compiled.py source code.
These bytecodes run much faster than source code. Why is that?
- Because the next time the program runs, if the source code has not been modified since the last bytecode save, Python will load
.pyc
File and skip the compilation step.
When you have to recompile, Python automatically checks the timestamps of the source and bytecode files, and if you save the source code again, the bytecode will be recreated automatically the next time the program runs.
Python Virtual Machine (PVM). When a Python Virtual Machine has a bytecode file, it is sent to the PVM for execution.
Note here that PVM does not refer to a stand-alone program that does not need to be installed. PVM can be thought of as Python’s run engine, a large loop that iterates through bytecode instructions, one by one, until it finishes.
From a technical point of view, PVM is the final step in the “interpreter.”
Second, about Python performance
The downside of Python, as you all know, is that it’s slow. But this is only relatively slow, and not fast enough compared to compiled languages like C and C++.
Why is that?
As mentioned above, Python converts the source code into bytecode and then interprets the bytecode. But for better cross-platform, the bytecode here is a platform-independent format, so it’s not the underlying binary. So far, Python has been able to handle most applications that are not speed-demanding, such as numerical computation or animation, where the core processing unit is often required to run at least C speed.
In contrast to its disadvantages, python is also an important factor that many people love.