Question

I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture?

Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they are implemented in different languages).

I also read somewhere that Python is C++ interpretation. Is this correct? And what does that mean?

I'm still very new to Python, so forgive me if I ask the dumb questions... Thank you so much!

Was it helpful?

Solution

CPython is the implementation of Python in C. It's the first implementation, and still the main one that people mean when they talk about Python. It compiles .py files to .pyc files. .pyc files contain bytecodes. The CPython implementation also interprets those bytecodes. CPython is not written in C++, it is C.

The compilation from .py to .pyc happens transparently as needed. When you execute a .py file, it will first be compiled to a .pyc file if needed, then the .pyc file will be interpreted.

Jython is different because (in addition to being implemented in Java instead of C) it compiles .py files into .class files so they can be executed in the JVM.

OTHER TIPS

First: The fact that CPython is a bytecode interpreter should not matter to you as a user of python. Go ahead and write code, and don't worry about how it's turned into action.

So, to answer your question and satisfy your curiosity, here is what happens: CPython reads python source code, and compiles it into python byte code, which is stored in the .pyc file. It then executes that code using a bytecode interpreter. This design separates the parsing of python from the execution, allowing both parts of the interpreter to be simpler.

Jython is only the front half -- it reads Python source, and outputs Java bytecodes, which are then interpreted by the JVM. It's the same architecture as CPython, with two noteworthy differences: One: the java bytecode is standardized and documented, while the CPython bytecode is considered internal to python, and can change at any time. Two: the JVM is a whole lot more complicated than the CPython interpreter. The JVM has one of the most advanced JIT engines in the world, while the CPython interpreter is pretty simple.

CPython is both the bytecode compiler, and interpreter (virtual machine).

It automatically detects if no existing pre-compiler file (.pyc) exists, compiles the code, and saves it out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top