CPython is bytecode interpreter?

后端 未结 3 1081
小蘑菇
小蘑菇 2020-12-03 02:10

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

相关标签:
3条回答
  • 2020-12-03 02:35

    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.

    0 讨论(0)
  • 2020-12-03 02:39

    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.

    0 讨论(0)
  • 2020-12-03 02:55

    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.

    0 讨论(0)
提交回复
热议问题